简体   繁体   English

Android BLE,读写特性

[英]Android BLE, read and write characteristics

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. 我的开发工具是三星Galaxy J和Android 4.3。

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堆栈是多么不稳定......

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. 这用于将数据发送到BLE设备,通常在BLE设备的数据模式下。 This callback is called when you type 键入时会调用此回调

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(特征)发送数据并且BLE设备以某个值响应时,将调用此回调。

Usually a BLE device has few characteristics, to make it simple, I name a few characteristics 通常BLE设备具有很少的特性,为了简单起见,我列举了一些特征

  • WRITE - write Characteristics - 写特征
  • READ - read Characteristics - 读特征

To make it clear, when you send data, you will need to use WRITE characteristics and then when the BLE device responds Android app will call READ characteristics 为清楚起见,当您发送数据时,您将需要使用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堆栈不会发出第二个写入特征!

setValue:characteristics.setValue(data)你应该使用gatt.setCharacteristicNotification(Char,true)来设置setNotification

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

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