Android BLE蓝牙4.0开发详解

这篇博客主要讲解 蓝牙 BLE 的用法。在讲解之前先讲一些概念性的东西,对于之前没接触过蓝牙开发,现在手上又有个蓝牙BLE项目需要做的人,先看下这些概念还是很重要的。因为我之前就是这样,之前没有接触过蓝牙方面的开发,然后来了个蓝牙的项目,于是就到网上百度了一番,于是有点茫然,产生了几点疑惑:
            1:发现蓝牙有传统蓝牙和低功耗蓝牙(ble)之分。那么什么是传统蓝牙,什么又是低功耗蓝牙?之前又没做过蓝牙开发,我该用哪种方式去开发我这个项目?用最新的 方式的话,传统方式蓝牙开发我是不是该要先了解?
            2:蓝牙到底有哪些版本?哪些版本称为传统蓝牙?哪些版本称为低功耗蓝牙?           
            3:传统蓝牙和低功耗蓝牙有什么区别?为什么低功耗蓝牙的出现使得智能能穿戴越来越流行?


             带着这些疑问,我又进行了一番的搜索,先把概念搞清,在研究了几份代码,才渐渐弄明白,再开发项目。


             蓝牙我们应该很早就听过,最常见的就是原来我们偶尔通过手机上的蓝牙来传输文件。貌似在低功耗蓝牙出现之前,蓝牙我们使用的并不多,蓝牙的产品貌似也不是很多。2010年6月30号蓝牙技术联盟推出了低功耗蓝牙,经过几年的发展,市场上基于低功耗系列的硬件产品越来越多,开发硬件,软件的厂商,公司越来越多。


             蓝牙发展至今经历了8个版本的更新。1.1、1.2、2.0、2.1、3.0、4.0、4.1、4.2。那么在1.x~3.0之间的我们称之为传统蓝牙,4.x开始的蓝牙我们称之为低功耗蓝牙也就是蓝牙ble,当然4.x版本的蓝牙也是向下兼容的。android手机必须系统版本4.3及以上才支持BLE API。低功耗蓝牙较传统蓝牙,传输速度更快,覆盖范围更广,安全性更高,延迟更短,耗电极低等等优点。这也是为什么近年来智能穿戴的东西越来越多,越来越火。还有传统蓝牙与低功耗蓝牙通信方式也有所不同,传统的一般通过socket方式,而低功耗蓝牙是通过Gatt协议来实现。若是之前没做过传统蓝牙开发,也是可以直接上手低功耗蓝牙开发的。因为它们在通信协议上都有所改变,关联不大。当然有兴趣的可以去下载些传统蓝牙开发的demo看看,在看看低功耗蓝牙的demo。两者的不同之处自然容易看出来。好了,我们下面开始讲低功耗蓝牙开发吧。低功耗蓝牙也叫BLE,下面都称之为BLE。

             BLE分为三部分:Service,Characteristic,Descriptor。这三部分都用UUID作为唯一标识符。UUID为这种格式:0000ffe1-0000-1000-8000-00805f9b34fb。比如有3个Service,那么就有三个不同的UUID与Service对应。这些UUID都写在硬件里,我们通过BLE提供的API可以读取到。

          一个BLE终端可以包含多个Service, 一个Service可以包含多个Characteristic,一个Characteristic包含一个value和多个Descriptor,一个Descriptor包含一个Value。Characteristic是比较重要的,是手机与BLE终端交换数据的关键,读取设置数据等操作都是操作Characteristic的相关属性。

            比如我有个BLE的硬件,我们可以用android 版本的light blue去连接上这个硬件,没有的话我文章底部提供了下载链接,不过它在android5.0以上跑不起来,大家也可以下载另外两个源码跑起来也可以连接上,不过这个android 版本的light blue最好用了。进入应用,就可以扫描到你的BLE设备,点击就会连接上,然后我们可以看到UUID列表,这里每一行的UUID都代表一个Service,再点击任意一行进去,又可以看到一个UUID列表,这里每一行的UUID都代表一个Characteristic,再点击任意一行进去,即可以操作这个Characteristic,比如写入数据或者读出数据等。

            好了,那我们来根据代码讲解下吧。代码有上传,里面有Bluetooth4_3/BLEDemo/Android_Lightblue.apk。下面讲解的是根据代码Bluetooth4_3来讲解的。

            1:首先在程序里我们要开启蓝牙权限

