蓝牙管理库
2.1功能概述
当您应用一个未连接的绑定设备时,您需要创建一个视图控制器来实现SDK蓝牙代理。扫描外围设备,代理将返回设备集合,在列表中显示,选择需要连接的设备,连接成功后返回设备信息以及设备是否处于OTA模式,连接失败则会有一个错误回调。默认扫描信号过滤参数为80,自动扫描连接超时时间为20秒。蓝牙管理库(IDOBluetooth)是依赖蓝牙协议库(IDOBlueProtocol),所以在使用蓝牙管理库前,先导入蓝牙协议库。
2.2扫描连接流程图
2.3蓝牙初始化
Objc:
[IDOBluetoothManager shareInstance].rssiNum = 100;
[IDOBluetoothManager shareInstance].delegate = self;
Swift:
IDOBluetoothManager.shareInstance()?.rssiNum = 100;
IDOBluetoothManager.shareInstance()?.delegate = self;
2.4参数说明
参数 | 说明 | 备注 |
---|---|---|
timeout | 自动扫描连接超时时长 | 默认20 |
rssiNum | 设置扫描过滤信号 | 默认值80 |
isIntervalScan | 是否启动超时间隔扫描 | 默认yes |
isReconnect | 是否需要重连机制 | 默认yes |
autoScanInterval | 设置扫描间隔时长 | 默认10秒 |
isMandatoryManual | 强制切换手动模式扫描连接 | 当前已经绑定设备再次添加设备 |
scanMode | 蓝牙扫描设备模式 | readonly |
state | 蓝牙管理连接状态 | readonly |
errorCode | 蓝牙连接错误码 | readonly |
manualConnectTotalTime | 手动点击连接设备的总时长 | readonly |
autoConnectTotalTime | 自动扫描到成功连接设备的总时长 | readonly |
2.5基础方法
Objc:
//开始扫描
[IDOBluetoothManager startScan];
//刷新蓝牙代理
[IDOBluetoothManager refreshDelegate];
//停止扫描
[IDOBluetoothManager stopScan];
//连接设备
[IDOBluetoothManager connectDeviceWithModel:model];
//断开当前外围设备的连接
[IDOBluetoothManager cancelCurrentPeripheralConnection];
Swift:
//开始扫描
IDOBluetoothManager.startScan();
//刷新蓝牙代理
IDOBluetoothManager.refreshDelegate();
//停止扫描
IDOBluetoothManager.stopScan();
//连接设备
IDOBluetoothManager .connectDevice(with: model as? IDOPeripheralModel);
//断开当前外围设备的连接
IDOBluetoothManager.cancelCurrentPeripheralConnection();
2.6代理方法
Objc:
<IDOBluetoothManagerDelegate>
- (BOOL)bluetoothManager:(IDOBluetoothManager *)manager
centerManager:(CBCentralManager *)centerManager
didConnectPeripheral:(CBPeripheral *)peripheral
isOatMode:(BOOL)isOtaMode
{
//设备连接成功回调
return YES;
}
- (void)bluetoothManager:(IDOBluetoothManager *)manager
allDevices:(NSArray<IDOPeripheralModel *> *)allDevices
otaDevices:(NSArray<IDOPeripheralModel *> *)otaDevices
{
//扫描设备实时返回设备集合回调
}
- (void)bluetoothManager:(IDOBluetoothManager *)manager
didUpdateState:(IDO_BLUETOOTH_MANAGER_STATE)state
{
//扫描连接状态回调
}
- (void)bluetoothManager:(IDOBluetoothManager *)manager
connectPeripheralError:(NSError *)error
{
//扫描连接错误回调
}
Swift:
IDOBluetoothManagerDelegate
func bluetoothManager(_ manager: IDOBluetoothManager!, connectPeripheralError error: Error!) {
//扫描连接错误回调
};
func bluetoothManager(_ manager: IDOBluetoothManager!, didUpdate state: IDO_BLUETOOTH_MANAGER_STATE) {
//扫描连接状态回调
};
func bluetoothManager(_ manager: IDOBluetoothManager!, allDevices: [IDOPeripheralModel]!, otaDevices: [IDOPeripheralModel]!) {
//扫描设备实时返回设备集合回调
};
func bluetoothManager(_ manager: IDOBluetoothManager!, center centerManager: CBCentralManager!, didConnect peripheral: CBPeripheral!, isOatMode isOtaMode: Bool) -> Bool {
//设备连接成功回调
return true;
};
2.7蓝牙通知
1.IDOBluetoothConnectStateNotifyName
蓝牙扫描连接状态监听通知名字,一般情况全局监听此通知,当蓝牙状态发生变化时抛出通知,修改对应的UI交互。
2.IDOBluetoothConnectErrorNotifyName
蓝牙扫描连接错误监听通知名字,一般情况全局监听此通知,当蓝牙发生错误时抛出通知,修改对应的UI交互。
3.IDOBluetoothOtaModeNotifyName
蓝牙连接手环为OTA模式监听通知名字,一般情况全局监听此通知,当蓝牙连接后发现当前手环为OTA模式时抛出通知,根据业务进行手环升级操作。