I've been using flutter Blue for some now and I'm stuck on the following: I'm using the example App I downloaded on https://github.com/pauldemarco/flutter_blue, through here the basic idea is that as soon as I connect to my bluetooth device it starts checking if the service "FFE0" exists and then the characteristic "FFE1". This Characteristic spits out random strings I need for my project.
Image of screen with characteristic open
Through the screen I can see the above is true I just need to somehow automatically set notifications for the characteristic as soon as it connects to the bluetooth device.
This is some current code i'm testing out in the _Connect Function.
_connect(BluetoothDevice d) async {
device = d;
// Connect to device
deviceConnection = _flutterBlue
.connect(device, timeout: const Duration(seconds: 4))
.listen(
null,
onDone: _disconnect,
);
// Update the connection state immediately
device.state.then((s) {
setState(() {
deviceState = s;
});
});
// Subscribe to connection changes
deviceStateSubscription = device.onStateChanged().listen((s) {
setState(() {
deviceState = s;
});
if (s == BluetoothDeviceState.connected) {
device.discoverServices().then((service) {
service.forEach((_service){
var characteristics = _service.characteristics;
_service.characteristics.map((c) {
print(c);
});
for(BluetoothCharacteristic _characteristic in characteristics) {
device.readCharacteristic(_characteristic).then((_value){
print(_value);
if (_value.contains("FFE0")) {
print("Found!!");
// do something
}
});
}
});
setState(() {
services = service;
});
_getServices();
});
}
});
}
I maybe someone has a suggestion on how to approach my problem.
Robin