蓝牙标准的温度service uuid如下
00001809-0000-1000-8000-00805f9b34fb
它包括如下三个character
00002a1c-0000-1000-8000-00805f9b34fb Temperature Measurement
00002a1d-0000-1000-8000-00805f9b34fb Temperature Type
00002a1e-0000-1000-8000-00805f9b34fb Intermediate Temperature
其中第一个character是获取实时温度的character
由于获取温度采用的是indicate方式
需要在获取温度服务后进行如下操作
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
System.out.println(“BluetoothFragment”+"onServicesDiscovered received: " );
BluetoothGattService heartservice = gatt.getService(UUID_HEART_RATE_MEASUREMENTSERVICE);//00001809-0000-1000-8000-00805f9b34fb
List serviceList = gatt.getServices();
for(BluetoothGattService eachservice:serviceList){
System.out.println(“BluetoothFragment”+"onServicesDiscovered received: "+eachservice.getUuid() );
}
if(heartservice==null){
System.out.println("BluetoothFragment"+" not support heart rate : ");
}else{
blegattCharacteristic = heartservice.getCharacteristic(UUID_HEART_RATE_MEASUREMENT);//00002a1c-0000-1000-8000-00805f9b34fb Temperature Measurement
if(gatt.setCharacteristicNotification(blegattCharacteristic,true)){//打开温度服务通知
List<BluetoothGattDescriptor> des = blegattCharacteristic.getDescriptors();
for(BluetoothGattDescriptor descriptor: des){
System.out.println("BluetoothFragment"+" descriptor: "+descriptor.getUuid()+"::"+Arrays.toString(descriptor.getValue()));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);//打开温度值监听
gatt.writeDescriptor(descriptor);
}
}
}
} else {
System.out.println("BluetoothFragment"+ "onServicesDiscovered received: " + status);
}
}
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
broadcastUpdate( null,characteristic);
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
broadcastUpdate(null,characteristic);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
broadcastUpdate(null,characteristic);
}
最后收到温度数据后会进入如下处理,此时读取characteristic数据即可
private void broadcastUpdate(BluetoothGatt gatt,final BluetoothGattCharacteristic characteristic) {
if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {//00002a1c-0000-1000-8000-00805f9b34fb Temperature Measurement
int format2 = BluetoothGattCharacteristic.FORMAT_FLOAT;
System.out.println("BluetoothFragment"+"Heart rate format UINT8."+characteristic.getFloatValue(format2,1)); //获取到温度值为float型
}
}
(乐贴智能体温贴)刚开始连接的时候温度大概7秒左右传输一次,超过七分钟以后每分钟上传,已经对比过官方demo,效果一致。同时该体温贴支持BLE广播发送温度数据,具体参考官方资料