Android BLE readCharacteristic总是返回false

时间:2018-02-06 09:28:02

标签: android bluetooth bluetooth-lowenergy

我正在开发和应用程序,通过蓝牙BLE在自定义设备中进行读写。写作没有问题。我能够建立联系,发现服务和特征。

请注意对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;
}

之后,我尝试拨打readCharacteristic,但始终返回false。我尝试了几种方法:

  • 按一下按钮,然后拨打readCharacteristic
  • 调用onCharacteristicWrite回调中的readCharacteristic(只是为了确保没有待处理的操作)

你知道这里发生了什么吗?

       @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();
    }

0 个答案:

没有答案