简体   繁体   English

Android BLE API:未收到GATT回调

[英]Android BLE API: GATT callback not received

I working on an app that connects to a BLE device on a bike. 我正在开发一个与自行车上的BLE设备连接的应用程序。 The app works fine on Android 4.x but on Android 5 we don't get any callback on the writeCharacterstic() call. 该应用程序在Android 4.x上运行良好,但在Android 5上,我们在writeCharacterstic()调用中未获得任何回调。 Any help is very appreciated since we are stuck at the moment! 非常感谢您的帮助,因为我们目前处于停滞状态!

All calls to the gatt seems to return true. 对gatt的所有调用似乎都返回true。 I have made sure that the calls are in the correct order so the GATT server won't return status busy: 我已确保调用顺序正确,因此GATT服务器不会返回忙状态:

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

I've been testing on the following devices: 我一直在以下设备上进行测试:

  • Samsung Galaxy S4 with Android 4.4.2 (working) 具有Android 4.4.2的Samsung Galaxy S4(正在运行)
  • Samsung Galaxy S3 with Android 4.3 (working) 具有Android 4.3的Samsung Galaxy S3(正在运行)
  • LG Nexus 4 with Android 5.0.1 ( not working, no callback on writeCharacteristic() ) 装有Android 5.0.1的LG Nexus 4( 无效,在writeCharacteristic()上没有回调

Code: 码:

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");
    }
}

Logs: 日志:

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()

I understand that it is late answer, but maybe it will help somebody other with such situation. 我知道这是一个迟来的答案,但在这种情况下可能会帮助其他人。 I have the same bug with Android 5 devices - note that you have disconnection event instead of writing characterictic. 我在Android 5设备上有同样的错误-请注意,您有断开连接事件,而不是编写特征记录。 Try to add empty callback: 尝试添加空的回调:

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

it helped me with Android 5 devices. 它帮助我使用了Android 5设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM