flutter_blue icon indicating copy to clipboard operation
flutter_blue copied to clipboard

How to find out if we're already connected to a device?

Open lambasoft opened this issue 5 years ago • 14 comments

When my app start, I need to know if I'm already connected to a device. How can I do that using flutter_blue, I couldn't find the function anywhere.

lambasoft avatar Mar 05 '19 02:03 lambasoft

What I do is keep track of the connected devices in the Flutter app itself. So when you are connected you just set a boolean to true.

Even better is to use this (also used in the Flutter Blue example):

deviceStateSubscription = device.onStateChanged().listen((s) {
  setState(() {
    deviceState = s;
  });
});

This will set deviceState to either of 4 states.

Then simply write a isConnected bool in your code: bool get isConnected => (deviceState == BluetoothDeviceState.connected);

When the device you are connected to disconnects the deviceState should automatically be updated by the onStateChanged() stream. When your Flutter app disconnects make sure you hard code deviceState = BluetoothDeviceState.disconnected; in your code. When you disconnect the deviceState = s; part may never be called again because device may already be null or the deviceStateSubscription is already cancelled or null (depending on your code).

Fleximex avatar Mar 12 '19 13:03 Fleximex

As far as I can tell, the only way to know about connected devices is to keep track of it from the .connect() on? I don't see any way to pull already connected devices, via FlutterBlue or the platform. .scan() seems to skip connected ones as well.

insequent avatar Apr 24 '19 18:04 insequent

I'm needing this too. Using version 0.6.0+1. The connect() function returns void. How to check if the connection succeded?

rrnicolay avatar Jun 22 '19 14:06 rrnicolay

try FlutterBlue.instance.connectedDevices

LastMonopoly avatar Jun 23 '19 05:06 LastMonopoly

I'm using the code below. But, it would be better if connect() returned the status.

void _connect(BluetoothDevice device) async {

    _stopScan();

    await device.connect();

    List<BluetoothDevice> connectedDevices = await flutterBlue.connectedDevices;

    if(connectedDevices.contains(device)) {
      Navigator.pushReplacementNamed(context, '/home');
    }
  }

rrnicolay avatar Jun 24 '19 11:06 rrnicolay

I've two devices connected to my Galaxy Tab S3 but using @LastMonopoly snippet the connected devices list is always empty even if it is not the truth image image

enricobenedos avatar Jan 08 '20 08:01 enricobenedos

@enricobenedos FlutterBlue.instance.connectedDevices will give u devices connected using FlutterBlue, not all Bluetooth-connected devices

LastMonopoly avatar Jan 08 '20 15:01 LastMonopoly

@LastMonopoly thank you for the clarification!

enricobenedos avatar Jan 08 '20 16:01 enricobenedos

So just to be clear, there is no way to get a list of the already connected devices to the phone prior to FlutterBlue being initialized? I am curious about what the limitations are that are stopping this from happening.

J0nSnuw avatar Apr 14 '20 23:04 J0nSnuw

Just wondering as well if there is a way to know currently connected device NOT connected using flutter_blue.

themobilecoder avatar Apr 30 '20 15:04 themobilecoder

I have the same issue! For security reasons there're bluetooth devices that don't broadcast themselves, but only connect to devices and don't let anyone find that device.

Ahmadre avatar Jul 11 '20 22:07 Ahmadre

Some day I want to look into this, I don't understand why it isn't what we expect as I see in Logcat that android is correctly reporting the connected devices. I also don't understand why people aren't crying about this more, it's kind of a big deal if you actually need to connect to a device for your usecase

SaikCaskey avatar Aug 27 '22 12:08 SaikCaskey

No solution for years?! hmm, I also looking for it, my app need to listen if the phone auto connect to a device (which already connected before).

ngoan98tv avatar May 30 '23 01:05 ngoan98tv

Maybe it's worthwhile to raise this same ticket in the flutter_blue_plus repository as it is actively being maintained by the community since flutter_blue is not.