Know if Bluetooth is on, Flutter Blue

319

If you are using flutter blue, then this should work.

MaterialApp(
  color: Colors.white,
  home: StreamBuilder<BluetoothState>(
      stream: FlutterBlue.instance.state,
      initialData: BluetoothState.unknown,
      builder: (c, snapshot) {
        final state = snapshot.data;
        if (state == BluetoothState.on) {
          return FindDevicesScreen();
        }
        return BluetoothOffScreen(state: state);
      }),
);

The state of Bluetooth is updated in the Stream of FlutterBlue.instance.state You can easily access the state of Bluetooth using this stream. FYI, for this to work you should ask for the permissions to use Bluetooth on any device.

Share:
319
aaronhaddad_
Author by

aaronhaddad_

Updated on December 25, 2022

Comments

  • aaronhaddad_
    aaronhaddad_ over 1 year

    I am making an app based on Bluetooth functionality, in order to redirect the user to the correct page I need to know whether Bluetooth is on or not. Does anyone know how I can do that with Flutter Blue?

    • Ashok
      Ashok over 3 years
      Show us what you've tried so far
    • aaronhaddad_
      aaronhaddad_ over 3 years
      I actually solved the problem, it turned out I needed a BlueoothState in order to know whether it is active or not
  • aaronhaddad_
    aaronhaddad_ over 3 years
    Really appreciate it mate, I solved it a few minutes before your reply in exactly the same way. Really appreciate it