其他分享
首页 > 其他分享> > 如何接收低功耗蓝牙消息

如何接收低功耗蓝牙消息

作者:互联网



private BluetoothGattCharacteristic mNotifyCharacteristic;


BluetoothGattService service = gatt.getService(UUID.fromString(serviceUuid));
                mNotifyCharacteristic = service.getCharacteristic(UUID.fromString(characterUuid));

                if (mNotifyCharacteristic != null)
                {
                    //使能Notify
                    setCharacteristicNotification(mNotifyCharacteristic, true);
                }



public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
            boolean enabled)
    {
        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

        // This is specific to BLE SPP Notify.
        // if (characterUuid.equals(characteristic.getUuid()))
        {
            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                    UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG));
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            mBluetoothGatt.writeDescriptor(descriptor);
        }
    }

1、连接成功后

2、onServicesDiscovered回调中发现服务后,调用setCharacteristicNotification

3、接收通知是在onCharacteristicChanged

标签:UUID,mNotifyCharacteristic,characteristic,低功耗,蓝牙,descriptor,fromString,接收,setCha
来源: https://blog.csdn.net/qwildwolf/article/details/120079129