flutter_blue icon indicating copy to clipboard operation
flutter_blue copied to clipboard

Help! connect to device

Open coduy96 opened this issue 5 years ago • 7 comments

How can I connect to device if I only have mac address value? Thanks a lot!

coduy96 avatar Aug 07 '19 05:08 coduy96

If you know the information of the device beforehand then you can create a BluetoothDevice object yourself and use that to connect. Simple example:

BluetoothDevice myDevice = BluetoothDevice(
   id: DeviceIdentifier("MAC address"),
   type: BluetoothDeviceType.le,
   name: "Device Name",
),
await myDevice.connect();

Pre Flutter Blue 0.6.0:

BluetoothDevice myDevice = BluetoothDevice(
   id: DeviceIdentifier("MAC address"),
   type: BluetoothDeviceType.le,
   name: "Device Name",
),
FlutterBlue.instance.connect(mydevice);

Fleximex avatar Aug 08 '19 12:08 Fleximex

Thanks for your support <3

coduy96 avatar Aug 09 '19 04:08 coduy96

On iOS you cannot connect to a device via mac address - they are not exposed to the users (https://stackoverflow.com/a/2971250/4312184). As I know, the only way to do it is to store device mac address inside an advertisement manufacturer data. At least we do it with our devices and everything working good.

votruk avatar Aug 09 '19 13:08 votruk

I am using ^0.7.2 version and when I run on apple device my app, the logs don't show me the mac address, but I know the device mac address to connect to, the code above doesn't work for me. How can in create device in connect to it? Problem: BluetoothDevice doesn't have a default constructor. Thank you.

JUREXT avatar Aug 05 '20 19:08 JUREXT

I am using ^0.7.2 version and when I run on apple device my app, the logs don't show me the mac address, but I know the device mac address to connect to, the code above doesn't work for me. How can in create device in connect to it? Problem: BluetoothDevice doesn't have a default constructor. Thank you.

I'm also in trouble

yuriboeira11tx avatar Apr 20 '21 13:04 yuriboeira11tx

Does anyone have any idea how to solve this issue, I also want to connect to a specific device, and there is no longer the constructor in the BluetoothDevice class.

WanKapef avatar Jan 12 '22 16:01 WanKapef

@WanKapef try importing this

import 'package:flutter_blue/gen/flutterblue.pb.dart' as ProtoBluetoothDevice;

and then use the ProtoBluetoothDevice.BlueoothDevice constructor.

    var protoBt = ProtoBluetoothDevice.BluetoothDevice(
      remoteId: uuid
    );
    var d = BluetoothDevice.fromProto(protoBt);

unitedsoftworks avatar Jul 06 '22 17:07 unitedsoftworks