BluetoothKit是一款功能强大的Android蓝牙通信框架,支持经典蓝牙和低功耗蓝牙设备混合扫描,提供了一系列简单易用的接口用于低功耗蓝牙设备的连接,数据读写,通知等。

项目地址: https://github.com/dingjikerbo/BluetoothKit

特点

一、支持经典蓝牙和BLE蓝牙混合扫描,支持自定义扫描策略
作为智能家居平台,接入的设备包括经典蓝牙和BLE,因此扫描设备时需要两者混合进行,而设备扫描场景不同,扫描策略也会不一样,因此需要支持扫描策略可配置。

二、充分解决了Android中BLE兼容性和稳定性问题
Android系统对蓝牙4.0支持得并不完善,有许多bug, BluetoothKit很好地解决了其中大部分已知的问题。
关于Android BLE设备连接的各种坑可以参考 ——Android 4.3 Bluetooth Low Energy unstable

三、简单易用,接口简洁明了
BluetoothKit采用异步串行化策略处理所有设备操作,并支持任务超时及出错重试。

技术

一、实现了一个完整的跨进程异步任务队列,支持任务超时、出错重试及防御队列溢出

二、拦截并Hook系统层蓝牙Binder,实现对所有蓝牙设备通信的监控,当同时连接设备数过多时会自动断掉活跃度最低的设备

三、整个框架封装在一个service中,可灵活指定service所在进程。通过client与service通信,client可源于多个不同进程,因此适用于多进程架构的app

四、屏蔽了接口异步回调可能持有调用端Activity引用导致的内存泄露

五、利用动态代理自动将所有操作封闭在工作线程,所以整个框架无锁

使用

首先,需要按如下方式初始化BluetoothClient:

BluetoothClient mClient = BluetoothClient.getInstance(context);

扫描设备

支持经典蓝牙和BLE设备混合扫描,可自由定制扫描策略如下:

SearchRequest request = new SearchRequest.Builder()
        .searchBluetoothLeDevice(3000, 3)   // scan Bluetooth LE device for 3000ms, 3 times
        .searchBluetoothClassicDevice(5000) // then scan Bluetooth Classic device for 5000ms, 1 time
        .searchBluetoothLeDevice(2000)      // at last scan Bluetooth LE device for 2000ms
        .build();

mClient.search(request, new SearchResponse() {
    @Override
    public void onSearchStarted() {

    }

    @Override
    public void onDeviceFounded(SearchResult device) {

    }

    @Override
    public void onSearchStopped() {

    }

    @Override
    public void onSearchCanceled() {

    }
});

停止蓝牙扫描

mClient.stopSearch();

BLE设备连接

1. 连接
mClient.connect(MAC, new BleConnectResponse() {
    @Override
    public void onResponse(int code, Bundle data) {
        if (code == REQUEST_SUCCESS) {
            
        }
    }
});
2. 断开连接
mClient.disconnect(MAC);
3. 读设备
mClient.read(MAC, serviceUUID, characterUUID, new BleReadResponse() {
    @Override
    public void onResponse(int code, byte[] data) {
        if (code == REQUEST_SUCCESS) {

        }
    }
});
4. 写设备
mClient.write(MAC, serviceUUID, characterUUID, bytes, new BleWriteResponse() {
    @Override
    public void onResponse(int code) {
        if (code == REQUEST_SUCCESS) {

        }
    }
});
5. 打开设备通知

这里打开通知的回调是onResponse,接收通知的回调是onNotify。

mClient.notify(MAC, serviceUUID, characterUUID, new BleNotifyResponse() {
    @Override
    public void onNotify(UUID service, UUID character, byte[] value) {
        
    }

    @Override
    public void onResponse(int code) {
        if (code == REQUEST_SUCCESS) {

        }
    }
});
6. 关闭设备通知
mClient.unnotify(MAC, serviceUUID, characterUUID, new BleUnnotifyResponse() {
    @Override
    public void onResponse(int code) {
        if (code == REQUEST_SUCCESS) {

        }
    }
});
7. 读取Rssi
mClient.readRssi(MAC, new BleReadRssiResponse() {
    @Override
    public void onResponse(int code, Integer rssi) {
        if (code == REQUEST_SUCCESS) {

        }
    }
});