一,ble 简单介绍
BLE: Bluetooth Low Energy, 即蓝牙低功耗,它是一种技术,从蓝牙 4.0 开始支持.蓝牙低功耗芯片有两种模式:单模和双模.
单模:只能执行低功耗协议栈,也就是只支持 ble.
双模:支持传统蓝牙以及 ble 的使用.
较传统蓝牙:传输速度更快,覆盖范围更广,安全性更高,
延迟更短,耗电低等优点.
关键术语和概念:
Gatt:(Generic Attribute Profile)-通用属性配置文件,用于在 ble 链路上发送和接收被称为 "属性" 的数据块.目前所有的 ble 应用都是基于 GATT 的.一个设备可以实现多个配置文件.
ble 交互的桥梁是 Service,Characteristic,Desciptor.
Characteristic:可以理解为一个数据类型,它包括一个 value 和 0 至多个对此 characteristic 的描述(Descriptor).
Descriptor:对 Characterisctic 的描述,如范围,单位等.
Service:Characteristic 的集合.它可以包含多个 Characteristic.
一个 ble 终端可以包含多个 Service,一个 Service 可以包含多个 Characteristic,一个 Characteristic 包含一个 value 和多个 Descriptor,一个 Descriptor 包含一个 value.其中 Characteristic 比较重要,用的比较多.
这三部分都由 UUID 作为唯一标示符,以此区分.
UUID(Universally Unique Identifier),含义是通用唯一识别码,它是在一定范围内唯一的机器生成的标识符.标准的 UUID 格式为:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12).
ble 中有四个角色:
广播者(Braodcaster):广播发送者,是不可连接的设备.
观察者(Observer):扫描广播,不能够启动连接.
广播者和观察者不能建立连接.应用:温度传感器和温度显示器.
外围(periphery):广播发送者,可连接的设备,在单一链路层作为从机.
中央(central):扫描广播,启动连接,在单一或多链路层作为主机.
中央和外围可以进行配对,连接,数据通信.应用:手机和手表.
网上说:(一个中央可以同时连接多个周边,但是一个周边只能连接一个中央.)
但是我测试周边可用连接多个中央设备,并且能正常通信.
二,Android
注意:Android 4.3(API 18)引入 ble 相关接口.
相关类
目录:frameworks/base/core/java/android/bluetooth/
BluetoothGatt:中央使用和处理数据;
BluetoothGattCallback:中央的回调.
BluetoothGattServer:周边提供数据;
BluetoothGattServerCallback:周边的回调
BluetoothGattService:Gatt 服务
BluetoothGattCharacteristic:Gatt 特性
BluetoothGattDescriptor:Gatt 描述
外围设备
主要步骤:
获取 / 打开周边(外围)
mGattServer = mBluetoothManager.openGattServer(mContext, callback);
//其中callback是一个MyGattServerCallback(继承了BluetoothGattServerCallback)对象.
初始化描述,特性和服务.
//描述:
new BluetoothGattDescriptor(UUID.fromString(DESC_UUID), descPermissions);
//特性 :
final int properties = BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_NOTIFY;
final int permissions = BluetoothGattCharacteristic.PERMISSION_READ; | BluetoothGattCharacteristic.PERMISSION_WRITE;
new BluetoothGattCharacteristic(UUID.fromString(CHAR_UUID), properties, permissions);
gattChar.addDescriptor(gattDesc);
property 表示属性.permission 表示权限.这两个都和权限相关.
如果 property 未设置 PROPERTY_READ,permission 设置 PERMISSION_READ,则中央设备 readCharacteristic 主动读取特征值方法返回 false,此操作失败.
而如果 property 设置 PROPERTY_READ,permission 未设置 PERMISSION_READ,则中央设备 readCharacteristic 主动读取特征值方法返回 true,此操作成功,外围设备发送响应,中央设备受到响应 GATT_READ_NOT_PERMITTED.
所以说如果想要 characteristic 可读,则这两个都要设置.
PROPERTY_WRITE 和 PERMISSION_WRITE 也和上面类似.
PROPERTY_NOTIFY 表示支持 notification.
除此之外,还有 WRITE_TYPE,
WRITE_TYPE_DEFAULT 中央设备写特征,默认由远程设备请求确认.
WRITE_TYPE_NO_RESPONSE 不需要请求确认
WRITE_TYPE_SIGNED 写特征包括认证签名.
//服务:
BluetoothGattService bs = new BluetoothGattService(UUID.fromString(SERV_UUID), BluetoothGattService.SERVICE_TYPE_PRIMARY);
bs.addCharacteristic(gattChar);
第二个参数为 service type,
SERVICE_TYPE_PRIMARY 基础服务,主要服务.
SERVICE_TYPE_SECONDARY 辅助服务(由初级服务包含在内).
BluetoothGattService 类中方法
addService(bluetoothGattService),将辅助服务添加到主要服务中.
getIncludeedServices() 获取包含的服务列表.
getType() 获取服务的 type.
getUuid() 获取服务的 UUID.添加服务
mGattServer.addService(bs);
设置广播数据
开始广播
这在 android4.3 没有提供,在 android5.0 才提供了设置广播数据,发送广告包等方法.我们开发是基于 android4.3 的,按理说我们是不可以作为外围设备的,不过我们 framework 以及底层都进行了修改,提供了这些方法,说以我们的 android 设备可以作为外围.
mGattServer.startAdvertising();// 开始广播 mGattServer.stopAdvertising();// 停止广播
收到 central 扫描请求,回应扫描请求.
这个不需要我们管理,此时会广播之前的设置的广播数据.
收到 central 连接请求,建立连接.
连接成功后 外围可以断开连接.
mGattServer.cancelConnection(device);
响应 central 发起的 gatt 服务发现请求,回应服务信息.
响应 central 发起的 gatt 特性发现请求,回应特性信息.
响应 central 发起的 gatt 描述发现请求,回应描述信息.
这三个不需要我们去操作,系统底层会处理.
对 central 的读写做响应.回应特性值更新特性值.
响应特征值:
MyGattServerCallback extends BluetoothGattServerCallback.
其中有几个常用的方法:
onConnectionStateChange(BluetoothDevice device, int status, int newState)
监听设备连接状态.device 远程设备,newStateble 连接状态,只能为 BluetoothProfile.STATE_CONNECTED 和 BluetoothProfile.STATE_DISCONNECTED.
onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset,
BluetoothGattCharacteristic characteristic)
监听中心设备读 Characteristic 的请求,
requestId 请求的标识.
offset 特性值偏移量.
Characteristic 要读的特性.
此方法要求作出响应.
mGattServer.sendResponse(device, requestId,
BluetoothGatt.GATT_SUCCESS, offset, null);
最后一个参数可以设置传的数据,byte[]类型的.
onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic,
boolean preparedWrite, boolean responseNeeded, int offset, byte[] value)
监听中心设备写 Characteristic 的请求,
preparedWrite true 则写操作必须排队等待稍后执行.
responseNeeded 是否需要响应.
value 写的数据.
需要响应则必须 sendResponse.
更新特征值:
外围设备向中心设备不能发送数据,必须通过下面这个方法.
characteristic.setValue(res.getBytes());
mGattServer.notifyCharacteristicChanged(device,
characteristic, false);
最后一个参数表示是否需要客户端确认.
中央设备
主要步骤:
搜索正在广播的外围设备. 发起扫描请求 这两步我们在上层只需要一步就可以了.
搜索ble设备
//搜索附近所有的外围设备
mBluetoothAdapter.startLeScan(mLeScanCallback);
//搜索某些uuid的外围设备.
mBluetoothAdapter.startLeScan(uuid[], mLeScanCallback);停止扫描mBluetoothAdapter.stopLeScan(mLeScanCallback);
监听扫描结果.
mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
}
};
device 搜索到的 bye 设备.
rssi 信号强度
scanRecord 远程设备广告记录的内容(蓝牙名称)
发起连接请求,获得中央.
mBluetoothGatt = device.connectGatt(mContext, false,mGattCallback);
第二个参数 autoConnect 如果为 false 直接立即连接.
true 等待远程设备可用时(在范围内,..)连接.并不是断开后重新连接.
第三个参数 Gatt callback.
private BluetoothGattCallback mGattCallback = newBluetoothGattCallback() {};
连接成功后,发送 gatt 服务发现请求.
//连接状态改变回调
onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
//连接成功后,发送发现服务请求.
mBluetoothGatt.discoverServices();
}
}
//发现服务回调.
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
//发现成功后,则可以通过下面方法获取service 列表.
mBluetoothGatt.getServices();
}
}
获得 Characteristic 和 Descriptor.
gattSer.getCharacteristics();
gattChar.getDescriptors();
通过获取的特性值,可以进行下操作:
写入特性值
读取特性值
订阅特性值.
写入:
characteristic.setValue(data.getBytes());
mBluetoothGatt.writeCharacteristic(characteristic);
此时 外围设备会收到回调 onCharacteristicWriteRequest
外围设备回应 mGattServer.sendResponse
中央设备收到响应,回调
onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status)
读取:
mBluetoothGatt.readCharacteristic(characteristic);
外围设备回调 onCharacteristicReadRequest
并响应此请求 sendResponse
中央设备收到响应回调
onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
订阅:
setCharacteristicNotification(characteristic, true);
当指定 Characteristic 值发生变化时,是否接收通知.
当设为 true,并且 Characteristic 发生变化时,回调方法:
onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic)
通过参数 characteristic,可获得 getValue 获得其中的内容.
中央设备的其他一些方法
readDescriptor(descriptor) 读取描述
writeDescriptor(descriptor) 写描述
readRemoteRssi() 读取连接设备的 rssi.
disconnect(); 断开 bel 连接.
close(); 关闭中央设备.(不用时及时关闭,否则有的手机重连连不上.)
看过本文的人也看了:
来源: http://lib.csdn.net/article/embeddeddevelopment/34566