在应用程序manifest文件中添加如下代码,声明蓝牙权限。


  • 1

如果你想声明你的应用程序只能在支持BLE的设备上运行,可以将下面声明包含进你的应用程序manifest文件中:


 
  • 1

然而,如果你想让你的应用程序也能够在不支持BLE的设备上运行,你就应该将上面标签中的属性设置为required="false"。然后在运行的过程中使用PackageManager.hasSystemFeature()方法来判断设备是否支持BLE:


  1. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {

  2. Toast.makeText( this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();

  3. finish();

  4. }

  • 1

             2:判断设备是否支持蓝牙ble


  1. // 检查当前手机是否支持ble 蓝牙,如果不支持退出程序

  2. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {

  3. Toast.makeText( this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();

  4. finish();

  5. }

  • 1


           3:获取蓝牙适配器BluetoothAdapter
可以通过以下两种方式获取


  1. final BluetoothManager bluetoothManager =(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

  2. BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();

  • 1
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();     
 
  • 1
//如果mBluetoothAdapter == null,说明设备不支持蓝牙
 
  • 1

           4:弹出是否启用蓝牙的对话框


  1. if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {

  2. Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

  3. startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

  4. }

  5. @Override

  6. protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  7. switch (requestCode) {

  8. case REQUEST_ENABLE:

  9. if (resultCode == Activity.REQUEST_ENABLE_BT) {

  10. Toast.makeText( this, "蓝牙已启用", Toast.LENGTH_SHORT).show();

  11. } else {

  12. Toast.makeText( this, "蓝牙未启用", Toast.LENGTH_SHORT).show();

  13. }

  14. break;

  15. }

  16. }

  • 1
//也可以直接调用mBluetoothAdapter.enable()mBluetoothAdapter.disable()来启用禁用蓝牙。不过这种方式不会弹出询问对话框
 
  • 1

            5:搜索设备


  1. private void scanLeDevice(final boolean enable) {

  2. if (enable) {

  3. // Stops scanning after a pre-defined scan period.

  4. mHandler.postDelayed( new Runnable() {

  5. @Override

  6. public void run() {

  7. mScanning = false;

  8. mBluetoothAdapter.stopLeScan(mLeScanCallback);

  9. }

  10. }, SCAN_PERIOD); //10秒后停止搜索

  11. mScanning = true;

  12. mBluetoothAdapter.startLeScan(mLeScanCallback); //开始搜索

  13. } else {

  14. mScanning = false;

  15. mBluetoothAdapter.stopLeScan(mLeScanCallback); //停止搜索

  16. }

  17. }

  18. //启动搜索的操作最好放在Activity的onResume里面或者服务里面,我有发现放在onCreate有时响应不及时

  • 1

        6:搜索到设备会回调LeScanCallback接口


  1. private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

  2. @Override

  3. public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {

  4. runOnUiThread( new Runnable() {

  5. @Override

  6. public void run() {

  7. //在这里可以把搜索到的设备保存起来

  8. //device.getName();获取蓝牙设备名字

  9. //device.getAddress();获取蓝牙设备mac地址

  10. }

  11. });

  12. }

  13. };

  • 1

        7:选择一个设备进行连接。连接后会返回一个BluetoothGatt 类型的对象,这里定义为mBluetoothGatt。该对象比较重要,后面发现服务读写设备等操作都是通过该对象。
代码里建了一个service,里面封装了连接,读写设备等操作。连接是通过获取到的mac地址去进行连接操作就可以了。


  1. public boolean connect(final String address) {

  2. if (mBluetoothAdapter == null || address == null) {

  3. Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");

  4. return false;

  5. }

  6. // Previously connected device. Try to reconnect. (先前连接的设备。 尝试重新连接)

  7. if (mBluetoothDeviceAddress != null&& address.equals(mBluetoothDeviceAddress)&& mBluetoothGatt != null) {

  8. Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");

  9. if (mBluetoothGatt.connect()) {

  10. mConnectionState = STATE_CONNECTING;

  11. return true;

  12. } else {

  13. return false;

  14. }

  15. }

  16. final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

  17. if (device == null) {

  18. Log.w(TAG, "Device not found. Unable to connect.");

  19. return false;

  20. }

  21. // We want to directly connect to the device, so we are setting the

  22. // autoConnect

  23. // parameter to false.

  24. mBluetoothGatt = device.connectGatt( this, false, mGattCallback); //该函数才是真正的去进行连接

  25. Log.d(TAG, "Trying to create a new connection.");

  26. mBluetoothDeviceAddress = address;

  27. mConnectionState = STATE_CONNECTING;

  28. return true;

  29. }

  • 1

          8:连接后会回调BluetoothGattCallback接口,包括读取设备,往设备里写数据及设备发出通知等都会回调该接口。其中比较重要的是BluetoothGatt。


  1. private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

  2. @Override //当连接上设备或者失去连接时会回调该函数

  3. public void onConnectionStateChange(BluetoothGatt gatt, int status,int newState) {

  4. if (newState == BluetoothProfile.STATE_CONNECTED) { //连接成功

  5. mBluetoothGatt.discoverServices(); //连接成功后就去找出该设备中的服务 private BluetoothGatt mBluetoothGatt;

  6. } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { //连接失败

  7. }

  8. }

  9. @Override //当设备是否找到服务时,会回调该函数

  10. public void onServicesDiscovered(BluetoothGatt gatt, int status) {

  11. if (status == BluetoothGatt.GATT_SUCCESS) { //找到服务了

  12. //在这里可以对服务进行解析,寻找到你需要的服务

  13. } else {

  14. Log.w(TAG, "onServicesDiscovered received: " + status);

  15. }

  16. }

  17. @Override //当读取设备时会回调该函数

  18. public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {

  19. System.out.println( "onCharacteristicRead");

  20. if (status == BluetoothGatt.GATT_SUCCESS) {

  21. //读取到的数据存在characteristic当中,可以通过characteristic.getValue();函数取出。然后再进行解析操作。

  22. //int charaProp = characteristic.getProperties();if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0)表示可发出通知。 判断该Characteristic属性

  23. }

  24. }

  25. @Override //当向设备Descriptor中写数据时,会回调该函数

  26. public void onDescriptorWrite(BluetoothGatt gatt,BluetoothGattDescriptor descriptor, int status) {

  27. System.out.println( "onDescriptorWriteonDescriptorWrite = " + status + ", descriptor =" + descriptor.getUuid().toString());

  28. }

  29. @Override //设备发出通知时会调用到该接口

  30. public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic) {

  31. if (characteristic.getValue() != null) {

  32. System.out.println(characteristic.getStringValue( 0));

  33. }

  34. System.out.println( "--------onCharacteristicChanged-----");

  35. }

  36. @Override

  37. public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {

  38. System.out.println( "rssi = " + rssi);

  39. }

  40. @Override //当向Characteristic写数据时会回调该函数

  41. public void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {

  42. System.out.println( "--------write success----- status:" + status);

  43. };

  44. }

  • 1

                9:设备连接成功并回调BluetoothGattCallback接口里面的onConnectionStateChange函数,然后调用mBluetoothGatt.discoverServices();去发现服务。发现服务后会回调BluetoothGattCallback接口里面的 onServicesDiscovered函数,在里面我们可以获取服务列表。


  1. public List getSupportedGattServices() {

  2. if (mBluetoothGatt == null)

  3. return null;

  4. return mBluetoothGatt.getServices(); //此处返回获取到的服务列表

  5. }

  • 1

                10:获取到服务列表后自然就是要对服务进行解析。解析出有哪些服务,服务里有哪些Characteristic,哪些Characteristic可读可写可发通知等等。


  1. private void displayGattServices(List gattServices) {

  2. if (gattServices == null)

  3. return;

  4. for (BluetoothGattService gattService : gattServices) { // 遍历出gattServices里面的所有服务

  5. List gattCharacteristics = gattServices.getCharacteristics();

  6. for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { // 遍历每条服务里的所有Characteristic

  7. if (gattCharacteristic.getUuid().toString().equalsIgnoreCase(需要通信的UUID)) {

  8. // 有哪些UUID,每个UUID有什么属性及作用,一般硬件工程师都会给相应的文档。我们程序也可以读取其属性判断其属性。

  9. // 此处可以可根据UUID的类型对设备进行读操作,写操作,设置notification等操作

  10. // BluetoothGattCharacteristic gattNoticCharacteristic 假设是可设置通知的Characteristic

  11. // BluetoothGattCharacteristic gattWriteCharacteristic 假设是可读的Characteristic

  12. // BluetoothGattCharacteristic gattReadCharacteristic 假设是可写的Characteristic

  13. }

  14. }

  15. }

  16. }

  • 1

              11:可接收通知的UUID,设置其可以接收通知(notification)。下面函数参数为10中的gattNoticCharacteristic


  1. public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {

  2. if (mBluetoothAdapter == null || mBluetoothGatt == null) {

  3. Log.w(TAG, "BluetoothAdapter not initialized");

  4. return;

  5. }

  6. mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

  7. BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID

  8. .fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));

  9. if (descriptor != null) {

  10. System.out.println( "write descriptor");

  11. descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

  12. mBluetoothGatt.writeDescriptor(descriptor);

  13. }

  14. }

  • 1

              12:可读的UUID。下面函数参数为10中的gattReadCharacteristicreadCharacteristic调用成功会回调步骤8中的onCharacteristicRead函数


  1. public void readCharacteristic(BluetoothGattCharacteristic characteristic) {

  2. if (mBluetoothAdapter == null || mBluetoothGatt == null) {

  3. Log.w(TAG, "BluetoothAdapter not initialized");

  4. return;

  5. }

  6. mBluetoothGatt.readCharacteristic(characteristic);

  7. }

  • 1

               13:可写的UUID。下面函数参数为10中的gattWriteCharacteristic。writeCharacteristic调用成功会回调步骤8中的onCharacteristicWrite函数


  1. public void wirteCharacteristic(BluetoothGattCharacteristic characteristic) {

  2. if (mBluetoothAdapter == null || mBluetoothGatt == null) {

  3. Log.w(TAG, "BluetoothAdapter not initialized");

  4. return;

  5. }

  6. mBluetoothGatt.writeCharacteristic(characteristic);

  7. }

  • 1


              14:一般硬件里读出写入的数据为二进制类型,所以要熟悉整型,字符串,二进制,十六进制等它们之间的转换。有时间的话再整理一份进制数据转换的。好了整个BLE的工作过程差不多就这些,剩下的就是看自己怎么去处理获取到的那些数据呈现到界面了。

