蓝牙BLE与设备交互研发录一
来源:互联网 发布:淘宝卖家怎样收款. 编辑:程序博客网 时间:2024/05/16 03:51
本文建立在官方demo BluetoothLeGatt上进行探索。在BluetoothLeGatt工程代码基础上对蓝牙设备进行连接与发送指令,并且接收到蓝牙设备发回来的指令。
搜索蓝牙设备
mBluetoothAdapter.startLeScan(mLeScanCallback);
mBluetoothAdapter.stopLeScan(mLeScanCallback);分别是这两个代码,demo里面有,这里就不要细讲,回调看mLeScanCallback。
连接蓝牙设备
Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);然后在回调mServiceConnection 里面的onServiceConnected方法执行连接蓝牙设备就行了。如:final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(mDeviceAddress);mBluetoothGatt = device.connectGatt(this, false, mGattCallback);mDeviceAddress为蓝牙设备的mac地址mGattCallback是回调
下面开始描述一个BLE设备进行开发的具体步骤。
设备的UUID
对于一种设备来说,需要知道它对应的UUID才能读写。一般先提前了解清楚产品的下面4个信息。
SERVICE_UUIDWRITE_UUIDREAD_UUIDCLIENT_CHARACTERISTIC_CONFIG
比如:
微信硬件规定的
SERVICE_UUID为0000FEE7-0000-1000-8000-00805F9B34FBWRITE_UUID为0000FEC7-0000-1000-8000-00805F9B34FBREAD_UUID为0000FEC8-0000-1000-8000-00805F9B34FBCLIENT_CHARACTERISTIC_CONFIG 是一个固定值:00002902-0000-1000-8000-00805f9b34fb
能使读characteristic
以下代码片段描述了怎么去使能对应的特征。
流程:先找到产品对应的自定义service,在从service里拿到读的characteristic,然后根据characteristic的特性使能characteristic 的Notification模式或者INDICATION模式,这时候就可以接收蓝牙设备发过来的信息了。
public void enableRxNotification()
BluetoothGattService service = mBluetoothGatt.getService(SERVICE_UUID);
BluetoothGattCharacteristic readCharacteristic = service.getCharacteristic(READ_UUID);
BluetoothGattDescriptor descriptor = readCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
if ((readCharacteristic.getProperties() | BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
mBluetoothGatt.setCharacteristicNotification(readCharacteristic, true);
mBluetoothGatt.writeDescriptor(descriptor);
else if ((readCharacteristic.getProperties() | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0)
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.setCharacteristicNotification(readCharacteristic, true);
mBluetoothGatt.writeDescriptor(descriptor);
}
写数据
简单来说就是写characteristic,以下代码片段描述了怎么给一个蓝牙设备写数据。流程:先找到产品对应的自定义service,在从service里拿到写的characteristic,然后设置characteristic的值,通过蓝牙把characteristic的数据发到设备上。如果发送成功,就会回调在onCharacteristicWrite里,这是能不能接收到信息取决了发过去的指令对不对,换句话说这时候没法判断发送的指令对不对。public boolean writeCharacter(String value)
{ BluetoothGattService service = mBluetoothGatt.getService(SERVICE_UUID);
BluetoothGattCharacteristic writeCharacteristic = service.getCharacteristic(WRITE_UUID);
writeCharacteristic.setValue(Utils.hexStringToBytes(value));
return mBluetoothGatt.writeCharacteristic(writeCharacteristic);}
了解指令格式
不是随便写一点数据蓝牙设备就会回信息过来的,需要满足一定的指令格式,这个就得读蓝牙产品的一些文档,看看里面的指令格式。如果发送的指令格式是正确的,蓝牙设备收到处理后就会回信息过来,这时,在onCharacteristicChanged
或者onCharacteristicRead里就可以看到数据的回调了
0 0
- 蓝牙BLE与设备交互研发录一
- BLE 蓝牙 与APP 得交互 内设 外设
- 基于蓝牙BLE的智能设备开发(一)
- Android开发蓝牙与ble设备的通讯
- android 蓝牙ble设备开发
- Android Ble 4.0 蓝牙开发交互
- 蓝牙4.0--Android BLE(一)
- 谈谈蓝牙4.0(BLE)模块与安卓的数据交互
- 医疗设备BLE蓝牙开发的总结。
- [BLE--Link Layer]设备蓝牙地址
- BLE低功耗蓝牙设备开发
- Android智能设备蓝牙连接(BLE)
- 安卓蓝牙BLE设备开发
- Android BLE设备蓝牙通信框架BluetoothKit
- Android BLE低功耗蓝牙开发(下) BLE客户端(中央设备)与GATT服务的通讯
- ionic项目中JavaScript与ble(低功耗蓝牙)设备传输数据
- 安卓--第十天--蓝牙app实现与BLE设备简单通信
- Android蓝牙4.0BLE与智能设备间的通讯全解析
- iOS 短信验证码倒计时按钮的实现
- IOS9 适配
- UVA 12825 dp
- Mac下安装Octave
- 打印1到最大的n位数
- 蓝牙BLE与设备交互研发录一
- Xcode7中你一定要知道的炸裂调试神技
- Linux----利用hosts.deny 防止暴力破解ssh
- 大型网站技术架构--读书笔记
- ireport打印pdf字体问题
- 尺寸单位转换
- PRML 02 Introduction:贝叶斯概率
- 统计单词频率-map
- HTML5——Range对象(1)