2015-12-04 63 views
1

我CLIENT_CHARACTERISTIC_CONFIG是BluetoothGattDescriptor始終是NULL

public final static UUID CLIENT_CHARACTERISTIC_CONFIG = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); 

我這樣做是爲了改變特點:

  BluetoothGatt mBluetoothGatt = device.connectGatt(this.context, false, mGattCallback); 
      mBluetoothGatt.connect(); 


      BluetoothGattService mCustomService = bleScanner.getBluetoothGatt().getService(UUID.fromString(bleServiceUUID)); 
      if(mCustomService == null){ 
       return; 
      } 

      BluetoothGattCharacteristic mCharacteristic = mCustomService.getCharacteristic(UUID.fromString(bleCharacteristicUUID)); 
      if(mReadCharacteristic == null){ 
       return; 
      } 
      String message = "myMessage"; 
      mCharacteristic.setValue(message.getBytes()); 

      if (!mBluetoothGatt.writeCharacteristic(characteristic)) { 
       Log.e(TAG, "Failed to write characteristic"); 
      } else { 
       Log.e(TAG, "Writed characteristic " + message); 
      } 

characteristic改變後的傳感器,onCharacteristicWrite被稱爲BluetoothGattCallback

@Override 
       public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 

        if (status == BluetoothGatt.GATT_SUCCESS) { 

         if (!mBluetoothGatt.setCharacteristicNotification(characteristic, true)) { 
          DLog.e(TAG, "Notification Setup failed: "+characteristic.getUuid()); 
         } 

         BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG); 
         if (descriptor!=null){ 
          descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 
          mBluetoothGatt.writeDescriptor(descriptor1); 
         } 
        } 
       } 

但是descriptor總是NULL。並且onCharacteristicChanged永遠不會被調用。 爲什麼?並且是我的CLIENT_CHARACTERISTIC_CONFIG權利?

+0

您可以共享這個問題或者您是否以某種方式解決了這個問題? – Harpreet

+0

@Harpreet,請參閱下面的回答 – NickUnuchek

回答

0

,這是因爲一些設備的(我的是三星的Android E5 4.3->更新到5.1.1)是支持BLE但不支持BluetoothGattDescriptor 0x2902(見鏈接)

看到這些都是描述符支持者在設備呼叫該:

for (BluetoothGattDescriptor descriptor:characteristic.getDescriptors()){ 
        Log.e(TAG, "BluetoothGattDescriptor: "+descriptor.getUuid().toString()); 
       } 

[UPDATE] 一些BLE設備不支持Writed characteristics,如iBeacon顯示

+1

有沒有解決這個問題? –