代码说明: 下面是源码下载。源码里面有Bluetooth4_3/BLEDemo/Android_Lightblue.apk三个.前两个是BLE的demo。我们上面讲解的是基于Bluetooth4_3这个demo讲解的。BLEDemo 这个功能较Bluetooth4_3多一些,有兴趣的可以都看下。Android_Lightblue.apk是Android版的lightblue,在进行ble开发的时候用该app作为辅助工具还是不错的,功能较Bluetooth4_3 BLEDemo 这两个demo都强大。不过Android_Lightblue.apk在android5.0以上的版本手机上运行不起来,我也没有该app的源码。看看后面会不会有更新

源码下载

蓝牙开发遇到的坑

1.onServicesDiscovered 回调里不能直接执行 write /readDataFromCharacteristic() 或者 enableNotificationOfCharacteristic之类的,而要放到主线程里执行,如 handler.post( … );

2.如果发现连接上了,service也discover到了,但是始终不能触发onCharacteristicChanged的,一定要查找如下2个重要原因:
1). 一定要gatt.setCharacteristicNotification(characteristic, enable);
2). 如果设置了1).却还是发现没有触发,这个时候比较坑爹了,加上对此Characteristic的descriptor做indication Enable就应该可以了;

for(BluetoothGattDescriptor dp:characteristic().getDescriptors()) {
dp.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt().writeDescriptor(dp);
}

