The aim of FlutterBluetoothManager
is to handle multiple devices and their streams with ease. It is built on top of the Flutter Bluetooth Package FlutterBluePlus
.
The BluetoothManager creates a DeviceModel. This represents the device and it's corresponding Services and Characteristics. A device is unique to a DeviceModel. There can be multiple Services and Characteristics per DeviceModel.
The general Structure looks like this:
class DeviceModel {
final BluetoothDevice device;
final List<BluetoothService> services;
final List<CharacteristicStream> characteristicsStream;
DeviceModel(this.device, this.services, this.characteristicsStream);
}
CharacteristicStream contains the BluetoothCharacteristic and Info wether it's Stream is initialized.
class CharacteristicStream {
final BluetoothCharacteristic characteristic;
bool isInitialized;
CharacteristicStream({
required this.characteristic,
this.isInitialized = false,
});
}
Following pictures shows the example of the BluetoothManager.
![]() |
![]() |
![]() |
![]() |
To get started you simply create an instance of the
BluetoothManager bluetoothManager = BluetoothManager();
bluetoothManager.connectDevice(BluetoothDevice device);
bluetoothManager.disconnectDevice(BluetoothDevice device);
A common problem is, if you accidently lose connection to a device it can lead to an exeption. With this function you constantly check the device connectionState for all connected devices and if it loses a connection the device will be disconnected and removed from the DeviceModel preventing exeptions or potential errors.
bluetoothManager.checkDeviceConnection();
To retrieve a Characteristic for subscribing to a stream or writing message you can perform this by calling getCharacteristic
. Since there can be multiple Characteristics per Service you can choose it by Number.
bluetoothManager.getCharacteristic(BluetoothDevice device, int characteristicNumber);
To receive a stream you need to call the openStream
function. It takes a characteristic
and returns/yields a Stream<List<int>>
.
bluetoothManager.openStream(BluetoothCharacteristic? characteristic);
You can handle multiple streams by simply calling the streamHandler. It takes the device index value and a stream as input. You need to use the stream of openStream
.
bluetoothManager.streamHandler(index, stream);
Retrieve a stream for Streambuilder by its device index
bluetoothManager.getStream(index)
bluetoothManager.closeStream(index)
Tested with IOS17.