将数据发送到BLE传感器设备

时间:2017-11-24 11:31:15

标签: java android bluetooth bluetooth-lowenergy sensor

我使用谷歌的代码连接到博世XDK传感器: https://github.com/googlesamples/android-BluetoothLeGatt

连接正常,但是为了让传感器开始流式传输,我需要向它发送一个命令(0x7374617274)。我有点失去了如何做到这一点。是否可以使用示例应用程序?

public boolean writeCharacteristic(){
    //check mBluetoothGatt is available
    if (mBluetoothGatt == null) {
        Log.e(TAG, "lost connection");
        return false;
    }

    BluetoothGattService Service = mBluetoothGatt.getService(UUID.fromString("b9e875c0-1cfa-11e6-b797-0002a5d5c51b"));
    if (Service == null) {
        Log.e(TAG, "service not found!");
        return false;
    }
    BluetoothGattCharacteristic charac = Service
            .getCharacteristic(UUID.fromString("1ed9e2c0-266f-11eb-850b-0002a5d5c51b"));
    if (charac == null) {
        Log.e(TAG, "char not found!");
        return false;
    }

    byte[] value = hexStringToByteArray("0x7374617274");
    charac.setValue(value);
    boolean status = mBluetoothGatt.writeCharacteristic(charac);
    return status;
 }

0 个答案:

没有答案