没有使用BLE112调用onCharacteristicChanged()

时间:2014-08-14 06:13:49

标签: android bluetooth bluetooth-lowenergy

我正在使用BLE112设备而我的回调方法onCharacteristicChanged(...)未在我的Android应用上触发。

在我的gatt.xml文件中,我定义了这项服务:

    <service uuid="4300" advertise="true" id="entry_service">
        <description>Entry_Service</description>

       <characteristic uuid="4301" id="unlock_state">
        <description>Unlock_State</description>
            <properties read="true" write="true" notify="true"/>
            <value length="1">0</value>
                <descriptor uuid="4381">
                    <properties read="true" write="true"/>
                    <value length="1">0</value>
                </descriptor>
        </characteristic>
    </service>ode here

我正在尝试使用此代码启用通知:

BluetoothGattService service = ble112Device.getBluetoothGatt().getService(uuidService);
BluetoothGattCharacteristic characteristic =  service.getCharacteristic(uuidChar);

if( !ble112Device.getBluetoothGatt().setCharacteristicNotification(characteristic, true)) // returns true
    Log.d("NOTIFICATION", "Setup failed.");

BluetoothGattDescriptor descriptor = characteristic.getDescriptor(uuidDescr); 

if(!descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)) // returns true
    Log.d("DESCRIPTOR", "error");

else if(!mBluetoothLeService.writeDescriptor(ble112Device, descriptor)) // returns true
    Log.d("DESCRIPTOR", "write error");

在我的Logchat中,我收到了写入描述符等的确认,但onCharacteristicChanged(...)从未被调用过。 有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

在您的GATT.XML文件中更改属性的顺序。

<properties notify="true" read="true" write="true" />

答案 1 :(得分:0)

我解决了这个问题。 获取描述符时,必须使用UUID:0x2902。

BluetoothGattDescriptor descriptor = characteristic.getDescriptor(uuidDescr); //uuidDescr must be 0x2902

您不必在gatt.xml中编写自己的描述符。 只需获取并编写您想要通知的特征UUID 0x2902的描述符。

之后,onCharacteristicChanged()正常解雇。

UUID 2902的描述符称为Client Characteristic Configuration descriptor

相关问题