Android获取蓝牙温度贴数据(乐贴智能体温贴)

蓝牙标准的温度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广播发送温度数据,具体参考官方资料

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这里给出一个简单的 Android BLE 获取体温数据蓝牙app代码示例。 首先,需要在项目的 `AndroidManifest.xml` 文件中添加 BLE 权限: ```xml <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> ``` 然后,在 `MainActivity.java` 中,进行 BLE 的初始化、连接和数据读取: ```java public class MainActivity extends AppCompatActivity { private BluetoothManager bluetoothManager; private BluetoothAdapter bluetoothAdapter; private BluetoothGatt bluetoothGatt; private BluetoothGattCharacteristic temperatureCharacteristic; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); bluetoothAdapter = bluetoothManager.getAdapter(); BluetoothDevice device = bluetoothAdapter.getRemoteDevice("00:11:22:33:44:55"); bluetoothGatt = device.connectGatt(this, false, gattCallback); } private BluetoothGattCallback gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { gatt.discoverServices(); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { // 断开连接 } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { BluetoothGattService service = gatt.getService(UUID.fromString("00001809-0000-1000-8000-00805f9b34fb")); temperatureCharacteristic = service.getCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb")); gatt.setCharacteristicNotification(temperatureCharacteristic, true); } } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { if (characteristic == temperatureCharacteristic) { // 解析温度数据 byte[] data = characteristic.getValue(); float temperature = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).getFloat(); Log.d(TAG, "Temperature: " + temperature); } } }; } ``` 在上面的代码中,我们首先通过 `BluetoothManager` 和 `BluetoothAdapter` 初始化 BLE,连接指定的设备,然后在 GATT 回调函数中,发现可用服务和特征,设置特征的通知,最后在特征值改变时读取并解析温度数据。 需要注意的是,这只是一个简单的示例,实际的实现可能会更加复杂,需要根据具体的硬件设计和数据格式进行适当的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值