简体   繁体   English

有没有办法在BLE特性中只写一个字节?

[英]Is there a way to write just a byte in BLE characteristic?

I am new to android. 我是Android新手。 My BLE device expects only one byte(flag). 我的BLE设备只需要一个字节(标志)。 I tried writing only one byte in a byte array but it never works. 我尝试只在字节数组中写入一个字节,但是它永远无法正常工作。 While writing an array to another characteristic where it expects 2 bytes works. 在将数组写入期望有2个字节的另一个特性时,该方法起作用。 Is there a way to solve my problem or do I have to ask for device code to be changed? 有什么办法可以解决我的问题,还是必须要求更改设备代码?

I am working with this project as my example 我正在以这个项目为例

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)

Here is the error, I don't get the BLUETOOTH_PRIVILEGED error, but I don't get any errors when I write to the characteristic that accepts 2 bytes. 这是错误,我没有收到BLUETOOTH_PRIVILEGED错误,但是当我写入接受2个字节的特征时,我没有任何错误。

First, to use bluetooth in your app, you need to add the following permissions to your app manifest : 首先,要在您的应用中使用蓝牙,您需要向您的应用清单添加以下权限:

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

It is possible to write a 1 byte value to a BLE characteristic, provided the device expects a 1 byte value. 如果设备期望一个1字节的值,则可以将1个字节的值写入BLE特性。

If there is a length issue, you should see the error in the onCharacteristicWrite() callback: 如果存在长度问题,您应该在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);
        }
}

Also, I would move the writeCharacteristic() call inside your check for PROPERTY_WRITE to ensure the write operation can be done: 另外,我将在您的PROPERTY_WRITE检查中移动writeCharacteristic()调用以确保可以完成写操作:

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

The problem wasn't in what I write in the characteristic it was what characteristic I used. 问题不在于我在特性中写的是我所使用的特性。 I used HID Control Point which when tested on the computer nRF Connect app worked fine for my device but it would not work on android because of its predefined states. 我使用了HID控制点,当在计算机上进行nRF Connect应用程序测试时,该设备对我的设备运行良好,但由于其预定义状态,因此无法在android上运行。 We just changed characteristic to Record Access Control Point and it works for now. 我们只是将特性更改为“记录访问控制点”,并且现在可以使用。

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

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