1

I am trying to get the current device Bluetooth Address, the one that is supposed to show as id to other devices when I run a Bluetooth scan. Any idea how I can do that?

1 Answers1

0

I didn't quite understand if you're trying to find a Bluetooth device to connect, using FlutterBlue, or if you want your device to be reachable.

So let's discuss both options.

Find other device

With FlutterBlue, you could look for devices and connect to them, so assuming they are reachable, you need to first instantiate your FlutterBlue, and then scan for devices:

FlutterBlue flutterBlue = FlutterBlue.instance;
flutterBlue.startScan(timeout: Duration(seconds: 4));

This scan method will create a stream-like object, to which you need to subscribe to get the results:

var subscription = flutterBlue.scanResults.listen((results) {
    // do something with scan results
    for (ScanResult r in results) {
        print('${r.device.name} found! rssi: ${r.rssi}');
    }
});

// You could stop the scan as well, after finding what you need or just got every device on the list
flutterBlue.stopScan();

Why the device I want to find is not reachable?
There are 2 possible reasons for this:

  1. The device is not exposing its Bluetooth connection, so other devices cannot access it. You need to turn the Bluetooth on, and ensure it's allowing the reachability (it's a OS configuration, on the device settings)
  2. FlutterBlue only returns devices that are not connected, so if you already connected to a device, then you won't get the name on the scan.

Make my device discoverable

From my experience, FlutterBlue doesn't support making changes to the Bluetooth state, or change the discoverability of the device to other devices. I suggested looking for alternative solutions.

Making a quick search on packages, I found this one that might work, but I haven't tested to tell you for sure: https://pub.dev/documentation/flutter_bluetooth_serial/latest/

Extra: Find my device ID

If you want to find your device ID, there's this lib device_info that could help. Check this thread for a complete overview on implementation: https://stackoverflow.com/a/49187284/10642035

Luan Naufal
  • 1,346
  • 9
  • 15
  • 1
    I want to know my device's hardware Bluetooth address, the one that shows to other devices when from devicesList[index].id I want to find my device's id, it looks something like this 00:11:22:AA:BB:CC but its not the Bluetooth mac – aaronhaddad_ Jan 19 '21 at 13:20
  • Just updated my response with this option, @AaronHaddad! – Luan Naufal Jan 19 '21 at 13:26
  • Does the device ID look anything like aa:bb:cc:11:22:33? Because I am trying to find the address that shows as the scanResult.id – aaronhaddad_ Jan 20 '21 at 13:38
  • 3
    I want to get this address https://developer.android.com/reference/android/bluetooth/BluetoothDevice#getAddress() Is there anyway I can do that in Flutter? – aaronhaddad_ Jan 20 '21 at 13:41