2015-03-31 81 views
1

我正在研究連接到自行車上BLE設備的應用程序。該應用在Android 4.x上運行良好,但在Android 5上,我們沒有在writeCharacterstic()調用上獲得任何回調。任何幫助非常感謝,因爲我們現在卡住了!Android BLE API:未收到GATT回撥

所有對gatt的調用似乎都返回true。我已經確定的調用是正確的順序,因此GATT服務器將不返回狀態忙道:

device.connectGatt() 
onConnectionStateChange() -> gatt.discoverServices() 
onServicesDiscovered() -> gatt.writeDescriptor() 
onDescriptorWrite() -> sendRequest() 
sendRequest() -> gatt.writeCharacteristic() 

我一直對以下設備測試:

  • 三星Galaxy S4與是Android 4.4.2(工作)
  • 三星Galaxy S3搭載Android 4.3(工作)
  • LG的Nexus 4採用Android 5.0.1(不工作,沒有任何回調的writeCharacteristic()

代碼:

private BluetoothGattCallback callback = new BluetoothGattCallback() { 
    @Override 
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 
     super.onConnectionStateChange(gatt, status, newState); 

     if (newState == BluetoothGatt.STATE_CONNECTED) { 
      Log.i(TAG, "onConnectionStateChange() - STATE_CONNECTED"); 
      boolean discoverServicesOk = gatt.discoverServices(); 
     } else if (newState == BluetoothGatt.STATE_DISCONNECTED) { 
      Log.i(TAG, "onConnectionStateChange() - STATE_DISCONNECTED"); 
     } 
    } 

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

     BluetoothGattService service = gatt.getService(UART_UUID); 
     characteristic = service.getCharacteristic(TX_UUID); 

     boolean result = gatt.setCharacteristicNotification(characteristic, true); 
     Log.i(TAG, "onServicesDiscovered() - setCharacteristicNotification " + result); 

     if (characteristic.getDescriptor(CLIENT_UUID) != null) { 
      BluetoothGattDescriptor desc = characteristic.getDescriptor(CLIENT_UUID); 
      result = desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 
      Log.i(TAG, "onServicesDiscovered() - ENABLE_NOTIFICATION_VALUE " + result); 
      result = desc.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); 
      Log.i(TAG, "onServicesDiscovered() - ENABLE_INDICATION_VALUE " + result); 
      result = gatt.writeDescriptor(desc); 
      Log.i(TAG, "onServicesDiscovered() - writeDescriptor " + result); 
     } 

     connectionState = STATE_CONNECTED; 
    } 

    @Override 
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { 
     super.onDescriptorWrite(gatt, descriptor, status); 
     mCurrentRequest = getNextRequest(); 
     Log.i(TAG, "onDescriptorWrite() - sending request, type: " + mCurrentRequest.getType()); 
     sendRequest(mCurrentRequest); 
    } 

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

     // DON'T GET HERE ON LOLLIPOP 
     Log.d(TAG, "onCharacteristicChanged() - size: " + characteristic.getValue().length + ", type: " + mCurrentRequest.getType()); 
    } 
} 

public void sendRequest(AsciiRequest asciiRequest) { 
    start = System.currentTimeMillis(); 
    if (characteristic != null && connectionState == STATE_CONNECTED) { 
     Log.i(TAG, "sendRequest() - sending request, type: " + mCurrentRequest.getType()); 
     characteristic.setValue(asciiRequest.getData()); 
     characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT); 
     boolean result = gatt.writeCharacteristic(characteristic); 
     Log.i(TAG, "sendRequest() - result writeCharacteristic: " + result); 
    } else { 
     Log.e(TAG, "sendRequest() - not connected"); 
    } 
} 

日誌:

03-31 14:08:28.614: I/BikeActivity(30045): onResume() - not connected yet, trying to connect 
03-31 14:08:28.616: I/BikeActivity(30045): doConnect() - connecting to device Bike 2 
03-31 14:08:28.634: I/BikeActivity(30045): doConnect() - connected to device Bike 2 
03-31 14:08:29.137: I/BikeActivity(30045): onConnectionStateChange() - STATE_CONNECTED 
03-31 14:08:29.151: I/BikeActivity(30045): onConnectionStateChange() - connected to device Bike 2 
03-31 14:08:29.164: I/BikeActivity(30045): onServicesDiscovered() 
03-31 14:08:29.170: I/BikeActivity(30045): onServicesDiscovered() - setCharacteristicNotification true 
03-31 14:08:29.170: I/BikeActivity(30045): onServicesDiscovered() - ENABLE_NOTIFICATION_VALUE true 
03-31 14:08:29.170: I/BikeActivity(30045): onServicesDiscovered() - ENABLE_INDICATION_VALUE true 
03-31 14:08:29.172: I/BikeActivity(30045): onServicesDiscovered() - writeDescriptor true 
03-31 14:08:29.173: I/BikeActivity(30045): onDescriptorWrite() - sending request, type: 25 
03-31 14:08:29.182: I/BikeActivity(30045): sendRequest() - sending request, type: 25 
03-31 14:08:29.186: I/BikeActivity(30045): sendRequest() - result writeCharacteristic: true 
03-31 14:08:34.182: I/BikeActivity(30045): doDisconnect() - disconnecting from device Bike 2 
03-31 14:08:34.233: I/BikeActivity(30045): onConnectionStateChange() - STATE_DISCONNECTED 
03-31 14:08:34.233: I/BikeActivity(30045): onConnectionStateChange() - disconnected 
03-31 14:08:34.263: I/BikeActivity(30045): doDisconnect() - toastText: No response from bike. 
03-31 14:08:34.318: I/BikeActivity(30045): onPause() 
03-31 14:08:34.712: I/BikeActivity(30045): onDestroy() 

回答

1

我明白,這是遲到的答案,但也許它會幫助其他有這種情況人。 我有與Android 5設備相同的錯誤 - 請注意,您已斷開連接事件而不是寫作characterictic。 嘗試添加空回調:

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

它幫助我使用Android 5設備。