FastBle:
BLE蓝牙开发框架
https://github.com/Jasonchenlijian/FastBle
蓝牙的种类
蓝牙分经典蓝牙和低功耗蓝牙,这里介绍的是低功耗蓝牙也就是BLE蓝牙开发。
初始化
Application.class
BleManager.getInstance().init(getApplication());
BleManager.getInstance()
.enableLog(true)
.setReConnectCount(1, 5000)//重连次数以及间隔时间
.setSplitWriteNum(20)//拆分写入数
.setConnectOverTime(10000)//连接超时时间
.setOperateTimeout(5000);//操作超时时间
搜索
val scanRuleConfig = BleScanRuleConfig.Builder()
// .setDeviceName(true, DEVICE_NAME) // 只扫描指定广播名的设备,可选
.setAutoConnect(true) // 连接时的autoConnect参数,可选,默认false
.setScanTimeOut(10000) // 扫描超时时间,可选,默认10秒
.build()
BleManager.getInstance().initScanRule(scanRuleConfig)
BleManager.getInstance().scan(object : BleScanCallback() {
override fun onScanFinished(scanResultList: MutableList<BleDevice>?) {
//搜索完成,scanResultList搜索到的所有设备
}
override fun onScanStarted(success: Boolean) {
//开始搜索
}
override fun onScanning(bleDevice: BleDevice?) {
//bleDevice:搜索到的设备
if (bleDevice?.name == DEVICE_NAME) {
//搜索到对应设备后停止搜索并连接
stopScan()
BleManager.getInstance().connect(bleDevice, bleGattCallback)
}
}
});
//停止搜索
private fun stopScan() {
if (BleManager.getInstance().scanSate == BleScanState.STATE_SCANNING) {//判断是否正在搜索
BleManager.getInstance().cancelScan()
}
}
连接
BleManager.getInstance().connect(bleDevice, bleGattCallback)
private val bleGattCallback = object : BleGattCallback() {
override fun onStartConnect() {
//开始连接
}
override fun onDisConnected(isActiveDisConnected: Boolean,device: BleDevice?,gatt: BluetoothGatt?,status: Int) {
//断开连接
}
override fun onConnectSuccess(bleDevice: BleDevice?, gatt: BluetoothGatt?, status: Int) {
//连接成功
}
override fun onConnectFail(bleDevice: BleDevice?, exception: BleException?) {
//连接失败
}
}
之前连接过的设备可以把设备mac储存起来,下次直接调用下面代码可以直接连接,速度会比重新搜索连接快很多
val deviceMac = SharedUtil.read(SP_DEVICE_MAC, null)
if (deviceMac != null) {
val device = BleManager.getInstance().bluetoothAdapter.getRemoteDevice(deviceMac)
if (device != null && device.name == DEVICE_NAME) {//判断是否是正确的设备
stopScan()
BleManager.getInstance().connect(BleDevice(device), bleGattCallback)
}
}
获取通知
设备连接成功后,调用下面代码即可创建通知的监听,其中如果获取到的Byte数组过大时,返回的数据是进行分包处理的,需要自行将其连接才是一段完整的数据
BleManager.getInstance().notify(BleDevice bleDevice,
String uuid_service,
String uuid_notify,
BleNotifyCallback callback)
这里我的设备的uuid_service为0000ffe0-0000-1000-8000-00805f9b34fb
uuid_notify为0000ffe1-0000-1000-8000-00805f9b34fb
uuid_write为0000ffe2-0000-1000-8000-00805f9b34fb
但是可能有的设备不一样,这时候可以调用gatt!!.services
获取到所有服务,查找到你所需要的uuid_service,uuid_notify则可以通过 gatt!!.getService(uuid_service).characteristics
里获取
其中有一个坑是在FastBle框架中,创建通知监听的时候会出现this characteristic not support notify!
这个错误,这个是因为框架内部过早调用gatt.discoverServices()
后导致gatt.getService()
时为空导致的。所以下面中,我会先进行判空,如果为空的话则延迟一秒调用gatt.discoverServices()
。之后内部会再次调用BleGattCallback中onConnectSuccess回调,在确保service不为空才进行调用BleManager.getInstance().notify()
//在BleGattCallback中onConnectSuccess回调调用
if (gatt!!.getService(UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb")) == null) {
//判断service是否为空
Handler(Looper.getMainLooper()).postDelayed({
gatt.discoverServices()
}, 1000)
return
}
//获取通知
BleManager.getInstance()
.notify(
bleDevice,
"0000ffe0-0000-1000-8000-00805f9b34fb",//uuidService
"0000ffe1-0000-1000-8000-00805f9b34fb",//uuidNotify
object : BleNotifyCallback() {
override fun onCharacteristicChanged(data: ByteArray?) {
//获取到的数据
if (data != null) {
getResult(data)
}
}
override fun onNotifyFailure(exception: BleException?) {
//错误
}
override fun onNotifySuccess() {
//成功
}
})
其他
表明、写入、读取也是类似的调用,这里就不详细写了
//表明
BleManager.getInstance().indicate(
BleDevice bleDevice,
String uuid_service,
String uuid_indicate,
BleIndicateCallback callback)
//写入
BleManager.getInstance().write(
BleDevice bleDevice,
String uuid_service,
String uuid_write,
byte[] data,
BleWriteCallback callback)
//读取
BleManager.getInstance().read(
BleDevice bleDevice,
String uuid_service,
String uuid_read,
BleReadCallback callback)
释放资源
BleManager.getInstance().destroy()