Hi. I get an exeption when trying to read a characteristic ( c.read() )
Here is the exeption:
_Exception has occurred.
PlatformException (PlatformException(read_characteristic_error, unknown reason, may occur if readCharacteristic was called before last read finished., null))_
Can you please help me with this?
@marileen-jansen Did you solve this problem?
@marileen-jansen Did you solve this problem?
No I have not.
add one
+1
I realized I had a characteristic that did not have a read property in my list of characteristics.
I added this if statement and everything seemed to work fine.
if (c.properties.read)
{
List<int> value = await c.read();
print(value);
}
Still same problem.
This library is unfortunately have not been maintained lately.
Have you guys tried https://github.com/Polidea/FlutterBleLib?
It looks promising. I will move there I guess.
@aytunch The plugin at FlutterBleLib is maintained by the Polidea folks, and utilizes their tried and tested platform libs for Android and iOS. I'm sure you'll be in good hands.
FlutterBlue is still under development and I'll be disclosing project plans for 2020 in the coming weeks.
Thanks.
forEach doesn鈥檛 wait for callback return, it just launches them and returns. If you are going to perform async operations inside forEach, you have to use 芦for in禄 instead.
Don鈥檛:
services.forEach((service) async {
// do something with service
});
Do:
for (BluetoothService service in services) {
// do something with service
}
you should listen to it.
for(BluetoothService service in services){
print("service id is : ${service.uuid}");
if (_matchSmartLockServiceId(service.uuid.toString())) {
var characteristics = service.characteristics;
for (BluetoothCharacteristic c in characteristics) {
print("### ${c.uuid}");
String c_id = c.uuid.toString();
if (_getBlueToothFunctionCode(c_id) == "*"){
await c.setNotifyValue(true);
c.value.listen((value) {
_readInfoFromDevice(value);
});
}else if (_getBlueToothFunctionCode(c_id) == ""){
_sendMsg(c);
}
}
}
_readInfoFromDevice(List
if(values.length>0){
print("value: ${values}");
}
}
_sendMsg(BluetoothCharacteristic c) async{
//do something
}
bool _matchSmartLockServiceId(String serviceId) {
//do your logic
if (serviceId.isEmpty) return false;
if (serviceId.substring(0, 4) == "*") return true;
return false;
}
+ 1
+1
+1
Confirmed: This happens on iOS only for me now. Android is working 100%. Shifted to flutter_reactive_ble and it works perfectly there.
+1
Note that this only works when the characteristic support Notifications - not all will do.
so check for c.isNotifying and then either read, or listen for a notification
you should listen to it.
if (_getBlueToothFunctionCode(c_id) == "**"){ await c.setNotifyValue(true); c.value.listen((value) { _readInfoFromDevice(value);
I get the above mentioned error even if I try to run the Flutter Blue example code. The moment I press the sync and try enabling notifications for a characteristic, the error is thrown.
@qurm I'm new to this and I'm facing the same issue. Can you please tell me what the read() and listen() functions are doing and why I should be using one over the other.
Any suggestions? Same problem in version 0.8.0!
I just hit this bug as well. The only information that I can give that might be helpful:
When I place a breakpoint on the read() call it will successfully complete the read. This suggests that if you attempt to do two different actions, even on different characteristics that it will throw. For example if I'm subscribed(notify) on one characteristic and my code does something to prompt a notification and at the same time requests a read, the BLE request fails.
Outside of some lib level queue, retry or wait to guarantee that you don't do two things at once, I'm not sure how to fix/workaround.
Most helpful comment
you should listen to it.
for(BluetoothService service in services){ values) async{
print("service id is : ${service.uuid}");
if (_matchSmartLockServiceId(service.uuid.toString())) {
var characteristics = service.characteristics;
for (BluetoothCharacteristic c in characteristics) {
print("### ${c.uuid}");
String c_id = c.uuid.toString();
if (_getBlueToothFunctionCode(c_id) == "*"){
await c.setNotifyValue(true);
c.value.listen((value) {
_readInfoFromDevice(value);
});
}else if (_getBlueToothFunctionCode(c_id) == ""){
_sendMsg(c);
}
}
}
_readInfoFromDevice(List
if(values.length>0){
print("value: ${values}");
}
}
_sendMsg(BluetoothCharacteristic c) async{
//do something
}
bool _matchSmartLockServiceId(String serviceId) {
//do your logic
if (serviceId.isEmpty) return false;
if (serviceId.substring(0, 4) == "*") return true;
return false;
}