-1

我試圖從我的應用中的BLE設備接收數據。我可以將BLE設備與我的應用程序成功連接,並且能夠找到BLE設備提供的服務。onCharacteristicChanged未在Android BLE GATT服務中調用

我可以在特性中寫入數據,並且能夠成功啓用特定服務的通知並返回TRUE。問題是gatt.writeCharacteristic(特性)成功它應該調用onCharacteristicChanged覆蓋方法。但它沒有調用這種方法。 只有我能夠從BLE設備接收數據。

我採取了以下網址

Android BLE

注: 使用的服務,我援引GATT建立連接。

private class BleGattCallback extends BluetoothGattCallback { 
     @Override 
     public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 

      switch (newState) { 
       case BluetoothProfile.STATE_CONNECTED: 

        gatt.discoverServices(); 

        break; 

       case BluetoothProfile.STATE_DISCONNECTED: 
        Log.d(TAG, "BluetoothProfile.STATE_DISCONNECTED"); 
        gatt.close(); 
        break; 

       default: 
        break; 
      } 
     } 


     @Override 
     public void onServicesDiscovered(BluetoothGatt gatt, int status) { 
      Log.d(TAG, "onServicesDiscovered"); 

      if (status == BluetoothGatt.GATT_SUCCESS) { 
       BluetoothGattService service = gatt.getService(SERVICE_UUID); 
       if (service != null) { 

        BluetoothGattCharacteristic characteristic = service.getCharacteristic(READING_UUID); 
        if (characteristic != null) { 
         Log.d(TAG, " Subscribe Characteristic Notification UUID : "+characteristic.getUuid()); 

         gatt.setCharacteristicNotification(characteristic, true); 

         BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CHARACTERISTIC_UUID); 
         descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 

         boolean success = gatt.writeDescriptor(descriptor); 


         Log.d(TAG, "writeDescriptor Status : " + success); 
        } 
       } 
      } 
     } 


     @Override 
     public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { 
      super.onCharacteristicChanged(gatt, characteristic); 

      Toast.makeText(mContext,"onCharacteristicChanged",Toast.LENGTH_LONG).show(); 
      // read Value 
     } 



     @Override 
     public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { 
      super.onDescriptorWrite(gatt, descriptor, status); 


      if (status == BluetoothGatt.GATT_SUCCESS) { 

       BluetoothGattService service = gatt.getService(SERVICE_UUID); 
       if (service != null) { 
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(INDEX_UUID); 
        Log.d(TAG, "onDescriptorWrite success UUID for "+characteristic.getUuid()); 
        if (characteristic != null) { 
         characteristic.setValue(new byte[] {0x03, 0x00}); 
         gatt.writeCharacteristic(characteristic); 
        } 
       } 
      } 
     } 


    } 

回答

0

編號onCharacteristicChanged在向特徵值寫入值後不會調用。 onCharacteristicWrite被調用。

onCharacteristicChanged在收到通知或指示時被調用。

+0

但我已經設置了一個通知字符(READING_UUID),然後只有我寫入字符(INDEX_UUID)。 –

+0

您應該重寫onCharacteristicWrite以等待寫入完成。 – Emil

相關問題