一步一步实现Android低功耗蓝牙(BLE)基本开发

项目需要接入两个低功耗蓝牙设备(BLE),并且与之交互(读/写)数据,所以看了下官方对于这块儿的介绍,总结了一下BLE开发中一些需要注意的地方以及基本流程。

BLE开发需要Android 4.3 (API level 18) 及以上

一.添加权限 为了能正常使用蓝牙相关功能(扫描等),首先需要添加以下权限:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
复制代码

在Android6.0及以上系统中,我们需要动态申请权限,这里推荐使用RxPermissions

简单介绍下RxPermissions如何引入。

1.在根build文件中添加代码:

...
allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
...
复制代码

2.在对应moudle的build文件中添加依赖:

...
dependencies {
    implementation 'com.github.tbruyelle:rxpermissions:0.10.2'
}
...
复制代码

3.使用:

RxPermissions rxPermissions=new RxPermissions(this);
rxPermissions.request(Manifest.permission.BLUETOOTH,
                Manifest.permission.BLUETOOTH_ADMIN,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION)
                .subscribe(granted -> {
                    if (granted) {
                        //权限允许成功
                    }
                });
复制代码

如果想了解RxPermissions更多用法,戳这里

二.判断设备是否支持蓝牙 这里有两种处理方式:

  • 如果你想让只有支持BLE的手机才能安装你的应用程序的话,可以在清单文件中添加如下内容,这样的话如果设备不支持BLE的话你的应用都装不上,当然这种方式不太友好:
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
复制代码
  • 在代码中判断当前设备是否支持BLE,以对用户做出反馈。 首先,在清单文件中声明需要使用BLE特性,不过required这里设置为false,然后在app运行时通过 PackageManager.hasSystemFeature()来判断设备是否支持ble:
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

复制代码
// Use this check to determine whether BLE is supported on the device. Then
// you can selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
    Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
    finish();
}
复制代码

三.扫描蓝牙设备 BLE设备的扫描由BluetoothManager对象提供方法来实现,有两个扫描方法:


    public boolean startLeScan(BluetoothAdapter.LeScanCallback callback) {
        throw new RuntimeException("Stub!");
    }

    public boolean startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback) {
        throw new RuntimeException("Stub!");
    }
复制代码

第二个方法允许我们提供特定的UUID,来扫描特定的设备,扫描结果通过BluetoothAdapter.LeScanCallback接口回调给我们:

 public interface LeScanCallback {
        /**
         * Callback reporting an LE device found during a device scan initiated
         * by the {@link BluetoothAdapter#startLeScan} function.
         *
         * @param device Identifies the remote device
         * @param rssi The RSSI value for the remote device as reported by the
         *             Bluetooth hardware. 0 if no RSSI value is available.
         * @param scanRecord The content of the advertisement record offered by
         *                   the remote device.
         */
        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord);
    }
复制代码

四.获取远程BLE设备 在扫描出设备以后,我们一般会选择某个扫描出来的设备,通过其地址获取一个远程的蓝牙设备对象。

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address)
复制代码

五.连接BLE设备的GATT服务 与BLE设备交互的第一步是连接到它,更具体地说,连接到设备上的GATT服务。要在BLE设备上连接到GATT服务,可以使用connectGatt()方法。该方法接受三个参数:一个上下文对象、autoConnect(布尔值表示是否在BLE设备可用时自动连接到该设备),以及对BluetoothGattCallback的引用:

mBluetoothGatt = device.connectGatt(context, true, mGattCallback);
复制代码

以上代码可以连接到由BLE设备托管的GATT服务,并返回一个BluetoothGatt实例,然后可以使用它来执行GATT客户端操作,例如写数据等。呼叫者(Android应用程序)是GATT客户端。连接状态,以及GATT的数据变化等通过BluetoothGattCallback接口回调给客户端(APP)。

一般使用BluetoothGattCallback的这些回调方法:

1.获取连接状态,在连接成功时扫描设备服务

@Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                if (connectChangedListener != null) {
                    connectChangedListener.onConnected();
                }
                mConnectionState = STATE_CONNECTED;
                mBluetoothGatt.discoverServices();

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                if (connectChangedListener != null) {
                    connectChangedListener.onDisconnected();
                }
                mConnectionState = STATE_DISCONNECTED;
            }
        }

复制代码

2.获取服务,特性等 一个BLE设备可能有多个服务BluetoothGattService,同样每个服务可以有多个BluetoothGattCharacteristic特性。

@Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                List<BluetoothGattService> services = mBluetoothGatt.getServices();
                for (int i = 0; i < services.size(); i++) {
                    HashMap<String, BluetoothGattCharacteristic> charMap = new HashMap<>();
                    BluetoothGattService bluetoothGattService = services.get(i);
                    String serviceUuid = bluetoothGattService.getUuid().toString();
                    List<BluetoothGattCharacteristic> characteristics = bluetoothGattService.getCharacteristics();
                    for (int j = 0; j < characteristics.size(); j++) {
                        charMap.put(characteristics.get(j).getUuid().toString(), characteristics.get(j));
                    }
                    servicesMap.put(serviceUuid, charMap);
                }
                BluetoothGattCharacteristic bluetoothGattCharacteristic = getBluetoothGattCharacteristic(UUID_SERVICE, UUID_CHARACTERISTIC);
                if (bluetoothGattCharacteristic == null)
                    return;
                enableGattServicesNotification(bluetoothGattCharacteristic);
            } else {
                Log.w(TAG, " --------- onServicesDiscovered received: " + status);
            }
        }
复制代码

在上面的代码中,我们将BLE设备的所有BluetoothGattServiceBluetoothGattCharacteristic全部保存下来,但是在实际需求中,我们一般只会与某个特定BluetoothGattService中的某个特性BluetoothGattCharacteristic进行数据读写。判断条件就是这里的UUID_SERVICEUUID_CHARACTERISTIC,这两个UUID一般提供BLE设备的时候会一并提供给我们。

找到这个特定的BluetoothGattCharacteristic后,我们希望它发生改变时可以得到通知,可以使用setCharacteristicNotification()方法为特性设置通知:

BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(UUID_DESCRIPTOR));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
复制代码

3.监听数据变化 经过以上设置,我们就可以在onCharacteristicChanged回调方法中获取BLE设备发过来的数据了:

@Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
	        //解析数据
            parseData(characteristic);
        }
复制代码

当然,我们也可以用第五步中获取的mBluetoothGatt来向BLE设备发送数据:

mBleGattCharacteristic.setValue(HexUtil.hexStringToBytes(value));
boolean b=mBluetoothGatt.writeCharacteristic(mBleGattCharacteristic);

复制代码

以上,就是Android端与BLE设备通信的基本开发流程,这里我抽成了一个Demo,项目目录如下:

几点说明:

  • 因为我这里需求是接入两个BLE设备,所以我抽取了一个BluetoothLeDeviceBase,代表基类设备,将一些通用的属性和操作封装在了这里
  • BluetoothLeDeviceA,BluetoothLeDeviceB代表具体的某个BLE设备,每个设备可能有不同之处,例如数据解析方式等。

完整代码地址:github.com/SolveBugs/B… 目前只是基本的封装,后续会继续完善。

选择bluetoothbledemo这个moudle运行即可,界面如下:

猜你喜欢

转载自juejin.im/post/5b9a3e1fe51d450e72108755