3… 不同的机型的discoverService到onServiceDiscovered之间的耗时长短不一,这会导致一个问题:如果蓝牙硬件设备支持离线传输,即有记忆功能,连接上之后多久发送之前的数据的问题。如果连接之上立即发送,那么手机端的onServiceDiscovered尚未触发,这样Characteristic的值就获取不了(因为你的service,Characteristic都尚未初始化好),从而导致失败。

解决的办法有3个:
1)建立一套ACK机制,蓝牙硬件设备不断的广播,直到所有的数据都收到返回的ACK确认才不再广播即可。
2)更好的办法是,当手机端onServiceDiscovered触发后,并且service,Characteristic都初始化好后,发送指令给蓝牙硬件设备(即writeCharacteristic)表示手机端已经准备好,可以发送数据给我了,蓝牙硬件设备收到后再发送数据,这样能很好的保证数据不丢失。
3) 最好的办法是1)和 2)的结合,即发送准备好的指令,然后让智能硬件发送数据,然后在接收数据的过程中,使用ACK机制确保数据没有任何丢失。

4… Read/Write Characteristic/Descriptor 等都是异步的,即立即返回,等待回调。因此如果Android手机底层自身如果没有做请求的同步顺序执行的话,那么当有很多请求几乎同时进行时,回调顺序是无法保证的。此时就造成错误,这也会导致很多蓝牙4.0不能兼容某些Android的原因,因此需要自己提供一套同步机制,如RequestQueue,来保证request&response 一个接一个高效有序的进行,即下一个request必须等到上一个request的response返回之后再执行。

