2015-03-31 70 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设备。