Android BLE readCharacteristic returns always false

隐身守侯 提交于 2019-12-08 06:15:16

问题


I am developing and application to read and write in a custom device through Bluetooth BLE. There is no problem for writing. I am able to establish a connections, discover the service and the characteristics.

Note the call to enableRXNotification

public void startReceiving() {
    boolean readOk = gatt.readCharacteristic(rx);
    if (!readOk) {
        Log.d("ANUBEBT", "ERROR INITIATING READ CHARACTERISTIC");
    }
}

public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        super.onServicesDiscovered(gatt, status);
        // Notify connection failure if service discovery failed.
        if (status == BluetoothGatt.GATT_FAILURE) {
            connectFailure();
            return;
        }

        // Save reference to each UART characteristic.
        tx = gatt.getService(SERIAL_SERVICE_UUID).getCharacteristic(TX_CHAR_UUID);
        rx = gatt.getService(SERIAL_SERVICE_UUID).getCharacteristic(RX_CHAR_UUID);

        enableRXNotification();

        // Notify of connection completion.
        notifyOnConnected(context);
    }

public boolean enableRXNotification() {
    if (gatt == null)   return false;

    BluetoothGattService SerialService = gatt.getService(SERIAL_SERVICE_UUID);
    if (SerialService == null)  return false;

    BluetoothGattCharacteristic RxChar = SerialService.getCharacteristic(RX_CHAR_UUID);
    if (RxChar == null) {
        Log.d("ANUBEBT", "Error getting rx characteristic");
        connectFailure();
        return false;
    }

    if (!setCharacteristicNotification(RxChar, true)) {
        Log.d("ANUBEBT", "Error setting rx notification");

        connectFailure();

        return false;
    }

    return true;
}

After that, I have tried to call readCharacteristic but always returns false. I have tried several alternatives:

  • Press a button and call to readCharacteristic
  • call to readCharacteristic in the onCharacteristicWrite callback (just to make sure there is no pending operations)

Do you know what's going on here?

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

        Log.d("ANUBEBT", "onCharacteristicWrite " + status);
        if (status == BluetoothGatt.GATT_SUCCESS) {
            notifyOnCommunicationError(characteristic.getStringValue(0).length(), characteristic.getStringValue(0));
        }
        writeInProgress = false;
        startReceiving();
    }

来源:https://stackoverflow.com/questions/48639573/android-ble-readcharacteristic-returns-always-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!