Bluetooth03
经典蓝牙 低功耗蓝牙的区别
蓝牙低功耗
Android 4.3 为发挥核心作用的蓝牙低功耗(蓝牙 LE)引入了平台支持。在 Android 5.0 中,Android 设备现在可以发挥蓝牙 LE 外围设备的作用。应用可以利用此功能让附近设备发现它。例如,您可以开发这样的应用:让设备发挥计步器或健康监测仪的作用,并与其他蓝牙 LE 设备进行数据通信。
新增的
android.bluetooth.leAPI 让您的应用可以发布广告、扫描响应以及与附近的蓝牙 LE 设备建立连接。要使用新增的广告和扫描功能,请在您的清单中添加BLUETOOTH_ADMIN权限。当用户更新您的应用或从 Play 商店下载您的应用时,会被要求向您的应用授予以下权限:“Bluetooth connection information:Allows the app to control Bluetooth, including broadcasting to or getting information about nearby Bluetooth devices.”要启动蓝牙 LE 广播,以便其他设备能发现您的应用,请调用
startAdvertising(),并传入AdvertiseCallback类的实现。回调对象会收到广播操作成功或失败的报告。Android 5.0 引入了
ScanFilter类,让您的应用可以只扫描其感兴趣的特定类型设备。要开始扫描蓝牙 LE 设备,请调用startScan(),并传入筛选器列表。在方法调用中,您还必须提供ScanCallback的实现,以便在发现蓝牙 LE 广播时进行报告。
蓝牙基本操作
蓝牙权限
1 | |
如果您希望您的应用启动设备发现或操作蓝牙设置,则还必须声明 BLUETOOTH_ADMIN 权限。 大多数应用需要此权限仅仅为了能够发现本地蓝牙设备。 除非该应用是将要应用户请求修改蓝牙设置的“超级管理员”,否则不应使用此权限所授予的其他能力。 注:如果要使用 BLUETOOTH_ADMIN 权限,则还必须拥有 BLUETOOTH 权限。 如打开关闭蓝牙
1 | |
6.0权限
1 | |
设置蓝牙
判断设备是否支持
1
2
3
4
5mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
}启用蓝牙
1
2
3
4
5if (mBluetoothAdapter==null||!mBluetoothAdapter.isEnabled()){
Toast.makeText(this,"设备不支持蓝牙权限",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"支持设备",Toast.LENGTH_SHORT).show();
}
查找设备
5.0以下的方式就不贴出来了,代码里有
1 | |
连接设备
蓝牙设备经常处于关机状态,先调用下面方法
1 | |
1 | |
https://www.cnblogs.com/Free-Thinker/p/11507349.html
6、关于autoConnect参数为true的意义?
在蓝牙核心文档Vol3: Core System Package[Host volume]->Part C: Generic Access Profile的Connection Modes and Procedures章节中有涉及到自动连接建立规程(Auto Connection Establishment Procedure)的定义。
自动连接建立规程用来向多个设备同时发起连接。一个中央设备的主机与多个外围设备绑定,只要它们开始广播,便立刻与其建立连接。跟多细节请参考蓝牙核心文档和协议栈源码。
回调
1 | |

Android 中 GATT 操作的流程

Android 中 GATT 操作的流程。右边这个图,APP 是我们的应用,右边蓝牙服务端,从左向右箭头是 APP 发起的请求,从右向左的箭头是回调。我们看到所有的操作都是异步的完成的。连接过程是,首先使用 gattConnect 发起连接,收到 onConnectionStateChange() 通知连接是否成功,若成功,则进行下一步的 discoverService(),这一步就是发现设备所有的 GATT Service,若发现成功,通过 onServiceDiscovered() 回调,这时才算真正的连接成功。然后可以通过 BluetoothGatt 的 getService() 来获得BluetoothGattService,进而获得BluetoothGattCharacteristic 等,然后对 Characteristic 进行读写。
office
https://developer.android.com/guide/topics/connectivity/bluetooth?hl=zh-cn
https://developer.android.com/guide/topics/connectivity/bluetooth-le?hl=zh-cn
un
https://juejin.im/entry/5919630444d904006c6e14ca
设备通知手机
5 当你从文档看到遍历出来的UUID有接送通知的功能。这时你就可以设置可以接收通知。通过拿到对应通知UUID的BluetoothGattCharacteristic,调用setCharacteristicNotification().其中00002902-0000-1000-8000-00805f9b34fb是系统提供接受通知自带的UUID,通过设置BluetoothGattDescriptor相当于设置BluetoothGattCharacteristic的Descriptor属性来实现通知,这样只要蓝牙设备发送通知信号,就会回调onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) 方法,这你就可以在这方法做相应的逻辑处理。
https://juejin.im/entry/58c74fc42f301e006bce23fb
经典蓝牙开发
https://www.jianshu.com/p/453a5cda5646
设置 00002902-0000-1000-8000-00805f9b34fb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18/* 设置特征信息推送 */
··· BluetoothGattCharacteristic characteristic;
mGatt.setCharacteristicNotification(characteristic,true);
/* CCCD 的UUID */
private UUID ID_CCCD = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
/* 获取CCCD */
BluetoothGattDescriptor cccd = characteristic.getDescriptor(ID_CCCD);
/* 设置推送通知,参考值为:
* BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE: 通知
* BluetoothGattDescriptor.ENABLE_INDICATION_VALUE: 指示
* BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE: 关闭
*/
cccd.setValue(参考值);
/* 写入CCCD */
mGatt.writeDescriptor(descriptor)https://www.jianshu.com/p/43b1956d9f5c
官方三个蓝牙示例 :https://github.com/googlesamples?utf8=%E2%9C%93&q=bluetooth&type=&language=
问题
扫描不到任何设备