Android BLE,读写特性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24008249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:43:47  来源:igfitidea点击:

Android BLE, read and write characteristics

androidbluetooth-lowenergyandroid-bluetooth

提问by Tim

I am currently developing Android BLE, and encounters a lot of problems with the Android BLE stacks..

我目前正在开发Android BLE,遇到了很多Android BLE堆栈的问题..

My development tool is Samsung Galaxy J with Android 4.3.

我的开发工具是带有 Android 4.3 的三星 Galaxy J。

I want to know how can I read a characteristics from the BLE and the write the characteristics (is like i verify what data I have received, and then I send another data using the BLE)

我想知道如何从 BLE 读取特性并写入特性(就像我验证收到的数据,然后使用 BLE 发送另一个数据)

and I have serious problem understanding how the Android BLE callbacks works, I dont understand these 5 functions...and the manual is not clear, can anyone good soul explain in simple form???

并且我在理解 Android BLE 回调如何工作时遇到严重问题,我不理解这 5 个功能......而且手册不清楚,任何好心人都可以用简单的形式解释一下???

onCharacteristicWrite
onCharacteristicRead
onCharacteristicChanged
onDescriptorRead
onDescriptorWrite

My current situation is, I managed to read the data in onCharacteristicChanged() callback and then I verified the received the data I try to send the data by using

我目前的情况是,我设法在 onCharacteristicChanged() 回调中读取了数据,然后我验证了接收到的数据我尝试使用

characteristics.setValue(data)
gatt.writeCharacteristic(characteristics)

But, the Android BLE stack is not calling onCharacteristicsWrite() and in fact, Android just hangs there..

但是,Android BLE 堆栈没有调用 onCharacteristicsWrite(),事实上,Android 只是挂在那里。

I try to google about Android BLE, there is not much information and only bunch of complains on how unstable the BLE stacks is......

我尝试谷歌关于 Android BLE 的信息,没有太多信息,只有一堆关于 BLE 堆栈不稳定的抱怨......

回答by user2094060

Each of the callback from the Android BLE has its functions;

Android BLE 的每个回调都有其功能;

onDescriptorRead and onDescriptorWrite

onDescriptorRead 和 onDescriptorWrite

This is used to write/read the configuration settings for the BLE device, some manufactures might require to send some data to the BLE device and acknowledge it by reading, before you can connect to the BLE device

这用于写入/读取 BLE 设备的配置设置,一些制造商可能需要向 BLE 设备发送一些数据并通过读取确认,然后才能连接到 BLE 设备

onCharacteristicWrite

onCharacteristicWrite

This is used to send data to the BLE device, usually in data mode for the BLE device. This callback is called when you type

这用于向 BLE 设备发送数据,通常在 BLE 设备的数据模式下。键入时调用此回调

gatt.writeCharacteristic(characteristics);

onCharacteristicRead

onCharacteristicRead

This is used to read data from the BLE device The callback is called when you write this code

这个是用来从BLE设备读取数据的,写这段代码的时候会调用回调

gatt.readCharacteristic(characteristics);

onCharacteristicChanged

onCharacteristicChanged

This callback is called when you are trying to send data using writeCharacteristic(characteristics) and the BLE device responds with some value.

当您尝试使用 writeCharacteristic(characteristics) 发送数据并且 BLE 设备以某个值进行响应时,将调用此回调。

Usually a BLE device has few characteristics, to make it simple, I name a few characteristics

通常一个 BLE 设备的特性很少,为了简单起见,我列举了一些特性

  • WRITE- write Characteristics
  • READ- read Characteristics
  • WRITE- 写入特性
  • READ- 读取特性

To make it clear, when you send data, you will need to use WRITEcharacteristics and then when the BLE device responds Android app will call READcharacteristics

明确地说,当您发送数据时,您将需要使用WRITE特性,然后当 BLE 设备响应时,Android 应用程序将调用READ特性

A very important point to note is Android BLE stack allows you to write characteristics one at a time only!!

需要注意的一个非常重要的一点是 Android BLE 堆栈允许您一次只编写一个特征!!

Example: IF you try to call write characteristics twice at a same time

示例:如果您尝试同时调用写入特性两次

gatt.writeCharacteristic(characteristics);
gatt.writeCharacteristic(characteristics);

The Android BLE stack will not issue the 2nd write characteristics!

Android BLE 堆栈不会发出第二次写入特性!

回答by xiao lu

Before setValue:characteristics.setValue(data)you should use gatt.setCharacteristicNotification(Char,true)to setNotification.

setValue:characteristics.setValue(data)您应该使用gatt.setCharacteristicNotification(Char,true)to之前setNotification