5… Read/Write Characteristic/Descriptor/RemoteRssi(),一般在不同的线程中回调。(除了onDescriptorWrite返回的线程与写入线程为同一个线程???)

BluetoothDevice.conncectGatt(),
BluetoothGatt.connect(),
BluetoothGatt.disconnect(),
BluetoothGatt.discoverServices()
最好都在主线程,否则会遇到很多意想不到的麻烦。

6… BLE的特征一次读写最大长度20字节。

7… Android手机会对连接过的BLE设备的Services进行缓存,若设备升级后Services等有改动,则程序会出现通讯失败。此时就得刷新缓存,反射调用BluetoothGatt类总的refresh()方法。

8… startLeScan(UUID[], LeScanCallback)在Android 4.4及以下手机中似乎只支持16位的短UUID,不支持128位。

9… connectGatt() 在某些三星手机上只能在UI线程调用。

10… Android L 新API扫描设备换为 startScan(List, ScanSettings, ScanCallback)。

11… Android M 必须拥有定位权限才能扫描BLE设备。

12… 一个主设备(例如Android手机)可以同时连接多个从设备(一般为6个,例如智能硬件。超过就连接不上了),一个从设备只能被一个主设备连接,一旦从设备连接上主设备,就停止广播,断开连接则继续广播。在任何时刻都只能最多一个设备在尝试建立连接。如果同时对多个蓝牙设备发起建立Gatt连接请求。如果前面的设备连接失败了,则后面的设备请求会被永远阻塞住,不会有任何连接回调。所以建议:如果要对多个设备发起连接请求,最好是一个接一个的顺序同步请求管理。

13… 任何出错,超时,用完就马上调用Gatt.disconnect(), Gatt.close()。

14… 从bindService 到 onServiceConnected 这个回调花费时间较长, onServiceConnected 这个回调很可能在 MainActivity onResume之后才执行, 所以不要指望onResume里去执行扫描,因为此时serviceConnected 回调都尚未执行

15… getBtAdapter().enable()是异步,立即返回,但从 off 到 on 的过程需要一个时间所以只能监听系统broadcast发出的intent里的state

16… onCharacteristicWrite … 等等是指本机写数据指令已经成功发送出去,并且智能硬件已经处理完回应回来了,另外,当智能硬件端要求发送的指令有顺序的话,那么这边不能发送速度过快,即不能在onCharacteristicWrite里立即发送下一条指令。例如OAD/OTA等等,字节必须严格按照image的字节顺序发送出去。
17… 在writeCharacteristic时,若速度过快(例如在OAD时),会发现发送出去的数据有可能不是你自己真正发出去的,在onCharacteristicWrite里打印出可以确定。
18… App端的关于同一个UUID的2个指令不能同时发出去,这样会导致硬件端无法辨识,所以需要串行发送,即等其中一个发送回调成功之后,再进行下一个。

19… 多次扫描蓝牙,在华为荣耀,魅族M3 NOTE 中有的机型,会发现多次断开–扫描–断开–扫描… 会扫描不到设备,此时需要在断开连接后,不能立即扫描,而是要先停止扫描后,过2秒再扫描才能扫描到设备。

20… 扫描尽量不要放在主线程进行,可以放入子线程里。不然有些机型会出现 do too many work in main thread.

作者:qingtiantianqing
来源:CSDN
原文:Android蓝牙4.0 BLE开发坑总结_qingtiantianqing的博客-CSDN博客
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(硬件,编程,嵌入式硬件,android)