我使用此代码并利用 flutter_reactive_ble 包从我的设备读取血压(收缩压、舒张压)(以 mmHg 为单位)和脉搏率(以 bpm 为单位),但我正在努力弄清楚如何做到这一点,在我的控制台中,我收到:
D/BluetoothGatt(21288):setCharacteristicNotification() - uuid:00002a35-0000-1000-8000-00805f9b34fb 启用:true I/flutter (21288):收到血压测量的原始数据:[222, 136, 244, 32, 243, 255, 7, 228, 7, 1, 19, 22, 55, 0, 188, 242, 1, 68, 1]
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('BLE Scanner'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: _scanForDevices,
child: Text('Scan for Devices'),
),
SizedBox(height: 20),
Text('Discovered Devices:'),
Expanded(
child: ListView.builder(
itemCount: devices.length,
itemBuilder: (context, index) {
final device = devices[index];
return ListTile(
title: Text(device.name ?? 'Unknown'),
subtitle: Text(device.id),
onTap: () {
_connectToDevice(device.id);
_subscribeToBloodPressureMeasurement(device.id);
},
);
},
),
),
],
),
),
);
}
void _scanForDevices() {
flutterReactiveBle.scanForDevices(
withServices: [],
scanMode: ScanMode.lowLatency,
).listen(
(device) {
final existingDeviceIndex =
devices.indexWhere((d) => d.id == device.id);
if (existingDeviceIndex != -1) {
setState(() {
devices[existingDeviceIndex] = device;
});
} else {
setState(() {
devices.add(device);
});
}
},
onError: (error) {
print('Error: $error');
},
);
}
void _connectToDevice(String deviceId) {
flutterReactiveBle
.connectToDevice(
id: deviceId,
servicesWithCharacteristicsToDiscover: {
Uuid.parse('00001810-0000-1000-8000-00805f9b34fb'): [ // Caractéristique de mesure de la pression artérielle Uuid.parse('00002a35-0000-1000-8000-00805f9b34fb'), // Caractéristique des fonctionnalités de la pression artérielle Uuid.parse('00002a49-0000-1000-8000-00805f9b34fb'), ],
},
connectionTimeout: const Duration(seconds: 2),
)
.listen(
(connectionState) {
// Gérez les mises à jour de l'état de connexion
if (connectionState == DeviceConnectionState.connected) {
print("Connected successfully to device: $deviceId");
_subscribeToBloodPressureMeasurement(deviceId);
}
},
onError: (error) {
print('Error connecting to device: $error');
// Gérez les erreurs éventuelles
},
);
}
void _subscribeToBloodPressureMeasurement(String deviceId) {
final bloodPressureCharacteristic = QualifiedCharacteristic(
characteristicId: Uuid.parse('00002a35-0000-1000-8000-00805f9b34fb'),
deviceId: deviceId,
serviceId: Uuid.parse('00001810-0000-1000-8000-00805f9b34fb'),
);
// Souscrire à la caractéristique de mesure de la pression artérielle
flutterReactiveBle
.subscribeToCharacteristic(bloodPressureCharacteristic)
.listen(
(data) {
print('Received raw data for blood pressure measurement: $data');
// Traitez les données de notification ici
},
onError: (error) {
print('Error subscribing to blood pressure measurement: $error');
// Gérez les erreurs éventuelles lors de l'abonnement
},
);
}
}
从服务开始:00001810-0000-1000-8000-00805f9b34fb。这是16位UUID模板,所以你关心的实际数字是0x1810。
规格页面并拉出最新版本。 阅读本文档。说真的,人们因为不阅读文档而迷失了方向。
也就是说,这个细节很糟糕。太可怕了。它没有解释格式,但它确实告诉您服务如何工作。
这样,我们查找特征 UUID 0x2a35,这是血压测量值。就像我说的,这个服务文档很糟糕,并且没有很好地解释它,但它在
the YAML标志(1字节)
压力(毫米汞柱)
这里的格式medfloat16,也叫sfloat,是16位浮点数。有关计算示例,请参阅
如何使用 CoreBluetooth 从 1822 脉搏血氧计的蓝牙 LE 数据中提取 SFLOAT 值。32, 243 -> 80255, 7 -> NaN(所以这实际上不可用)
date_time
结构)<<8 + 228 because it's little endian)
用户ID(1字节)1
0001 0100 0100
脉搏不规则