flutter - 无法在 Flutter 中使用 flutter_blue 读取多个服务的数据
问题描述
我在我的颤振应用程序中使用颤振蓝包,但问题是我无法读取第二个服务的数据。虽然我得到了第一服务特征的价值。
我从 BASIC Services UUID 获取数据,但不是从 PV Services UUID 获取数据。
但是如果我睡了大约 20~30 秒,那么它就可以工作了。分享代码:
discoverServices() async {
List<BluetoothService> services =await widget.device.discoverServices();
EasyLoading.show(status: 'Fetching data from device...');
Future.delayed(Duration(seconds: 7), () async {
Logger().d("Sensor Screen: Waiting for services to be read");
for(BluetoothService service in services){
String formattedServiceUUID =
"0x${service.uuid.toString().toUpperCase().substring(4, 8)}";
if(formattedServiceUUID==ServicesAndCharacteristics.PV_SERVICES_UUID){
Logger().d("Sensor Screen: PV_SERVICES_UUID ==FOUND==");
for(BluetoothCharacteristic characteristic in service.characteristics){
String formattedCharacteristic = "0x${characteristic.uuid.toString().toUpperCase().substring(4, 8)}";
if (formattedCharacteristic == ServicesAndCharacteristics.CHAR_2A11) {
if(characteristic.isNotifying==false){
await characteristic.setNotifyValue(true);
}
await characteristic.read();
var value=characteristic.value;
Logger().d("Sensor Screen: Value of VV1 value is-"+value.toString());
CHAR_2A11_STREAM = characteristic.value;
setState(() {
if(isReady==false){
isReady = true;
EasyLoading.dismiss();
}
});
}
}
}
if(formattedServiceUUID==ServicesAndCharacteristics.BASIC_SERVICES_UUID){
Logger().d("Sensor Screen: CHAT_SERVICE_UUID ==FOUND==");
for(BluetoothCharacteristic characteristic in service.characteristics){
String formattedCharacteristic = "0x${characteristic.uuid.toString().toUpperCase().substring(4, 8)}";
Logger().d("Sensor Screen: CHAR FOUND-> "+formattedCharacteristic);
/* if (formattedCharacteristic == BATTERY_CHAR_UUID) {
Logger().d("Sensor Screen: BATTERY_CHAR_UUID ==FOUND==");
if(characteristic.properties.notify==true){
if(characteristic.isNotifying==false){
await characteristic.setNotifyValue(true);
}
}
await characteristic.read();
CHAR_2A02_STREAM = characteristic.value;
setState(() {
if(isReady==false){
isReady = true;
EasyLoading.dismiss();
}
});
}
if (formattedCharacteristic == CHAT_CHAR_UUID) {
Logger().d("Sensor Screen: CHAT_CHAR_UUID ==FOUND==");
if(characteristic.properties.notify==true){
if(characteristic.isNotifying==false){
await characteristic.setNotifyValue(true);
}
}
await characteristic.read();
CHAR_2A0A_STREAM = characteristic.value;
setState(() {
if(isReady==false){
isReady = true;
EasyLoading.dismiss();
}
});
}*/
if (formattedCharacteristic == ServicesAndCharacteristics.CHAR_2A03) {
if(characteristic.properties.notify==true){
Logger().d("Sensor Screen: Notify property FOUND For -> "+formattedCharacteristic);
if(characteristic.isNotifying==false){
await characteristic.setNotifyValue(true);
}
}
await characteristic.read();
CHAR_2A03_STREAM = characteristic.value;
setState(() {
if(isReady==false){
isReady = true;
EasyLoading.dismiss();
}
});
}
if (formattedCharacteristic == ServicesAndCharacteristics.CHAR_2A0A) {
if(characteristic.properties.notify==true){
Logger().d("Sensor Screen: Notify property FOUND For -> "+formattedCharacteristic);
if(characteristic.isNotifying==false){
await characteristic.setNotifyValue(true);
}
}
await characteristic.read();
CHAR_2A0A_STREAM = characteristic.value;
Logger().d("Sensor Screen: NOW CHANGE STATE OF SCREEN");
setState(() {
if(isReady==false){
isReady = true;
EasyLoading.dismiss();
}
});
}
if (formattedCharacteristic == ServicesAndCharacteristics.CHAR_2A04) {
if(characteristic.properties.notify==true){
Logger().d("Sensor Screen: Notify property FOUND For -> "+formattedCharacteristic);
if(characteristic.isNotifying==false){
await characteristic.setNotifyValue(true);
}
}
await characteristic.read();
CHAR_2A04_STREAM = characteristic.value;
setState(() {
if(isReady==false){
isReady = true;
EasyLoading.dismiss();
}
});
}
if (formattedCharacteristic == ServicesAndCharacteristics.CHAR_2A02) {
if(characteristic.properties.notify==true){
Logger().d("Sensor Screen: Notify property FOUND For -> "+formattedCharacteristic);
if(characteristic.isNotifying==false){
await characteristic.setNotifyValue(true);
}
}
await characteristic.read();
CHAR_2A02_STREAM = characteristic.value;
setState(() {
if(isReady==false){
isReady = true;
EasyLoading.dismiss();
}
});
}
/*if (formattedCharacteristic == ServicesAndCharacteristics.CHAR_2A11) {
if(characteristic.isNotifying==false){
await characteristic.setNotifyValue(true);
}
await characteristic.read();
CHAR_2A11_STREAM = characteristic.value;
setState(() {
if(isReady==false){
isReady = true;
EasyLoading.dismiss();
}
});
}*/
}
}
}
});
}
解决方案
推荐阅读
- html - 如何在没有伪之前/之后和/或定位的 div 的情况下在 CSS 中设置背景图像不透明度
- node.js - zsh - sh:节点:找不到命令
- python - 如何在 python 中找到我的 PCA 图中的数据点?
- mysql - 如何使用 mysql IN 语句获取多行
- mysql - 如何从表格中删除括号、引号?
- batch-file - 如何使用 cmd 从文本文件中插入密码?
- matlab - 重写脚本并将“内核”分布图拟合到直方图中
- apache-camel - Camel:是否可以在给定时间使用计时器组件来安排日常任务?
- angular - __importDefault 未定义 Ionic
- linux - tmux pin 以避免滚动