Kalin Kochnev
Kalin Kochnev

Reputation: 347

Listing nearby phones with bluetooth on (flutter_blue package)

So I'm trying to get a list all bluetooth devices (phones specifically) but Flutter doesn't seem to be seeing the phones. I have this method called on a button press just to test what gets output. Here is the code for it

//Listen to scan results
flutterBlue.startScan(timeout: Duration(seconds: 4));

var subscription = flutterBlue.scanResults.listen((scanResult) {
  for (ScanResult scan in scanResult) {
    BluetoothDevice device = scan.device;
    print('${device.name} found! rssi: ${scan.rssi}dBm');
  }
});

After pressing the button I get this:

I/flutter (12864): Device1 found! rssi: -94dBm
I/flutter (12864): Device1 found! rssi: -94dBm
I/flutter (12864):  found! rssi: -70dBm
I/flutter (12864): Device1 found! rssi: -94dBm
I/flutter (12864):  found! rssi: -70dBm
I/flutter (12864): Jabra Evolve 75e found! rssi: -70dBm
I/flutter (12864): Device1 found! rssi: -94dBm
I/flutter (12864):  found! rssi: -70dBm
I/flutter (12864): Jabra Evolve 75e found! rssi: -70dBm
I/flutter (12864): Inspire HR found! rssi: -86dBm

Some weird things are happening. I seem to be getting duplicate listings and there seems to be a device with no name. But the phone I enabled discoverability on does not show up. I'm expecting something like this: Moto G5 found! rssi:##dBm

I also connected directly to the phone and tried to list all connected devices with no luck:

for (BluetoothDevice device in await flutterBlue.connectedDevices) {
    print('Connected device: ${device.name} ${device.id}');
});

Output:

D/FlutterBluePlugin(12864): mDevices size: 0
D/FlutterBluePlugin(12864): mDevices size: 0
I/Surface (12864): opservice is null false

Does flutter_blue not support scanning for phones? I just started using this so I'm not sure what I'm doing.

Thanks for the help!

Upvotes: 0

Views: 4237

Answers (1)

JJ_Coder4Hire
JJ_Coder4Hire

Reputation: 4901

you can advertise with this package: https://pub.dev/packages/beacon_broadcast

Alternatively if your app will be running on all the phones you want to discover, you can use the nearby api wrapped for flutter in the package: nearby_connections, found here: https://pub.dev/packages/nearby_connections I have a usecase where my app is installed on multiple phones, and I can actually send custom data like userId/email/firstname over bluetooth, so the app discovers each device to get the userid and can lookup from the database for more info and further interaction on the UI.

Upvotes: 2

Related Questions