扫描BLE蓝牙

Android4.3以上加入了低功耗蓝牙,可以大大节省设备功耗。
低功耗蓝牙包括的术语及概念:



如上图所示,使用低功耗蓝牙可以包括多个Profile,一个Profile中有多个Service,一个Service中有多个Characteristic,一个Characteristic中包括一个value和多个Descriptor。

Android中进行蓝牙开发需要使用到的类的执行过程是:
1、使用BluetoothAdapter.startLeScan来扫描低功耗蓝牙设备
2、在扫描到设备的回调函数中会得到BluetoothDevice对象,并使用BluetoothAdapter.stopLeScan停止扫描
3、使用BluetoothDevice.connectGatt来获取到BluetoothGatt对象
4、执行BluetoothGatt.discoverServices,这个方法是异步操作,在回调函数onServicesDiscovered中得到status,通过判断status是否等于BluetoothGatt.GATT_SUCCESS来判断查找Service是否成功
5、如果成功了,则通过BluetoothGatt.getService来获取BluetoothGattService
6、接着通过BluetoothGattService.getCharacteristic获取BluetoothGattCharacteristic
7、然后通过BluetoothGattCharacteristic.getDescriptor获取BluetoothGattDescriptor

Android Bluetooth源码静态类图如下:


基础了解就到这
现在来看DeviceScanActivity的代码(第一次用简书,直接复制过来一团糟,我类个去,只能截图上了)

Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png

Paste_Image.png

在onListItemClick中有注释了三句,那是蓝牙连接的,暂时还没去弄,因为还涉及到蓝牙服务,设备控制,心率
不过详情可以看https://developer.android.com/samples/BluetoothLeGatt/src/com.example.android.bluetoothlegatt/DeviceScanActivity.html

温馨提示:记得翻墙!

超级截屏_20161201_161006.png

灰常抱歉,BEL蓝牙设备没电了,所以列表是空的,将就一下,sinIMasEI。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容