蓝牙库(仅限full版)
功能概述
基于原生平台蓝牙库封装,用于扫描外围设备,获取返回设备集合,在列表中显示,选择需要连接的设备,连接成功后返回设备信息以及设备是否处于OTA模式,连接失败则会有一个错误回调。默认扫描信号过滤参数为80,自动扫描连接超时时间为20秒。(注意:仅限full版使用)
方法
addBleDelegate(api:)
添加蓝牙代理 IDOBleDelegate
addDfuDelegate(api:)
添加DFU升级 (nordic)
bluetoothRegister(heartPingSecond:outputToConsole:)
注册,程序开始运行调用
delegate 代理 heartPingSecond:心跳包间隔(ios) outputToConsole:控制台输出日志
startScan(macAddress:completion:)
开始搜索 macAddress(Android):根据Mac地址搜索 返回指定搜索的设备,如未指定返回null
scanFilter(deviceName:deviceID:macAddress:uuid:)
搜索筛选条件
deviceName: 只搜索deviceName的设备
deviceID:只搜索deviceID的设备
macAddress:只搜索macAddress的设备
uuid:只搜索uuid的设备
stopScan()
停止搜索
connect(device:)
连接 device: Mac地址必传,iOS要带上uuid,最好使用搜索返回的对象
autoConnect(device:)
使用这个重连设备
cancelConnect(macAddress:completion:)
取消连接
getBluetoothState(completion:)
获取蓝牙状态
getDeviceState(device:completion:)
获取设备连接状态
writeData(data:device:type:completion:)
发送数据
data:数据
device: 发送数据的设备
type:0 BLE数据, 1 SPP数据
platform: 0 爱都, 1 恒玄, 2 VC
setBtPair(device:)
bt配对(android)
cancelPair(device:)
取消配对(android)
connectSPP(btMacAddress:)
连接SPP(android)
disconnectSPP(btMacAddress:)
断开SPP(android)
startNordicDFU(config:)
发起dfu升级
exportLog(completion:)
导出ble日志,返回压缩后日志zip文件绝对路径
示例
注册Bluetooth模块
Swift:
// 注册蓝牙库
sdk.ble.addBleDelegate(api: <IDOBleDelegate>)
sdk.ble.bluetoothRegister(heartPingSecond: 5, outputToConsole: false)
sdk.ble.getBluetoothState { [weak self] stateModel in
// 状态监听
}
Kotlin:
//注册蓝牙库
sdk.ble.addBleDelegate(IDOBleDelegate)
sdk.ble.bluetoothRegister(false)
sdk.ble.getBluetoothState {
//状态监听
}
ArkTs:
sdk.ble.addBleDelegate(IDOBleDelegate)
sdk.ble.getBluetoothState((state)=>{
//状态监听
})
代理实现
Swift:
// 实现代理
extension SomeClass: IDOBleDelegate {
func scanResult(list: [IDODeviceModel]?) {
// 扫描设备列表
print("scanResult list count:\(String(describing: list?.count))")
}
func bluetoothState(state: IDOBluetoothStateModel) {
// 蓝牙状态
print("on bluetoothState callback: \(String(describing: state.scanType?.rawValue))")
}
func deviceState(state: IDODeviceStateModel) {
// 设备状态
print("on deviceState callback: \(String(describing: state.state))")
//如果你接入的设备支持配对加密,则需要处理以下两种情况,示例如下
if (deviceState.errorState == IDOConnectErrorType.deviceAlreadyBindAndNotSupportRebind) {//1.设备不这次重复绑定,需要先重置设备
let alert = UIAlertController(title: "Note",
message: "The watch has been bound and does not support repeated binding, First reset the watch on the watch side!",
preferredStyle: UIAlertController.Style.alert)
let ok = UIAlertAction(title: "Got it", style: UIAlertAction.Style.default) { action in
self.dismiss(animated: true)
}
alert.addAction(ok)
present(alert, animated: true, completion: nil)
} else if (deviceState.errorState == IDOConnectErrorType.deviceHasBeenReset) {//2.设备已经重置了,需要删除本地设备信息
let alert = UIAlertController(title: "Note",
message: "The watch has been reset, pls remove the watch!",
preferredStyle: UIAlertController.Style.alert)
let delete = UIAlertAction(title: "Remove it",
style: UIAlertAction.Style.default) { action in
SVProgressHUD.show(withStatus: "unbind...")
guard let macAddress = deviceState.macAddress else {
SVProgressHUD.showSuccess(withStatus: "macAddress is nil")
return
}
//您可以在这里删除 app 端的设备,示例如下
sdk.cmd.unbind(macAddress: macAddress , isForceRemove: true, completion: { [weak self] rs in
//释放资源
if rs {
SVProgressHUD.showSuccess(withStatus: "unbind successful")
} else {
SVProgressHUD.showError(withStatus: "unbind failure")
}
UserDefaults.standard.setBind(macAddress, isBind: false)
UserDefaults.standard.synchronize()
self?.dismiss(animated: true)
})
}
alert.addAction(delete)
present(alert, animated: true, completion: nil)
}
}
}
Kotlin:
// 实现代理
private val bleDelegate = object : IDOBleDelegate {
override fun scanResult(list: List<IDOBleDeviceModel>?) {
//扫描设备列表
}
override fun bluetoothState(state: IDOBluetoothStateModel) {
//蓝牙状态
}
override fun deviceState(state: IDODeviceStateModel) {
// 设备状态
//如果你接入的设备支持配对加密,则需要处理以下两种情况,示例如下
if (idoDeviceStateModel.errorState == IDOConnectErrorType.DEVICEALREADYBINDANDNOTSUPPORTREBIND) {
//device already bind, and not support rebind
AlertDialog.Builder(this@FunctionActivity).setTitle("Note")
.setMessage("The watch has been bound and does not support repeated binding, First reset the watch on the watch side!").setPositiveButton("Got it", null)
.show()
} else if (idoDeviceStateModel.errorState == IDOConnectErrorType.DEVICEHASBEENRESET) {
//The watch has been reset, pls remove the watch!
AlertDialog.Builder(this@FunctionActivity).setTitle("Note")
.setMessage("The watch has been reset, pls remove the watch!").setPositiveButton("Remove it", { dialog, which ->
//您可以在这里删除 app 端的设备,示例如下
sdk.cmd.unbind(device?.macAddress ?: "", true, {
if (it) {
//释放资源
sdk.ble.cancelPair(device);
sdk.ble.cancelConnect(device?.macAddress) {}
sdk.messageIcon.resetIconInfoData(
macAddress = device?.macAddress.toString(),
deleteIcon = true
) {}
} else {
//unbind failed
}
})
}).show()
}
}
override fun stateSPP(state: IDOSppStateModel) {
}
override fun writeSPPCompleteState(btMacAddress: String) {
}
}
ArkTs:
// 实现代理
let bleDelegate : IDOBleDelegate = {
scanResult: (list: IDOBleDeviceModel[] | null): void => {
//扫描设备列表
},
bluetoothState: (state: IDOBluetoothStateModel): void => {
//蓝牙状态
},
deviceState: (state: IDODeviceStateModel): void => {
// 设备状态
},
receiveData: (data: IDOReceiveData): void => {
//接收到的数据模型
}
}