1 year ago
#207902
Abid Hussain
Flutter Blue characteristic length is limited
I am developing an app in flutter using flutter blue plugin where i am getting the data from a customized bluetooth low energy controller all the neccessary data is in characteristic of the property of notify listner. when i listen the data its 16 bytes evertime but some sensors have data more than 16 bytes so its breaks the array into multiple arrays one array at a time. like
[187(start), 17, 8, 132, 86, 34, 146, 255, 255, 255, 255, 255, 255, 255, 255, 255]
[4, 255, 255, 143, 204(end-checksum), 187(start again), 17, 8, 168, 77, 34, 146, 114, 0, 19, 1]
[251, 0, 235, 70, 65, 5, 0, 0, 169, 204]
i tried to request with larger mtu with status 0 like
widget.device.requestMtu(512);
Controller's maximum mtu size is 247 bytes but by default its showing 20 bytes.
but it gives the same result but i want to get array from 187 to 204 everytime when characteristic listner is called with no broken arrays.and data is not limited its keep updating. console :
I/flutter ( 505): full value [187, 12, 9, 100, 90, 80, 70, 40, 1, 251,
8, 16, 24, 85, 6, 204]
I/flutter ( 505): full value [187, 5, 16, 15, 58, 92, 100, 25, 204]
I/flutter ( 505): full value [187, 7, 25, 1, 14, 20, 10, 0, 0, 70,
204]
I/flutter ( 505): full value [187, 6, 26, 5, 3, 5, 0, 0, 39, 204]
I/flutter ( 505): full value [187, 10, 27, 1, 10, 15, 50, 55, 80, 85,
130,
135, 76, 204]
I/flutter ( 505): full value [187, 17, 8, 0, 0, 0, 0, 253, 0, 19, 1,
251,
0, 233, 74, 16]
I/flutter ( 505): full value [0, 0, 0, 87, 204, 187, 13, 1, 84, 77, 85,
95,
84, 69, 83, 84]
code i tried :
await widget.device.requestMtu(512);
await Future.delayed(Duration(seconds: 5));
final mtu=await widget.device.mtu.first;
print(mtu);
List<BluetoothService> servicevalue = await widget.device
.discoverServices();
print("sssssssssssss+${servicevalue}");
for (BluetoothService service in servicevalue) {
for (BluetoothCharacteristic c in service.characteristics) {
if (c.uuid.toString() == "6e400003-b5a3-f393-e0a9-
e50e24dcca9e") {
print("cccccccccccccccccccccccccccc+${c}");
c.value.listen((value) {
print("full value ${value}");
if (value.isNotEmpty) {
////////////////////////////////Door///////////////////////////////////
if (value?.elementAt(0) == 187 &&
value?.elementAt(2) == 9 &&
value?.elementAt(8) == 1) {
setState(() { doortext = "OPEN";
doorcolor = Colors.redAccent;});
}
if (value?.elementAt(0) == 187 &&
value?.elementAt(2) == 9 &&
value?.elementAt(8) == 0) {
setState(() {
doortext = "CLOSED";
doorcolor = Colors.green;
});
}
///////////////////////BRAKE////////////////////////////////////////
if (value?.elementAt(0) == 187 &&
value?.elementAt(2) == 27 &&
value?.elementAt(3) == 0) {
setState(() {
pbrake = "RELEASED";
brakecolor = Colors.black;
});
}
if (value?.elementAt(0) == 187 &&
value?.elementAt(2) == 27 &&
value?.elementAt(3) == 1) {
setState(() {
pbrake = "ENGAGED";
brakecolor = Colors.redAccent;
});
}
if (value?.elementAt(0) == 187 &&
value?.elementAt(2) == 27 &&
value?.elementAt(3) == 255) {
setState(() {
pbrake = "-----";
brakecolor = Colors.black;
});
}
if (value?.elementAt(0) == 187 &&
value?.elementAt(2) == 26) {
List<int> chunk = <int>[];
chunk.addAll(value.sublist(3, 7));
if (chunk.contains(1) || chunk.contains(2) ||
chunk.contains(4) || chunk.contains(5)) {
setState(() {
lamp = "CAUTION";
lampcolor = Colors.redAccent;
});
}
else {
if (chunk.contains(0)) {
setState(() {
lamp = "-----";
lampcolor = Colors.black;
});
}
else {
if (chunk.contains(3)) {
setState(() {
lamp = "GOOD";
lampcolor = Colors.green;
});
}
else {
setState(() {
lamp = "RECONNECT";
lampcolor = Colors.black;
});
}
}
}
}
if (value?.elementAt(0) == 187 &&
value?.elementAt(2) == 9) {
List<int> tempchunk = <int>[];
tempchunk.addAll(value.sublist(3, 8));
double total = 0;
for (int i = 0; i < tempchunk.length; i++) {
if (tempchunk[i] == 255 || tempchunk[i] == 254) {
tempchunk[i] = 0;
}
else {
total += tempchunk[i];
}
}
if (total != 0) {
double per = (total / 500) * 100;
setState(() {
cargo = per.toString() + "%";
});
}
else {
setState(() {
cargo = "---%";
});
}
}
}
});
await c.setNotifyValue(true);
}
}
}
flutter
bluetooth-lowenergy
gatt
characteristics
0 Answers
Your Answer