flutterblue
flutterblue is a bluetooth plugin for flutter, a new mobile sdk to help developers build modern apps for ios and android.
cross-platform bluetooth le
flutterblue aims to offer the most from both platforms (ios and android).
using the flutterblue instance, you can scan for and connect to nearby devices (bluetoothdevice). once connected to a device, the bluetoothdevice object can discover services (bluetoothservice), characteristics (bluetoothcharacteristic), and descriptors (bluetoothdescriptor). the bluetoothdevice object is then used to directly interact with characteristics and descriptors.
usage for bluetooth plugin
obtain an instance
flutterblue flutterblue = flutterblue.instance;
scan for devices
/// start scanning
var scansubscription = flutterblue.scan().listen((scanresult) {
// do something with scan result
});
/// stop scanning
scansubscription.cancel();
connect to a device
/// create a connection to the device
var deviceconnection = flutterblue.connect(device).listen((s) {
if(s == bluetoothdevicestate.connected) {
// device is connected, do something
}
});
/// disconnect from device
deviceconnection.cancel();
discover services
list<bluetoothservice> services = await device.discoverservices();
services.foreach((service) {
// do something with service
});
read and write characteristics
// reads all characteristics
var characteristics = service.characteristics;
for(bluetoothcharacteristic c in characteristics) {
list<int> value = await device.readcharacteristic(c);
print(value);
}
// writes to a characteristic
await device.writecharacteristic(c, [0x12, 0x34])
read and write descriptors
// reads all descriptors
var descriptors = characteristic.descriptors;
for(bluetoothdescriptor d in descriptors) {
list<int> value = await device.readdescriptor(d);
print(value);
}
// writes to a descriptor
await device.writedescriptor(d, [0x12, 0x34])
set notifications
await device.setnotifyvalue(characteristic, true);
device.onvaluechanged(characteristic).listen((value) {
// do something with new value
});
reference
flutterblue api
android | ios | description | |
---|---|---|---|
scan | :white_check_mark: | :white_check_mark: | starts a scan for bluetooth low energy devices. |
connect | :white_check_mark: | :white_check_mark: | establishes a connection to the bluetooth device. |
state | :white_check_mark: | :white_check_mark: | gets the current state of the bluetooth adapter. |
onstatechanged | :white_check_mark: | :white_check_mark: | stream of state changes for the bluetooth adapter. |
bluetoothdevice api
android | ios | description | |
---|---|---|---|
discoverservices | :white_check_mark: | :white_check_mark: | discovers services offered by the remote device as well as their characteristics and descriptors. |
services | :white_check_mark: | :white_check_mark: | gets a list of services. requires that discoverservices() has completed. |
readcharacteristic | :white_check_mark: | :white_check_mark: | retrieves the value of a specified characteristic. |
readdescriptor | :white_check_mark: | :white_check_mark: | retrieves the value of a specified descriptor. |
writecharacteristic | :white_check_mark: | :white_check_mark: | writes the value of a characteristic. |
writedescriptor | :white_check_mark: | :white_check_mark: | writes the value of a descriptor. |
setnotifyvalue | :white_check_mark: | :white_check_mark: | sets notifications or indications on the specified characteristic. |
onvaluechanged | :white_check_mark: | :white_check_mark: | notifies when the characteristic’s value has changed. |
state | :white_check_mark: | :white_check_mark: | gets the current state of the bluetooth device. |
onstatechanged | :white_check_mark: | :white_check_mark: | notifies of state changes for the bluetooth device. |
troubleshooting
scanning for service uuid’s doesn’t return any results
make sure the device is advertising which service uuid’s it supports. this is found in the advertisement
packet as uuid 16 bit complete list or uuid 128 bit complete list.
Comments are closed.