【问题标题】:Is there a way to write just a byte in BLE characteristic?有没有办法在BLE特性中只写一个字节?
【发布时间】:2019-08-06 15:33:45
【问题描述】:

我是安卓新手。我的 BLE 设备只需要一个字节(标志)。我尝试在字节数组中只写入一个字节,但它永远不会起作用。在将数组写入另一个需要 2 个字节的特征时。有没有办法解决我的问题,还是我必须要求更改设备代码?

我以this 项目为例

public void writeCustomCharacteristicStart() {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        /*check if the service is available on the device*/
        BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("00001813-0000-1000-8000-00805f9b34fb"));
        if(mCustomService == null){
            Log.w(TAG, "Custom BLE Service not found");
            return;
        }
        /*get the read characteristic from the service*/
        byte pom= 1;
        byte[] data= new byte[1];
        data[0]=pom;
        BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("00002a4e-0000-1000-8000-00805f9b34fb"));

        if((mWriteCharacteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE)>0 ) {
            Log.w(TAG,"Writing started");
            mWriteCharacteristic.setValue(data)
        }
        if(!mBluetoothGatt.writeCharacteristic(mWriteCharacteristic)){
            Log.w(TAG, "Failed to write characteristic");
        }
        Log.w(TAG,"Writing ending");
    }
08-06 11:59:57.700 29279-29279/com.example.devicescanactivity E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.devicescanactivity, PID: 29279
    java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:4084)
        at android.view.View.performClick(View.java:4858)
        at android.view.View$PerformClick.run(View.java:20167)
        at android.os.Handler.handleCallback(Handler.java:815)
        at android.os.Handler.dispatchMessage(Handler.java:104)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5931)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at android.view.View$1.onClick(View.java:4079)
        at android.view.View.performClick(View.java:4858) 
        at android.view.View$PerformClick.run(View.java:20167) 
        at android.os.Handler.handleCallback(Handler.java:815) 
        at android.os.Handler.dispatchMessage(Handler.java:104) 
        at android.os.Looper.loop(Looper.java:194) 
        at android.app.ActivityThread.main(ActivityThread.java:5931) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:987) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782) 
     Caused by: java.lang.SecurityException: Need BLUETOOTH_PRIVILEGED permission: Neither user 10097 nor current process has android.permission.BLUETOOTH_PRIVILEGED.
        at android.os.Parcel.readException(Parcel.java:1549)
        at android.os.Parcel.readException(Parcel.java:1502)
        at android.bluetooth.IBluetoothGatt$Stub$Proxy.writeCharacteristic(IBluetoothGatt.java:1003)
        at android.bluetooth.BluetoothGatt.writeCharacteristic(BluetoothGatt.java:1029)
        at com.example.devicescanactivity.BluetoothLeService.writeCustomCharacteristic(BluetoothLeService.java:377)
        at com.example.devicescanactivity.DeviceControlActivity.onClickWrite(DeviceControlActivity.java:332)

这是错误,我没有收到 BLUETOOTH_PRIVILEGED 错误,但是当我写入接受 2 个字节的特征时,我没有收到任何错误。

【问题讨论】:

  • 您能否在问题中包含您写入字节的代码?
  • @zanerock 是对的,您应该发布您的代码,以便我们了解发生了什么。否则很难帮你调试。你可能只是做错了一些小事。有了这个,我知道这是可能的,而且您不需要更改设备代码。
  • 如果出错,也发布错误

标签: java android bluetooth-lowenergy


【解决方案1】:

首先,要在您的应用中使用蓝牙,您需要在您的应用清单中添加以下权限:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

可以将 1 字节值写入 BLE 特性,前提是设备需要 1 字节值。

如果存在长度问题,您应该会在 onCharacteristicWrite() 回调中看到错误:

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

        if (status == BluetoothGatt.GATT_SUCCESS) {
            Log.i("TAG", "onCharacteristicWrite() status: " + status + "  - UUID: " + characteristic.getUuid());
        } else if (status == BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH)  {
            Log.e("TAG", "onCharacteristicWrite() A write operation failed due to invalid attribute length");
        } else {
            Log.e("TAG", "onCharacteristicWrite() A write operation failed. Status = " + status);
        }
}

另外,我会将 writeCharacteristic() 调用移动到您对 PROPERTY_WRITE 的检查中,以确保可以完成写入操作:

if((mWriteCharacteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0 ) {
    Log.i(TAG,"Writing started");
    mWriteCharacteristic.setValue(data)
    if(!mBluetoothGatt.writeCharacteristic(mWriteCharacteristic)){
        Log.e(TAG, "Failed to write characteristic");
    }
}

【讨论】:

  • 感谢您的帮助。设备需要一个字节,但我只知道如何发送字节数组。
  • 然后发送一个 1 字节数组。您在 onCharacteristicWrite() 回调中看到什么日志?对于信息,当写入 BLE 特性时,我们写入字节数组。查看 Android 的 BluetoothGattCharacteristic.setValue(),它需要一个字节数组作为参数。
  • 在写入之前会出错,特别是在 writeCharacteristic 上。整个应用重启
  • 您是否已将权限添加到清单中,正如我的回答中指出的那样?
  • 该权限只能为系统特权应用添加。
【解决方案2】:

问题不在于我在特性中写了什么,而在于我使用了什么特性。我使用了 HID 控制点,在计算机上测试时,nRF Connect 应用程序在我的设备上运行良好,但由于其预定义的状态,它无法在 android 上运行。我们刚刚将特征更改为记录访问控制点,现在它可以工作了。

【讨论】:

    猜你喜欢
    • 2015-03-11
    • 1970-01-01
    • 2010-10-24
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2022-01-05
    相关资源
    最近更新 更多