android 蓝牙研发之从设备
1,定义权限
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
2,检查和请求权限
在启动页检查和请求权限,开启蓝牙。启动后台服务Service发送广播。
3,蓝牙广播定义
3.1广播配置
广播数据为62位,厂商信息长度不能超过20,可以向服务中携带数据即addServiceData(),长度不能超过20,广播总长度不得超过62。
AdvertiseSettings settings = new AdvertiseSettings.Builder()
.setConnectable(true) .setTimeout(0) .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
.setTxPowerLevel(ADVERTISE_TX_POWER_HIGH) .build();
setConnectable设置该蓝牙是否可连接;setTimeout(0)设置该蓝牙连接超时无限制,最大可设置180000 ms;setAdvertiseMode(int) 设置广播频率,可设高中低三种模式,即1000ms、250ms、100ms; setTxPowerLevel 设置功耗,高功耗在一米内信号值56。
3.2 设置广播数据
AdvertiseData advertiseData = new AdvertiseData.Builder()
.setIncludeDeviceName(false) //广播不包含蓝牙名称
.setIncludeTxPowerLevel(false)//广播不包含电源信息
.addServiceUuid(new ParcelUuid(UUID_SERVER))//广播中添加ServiceUUID
.addManufacturerData(0,new byte[]{1,2,3,4}) //设置厂商信息
.build();
3.3 广播响应数据定义
AdvertiseData scanResponseData = new AdvertiseData.Builder()
.setIncludeDeviceName(true) //如果不广播名字搜索不到
.addManufacturerData(0,new byte[]{1,2,3,4})//厂商信息
.setIncludeTxPowerLevel(false).build();//不广播电源
3.4 设置广播回调AdvertiseCallback
实现AdvertiseCallback的onStartSuccess和onStartFailure方法。发出广播后回调该方法是否广播成功。
3.5开始广播
BluetoothLeAdvertiser bla= mBluetoothAdapter.getBluetoothLeAdvertiser();
bla.startAdvertising(settings, advertiseData, scanResponseData, callback);
4 添加定制的服务ServiceUUID
在3.4广播成功后,需要添加定制的ServiceUUID,利于移动端扫描和数据通信。
private void initServices(Context context) {
bluetoothGattServer=mBluetoothManager.openGattServer(context, bgsCallback); BluetoothGattService service = new BluetoothGattService(UUID_SERVER, BluetoothGattService.SERVICE_TYPE_PRIMARY);
//add a read characteristic.
BluetoothGattCharacteristic charR = new BluetoothGattCharacteristic(UUID_CHARREAD,
BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ);
BluetoothGattCharacteristic charRW= new BluetoothGattCharacteristic(UUID_CHARWRITE,
BluetoothGattCharacteristic.PROPERTY_READ
| BluetoothGattCharacteristic.PROPERTY_WRITE
| BluetoothGattCharacteristic.PROPERTY_NOTIFY
, BluetoothGattCharacteristic.PERMISSION_WRITE | BluetoothGattCharacteristic.PERMISSION_READ);
service.addCharacteristic(charR);
service.addCharacteristic(charRW);
bluetoothGattServer.addService(service);
}
private static UUID UUID_SERVER = UUID.fromString("000018f4-0000-1000-8000-00805f9b34fb"); //服务
private static UUID UUID_CHARREAD = UUID.fromString("00002a15-0000-1000-8000-00805f9b34fb"); //特征 读
private static UUID UUID_CHARWRITE = UUID.fromString("00002a16-0000-1000-8000-00805f9b34fb"); //特征 读\写\通知
定制服务,向服务中添加两个特征2a15和2a16,定义2a15的权限为读,定义2a15的权限为读、写、通知。
5 定制广播的监听
即4中添加Service时的BluetoothGattServerCallback,bgsCallback,按需求实现方法。
onConnectionStateChange()连接状态改变时回调
onServiceAdded() 定制的服务添加结果回调
onCharacteristicReadRequest() 主设备请求读时回调
onCharacteristicWriteRequest() 主设备请求写时回调
onNotificationSent() 从设备通知发送时回调
onMtuChanged() 厂商信息变更时回调
在demo中,主设备请求写入开门指令时,onCharacteristicWriteRequest将执行,根据请求的id、设备、写入的数据进行开门业务处理,将开门结果响应给主设备。因通信过程耗时,所以demo中响应主设备后延迟3秒执行与该设备断开连接。