简体   繁体   English

在 BLE 设备上写入特性?

[英]Write characteristic on BLE devices?

can anyone help me with Write characteristic on BLE devices?i would need detail explanation i`m reading tutorials but i get more confused.任何人都可以帮助我在 BLE 设备上写入特性吗?我需要详细解释我正在阅读教程,但我更困惑。 Please help me.请帮我。 Now i´m working with read characteristic and i´m able to check the notification that is sending me the other device with no problem.现在我正在使用读取特性,并且我能够检查向我发送其他设备的通知,没有问题。 What i need now is to be able to send information from my aplication to the other device;我现在需要的是能够将信息从我的应用程序发送到其他设备; making that my device could act as a Server.使我的设备可以充当服务器。 (BluetoothGattServer) i´m working with android application. (BluetoothGattServer) 我正在使用 android 应用程序。

Basically, what you need to do is to implement the onCharacteristicWriteRequest function.基本上,您需要做的是实现 onCharacteristicWriteRequest 函数。 Then in it check whether you need to store the buffers to combine them later by checking the value of preparedWrite.然后在其中检查是否需要存储缓冲区以便稍后通过检查 prepareWrite 的值来合并它们。

Then if you need to combine the whole buffer later on, you need to store the buffers and know the order.然后,如果您稍后需要合并整个缓冲区,则需要存储缓冲区并知道顺序。 then when the onExecuteWrite is called with execute set to true, you would combine the buffers, otherwise you would cancel the write.然后当在执行设置为 true 的情况下调用 onExecuteWrite 时,您将合并缓冲区,否则您将取消写入。

Full exanmple of the logic descriped here can be found from my github repo's BLEAdvertiserLollipop.java file.此处描述的逻辑的完整示例可以从我的 github 存储库的BLEAdvertiserLollipop.java文件中找到。

you can find an implementation of writeCharacteristic in this repo basically you must implement it in your service:你可以在这个repo 中找到 writeCharacteristic 的实现,基本上你必须在你的服务中实现它:

public void writeCharacteristic(BluetoothGattCharacteristic characteristic, byte[] value) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        characteristic.setValue(value);
    }

and then call it from your activity.然后从您的活动中调用它。

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

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