2016-06-09 124 views
0

我正在開發一個包含Bluegiga BLE113模塊和Android應用程序的項目。在Bluegiga模塊上,我設置了幾個特性。對於一個特性,我定義了一個描述符來激活clie(在我的情況下,客戶端是Android應用程序)。在BLE113模塊的特徵定義是這樣的:Android BLE onCharacteristicChanged()未調用

<characteristic uuid="dcfa2671-974d-4e4a-96cd-6221afeabb62"  id="ez1_flow_data_out"> 
    <!-- org.bluetooth.descriptor.gatt.characteristic_user_description --> 
    <description>Data to transmit to Android device</description> 
    <properties write="true" read="true" /> 
    <value variable_length="true" length="255" type="hex" /> 
    <!-- org.bluetooth.descriptor.gatt.client_characteristic_configuration --> 
    <descriptor uuid="2902"> 
    <properties write="true" read="true" const="false" /> 
    <!-- Bit 0 = 1: Notifications enabled --> 
    <value type="hex">0001</value> 
    </descriptor> 


    </characteristic> 

在Android方面,我根據Android的藍牙低能量引導建立onServicesDiscovered(...)回調內部通知:

characteristic=gatt.getService(BLEuuids.DATAFLOW_SERVICE).getCharacteristic(BLEuuids.DATAFLOW_OUT_CHARACT); 
//Enable local notifications 
gatt.setCharacteristicNotification(characteristic, true); 
//Enabled remote notifications 
BluetoothGattDescriptor desc = characteristic.getDescriptor(BLEuuids.DATAFLOW_OUT_DESCRIPT); 
boolean test; 
test = desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); // return value = true 
test = gatt.readDescriptor(desc); // return value = true 
test = gatt.writeDescriptor(desc); // return value = true 

但是,如果我使用Bluegiga模塊的串行接口更改DATAFLOW_OUT_CHARACT特性UUID的值,則在我的Android應用程序上不會觸發onCharacteristicChanged(...)的預期回調。

我是否正確設置了描述符,還是在我的Android代碼中失敗?

回答

0

現在使通知回調onCharactersticsChanged()和設置屬性ENABLE_NOTIFICATION_VALUE和ENABLE_INDICATION_VALUE

Log.d(TAG, "setCharacteristicNotification"); 
     if (mBluetoothAdapter == null || customBluetoothGatt == null) { 
      Log.d(TAG, "BluetoothAdapter not initialized"); 
      return; 
     }cx 
     customBluetoothGatt.setCharacteristicNotification(characteristic, enabled); 

     BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG)); 
     descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 
     descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); 
     boolean success = customBluetoothGatt.writeDescriptor(descriptor); 
相關問題