android ble notify,Android BLE 以Notify的方式接收数据

Log

d46dcde61572f05645486aa83a5c83fb.png

欲善其事,先利其器。推荐一款功能强大的测试工具:nRF Connect

一般情况蓝牙设备所有的UUID都是硬件工程师提供的,但也有例外,比如和我对接的硬件工程师连UUID是什么都不知道……那你就需要自己动手丰衣足食,使用nRF Connect这款工具来查看你的设备的UUID。

一、使用的Android自带的bluetooth

import android.bluetooth.BluetoothDevice;

import android.bluetooth.BluetoothGatt;

import android.bluetooth.BluetoothGattCallback;

import android.bluetooth.BluetoothGattCharacteristic;

import android.bluetooth.BluetoothGattDescriptor;

import android.bluetooth.BluetoothGattService;

import android.bluetooth.BluetoothProfile;

二、接收数据需要订阅,重写BluetoothGattCallback里onServicesDiscovered方法,然后在里面订阅

/**

* 发现设备(真正建立连接)

* @param gatt

* @param status

*/

@Override

public void onServicesDiscovered(BluetoothGatt gatt, int status)

{

//直到这里才是真正建立了可通信的连接

//通过UUID找到服务

m_bluetoothGattService = m_bluetoothGatt.getService(SERVICE_UUID);

if(m_bluetoothGattService != null)

{

//写数据的服务和特征

m_bluetoothGattCharacteristic_write = m_bluetoothGattService.getCharacteristic(WRITE_UUID);

if(m_bluetoothGattCharacteristic_write != null)

{

Log.i(TAG, ">>>已找到写入数据的特征值!");

Message message = new Message();

message.what = MESSAGE_1;

handler.sendMessage(message);

}

else

{

Log.e(TAG, ">>>该UUID无写入数据的特征值!");

}

//读取数据的服务和特征

m_bluetoothGattCharacteristic_read = m_bluetoothGattService.getCharacteristic(READ_UUID);

if(m_bluetoothGattCharacteristic_read != null)

{

Log.i(TAG, ">>>已找到读取数据的特征值!");

//订阅读取通知

gatt.setCharacteristicNotification(m_bluetoothGattCharacteristic_read, true);

BluetoothGattDescriptor descriptor = m_bluetoothGattCharacteristic_read.getDescriptor(CONFIG_UUID);

descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

gatt.writeDescriptor(descriptor);

}

else

{

Log.e(TAG, ">>>该UUID无读取数据的特征值!");

}

}

}

SERVICE_UUID、WRITE_UUID、READ_UUID可以通过工具识别出来,CONFIG_UUID后面再说:

faf1a09eadfb8902934a6f848e559893.png

正常情况下工具会把CLIENT_CHARACTERISTIC_CONFIG对应的UUID也给识别出来,但是我手里这个设备它就是没有,再但是,没有CONFIG的UUID那这个工具又是怎么以Notify的方式接收到数据的???参考了别人的博客,抱着试一试的心态把别人UUID:00002902-0000-1000-8000-00805f9b34fb复制过来了,然后就接收到数据了(技术不够,玄学来凑)……

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android BLE(低功耗蓝牙)通信中,接收分包数据主要依赖于特性操作和回调函数的处理。 首先,我们需要注册特性操作的回调函数,以便能够正确地接收分包数据。在回调函数中,你可以通过onCharacteristicRead()方法处理读取特性的分包数据,或通过onCharacteristicChanged()方法处理接收特性的分包通知数据。 接收分包数据的步骤如下: 1. 连接到BLE设备并发现服务和特性。 2. 找到需要读取或接收的特性对象。 3. 使用BluetoothGatt的readCharacteristic()方法读取特性的分包数据,或使用BluetoothGatt的setCharacteristicNotification()方法启用该特性的通知功能。 4. 处理回调函数中的分包数据。 在处理回调函数时,可以通过分包数据的属性(Properties)来判断是否为分包数据。例如,如果一个特性的Properties包含BluetoothGattCharacteristic.PROPERTY_READ,那么可以使用readCharacteristic()方法逐个读取分包数据。如果一个特性的Properties包含BluetoothGattCharacteristic.PROPERTY_NOTIFY,那么可以使用onCharacteristicChanged()方法接收分包通知数据。 在处理分包数据时,可能需要将多个分包数据拼接在一起形成完整的信息。为了确保准确性,可以使用一个缓冲区来存储和管理拼接后的数据。 需要注意的是,对于较大的分包数据,一次性读取可能会导致BLE通信失败。因此,在读取分包数据时,可能需要将数据分成多个片段来处理,直到接收到所有分包数据为止。 总之,在Android BLE接收分包数据的过程中,我们需要正确处理回调函数,使用特性操作来读取或接收分包数据,并在处理数据时进行数据拼接和分片操作,以确保数据的完整性和准确性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值