设备绑定解绑
4.1功能概述
第一次连接设备时,需要执行绑定命令,根据绑定命令返回的状态类型,授权绑定和非授权绑定,授权绑定需要输入手环显示的验证码才能绑定成功,非授权绑定不需要确认可直接绑定。设备解绑有连接解绑和断开解绑,连接时解绑是App发送解绑命令实现双方解绑,同时会抹掉手环上的所有数据,断开时解绑是App单方修改绑定状态实现解绑,不会抹掉手环上的数据。
4.2绑定命令
Objc:
IDOSetBindingInfoBluetoothModel * model = [[IDOSetBindingInfoBluetoothModel alloc]init];
[IDOFoundationCommand bindingCommand:model waitForSure:^{
//绑定等待的过程
} callback:^(IDO_BIND_STATUS status, int errorCode) {
if (errorCode == 0) {
if (status == IDO_BLUETOOTH_BIND_SUCCESS) {
//绑定成功
}else if (status == IDO_BLUETOOTH_BINDED) {
//已经绑定
}else if (status == IDO_BLUETOOTH_BIND_FAILED) {
//绑定失败
}else if (status == IDO_BLUETOOTH_NEED_AUTH) {
//需要授权绑定
}else if (status == IDO_BLUETOOTH_REFUSED_BINDED) {
//拒绝绑定
}
}else {
//绑定失败
}
}];
//如果需要授权绑定,执行下面的代码
IDOSetBindingInfoBluetoothModel * model = [IDOSetBindingInfoBluetoothModel currentModel];
//输入授权码
model.authCode = authCode;
[IDOFoundationCommand setAuthCodeCommand:model callback:^(int errorCode) {
if (errorCode == 0) {
//授权绑定成功
}else {
//授权绑定失败
}
}];
Swift:
let model = IDOSetBindingInfoBluetoothModel.init();
IDOFoundationCommand.bindingCommand(model, callback: {(state,errorCode) in
if errorCode == 0 {
if state == IDO_BIND_STATUS.BLUETOOTH_BIND_SUCCESS {
//绑定成功
}else if state == IDO_BIND_STATUS.BLUETOOTH_BINDED {
// 已经绑定
}else if state == IDO_BIND_STATUS.BLUETOOTH_BIND_FAILED {
// 绑定失败
}else if state == IDO_BIND_STATUS.BLUETOOTH_NEED_AUTH {
// 授权绑定
}else if state == IDO_BIND_STATUS.BLUETOOTH_REFUSED_BINDED {
// 拒绝绑定
}
}else {
//绑定失败
}
});
//如果需要授权绑定,执行下面的代码
let currentModel = IDOSetBindingInfoBluetoothModel.current();
currentModel?.authCode = authCode;
IDOFoundationCommand.setAuthCode(currentModel, callback: {(errorCode) in
if errorCode == 0 {
//授权绑定成功
}else {
//授权绑定失败
}
});
4.3解绑命令
Objc:
/*
此方法在连接状态时,执行成功回调,属于双方解绑,并删除手环数据;
在断开状态时,执行成功回调,App单方解绑,并不会删除手环数据;
*/
[IDOFoundationCommand mandatoryUnbindingCommand:^(int errorCode) {
if (errorCode == 0) {
//解绑成功
}else {
//解绑失败
}
}];
Swift:
/*
此方法在连接状态时,执行成功回调,属于双方解绑,并删除手环数据;
在断开状态时,执行成功回调,App单方解绑,并不会删除手环数据;
*/
IDOFoundationCommand.mandatoryUnbindingCommand { (errorCode) in
if errorCode == 0 {
//解绑成功
}else {
//解绑失败
}
};
4.4 切换设备
Objc:
//传入的Mac地址是已经绑定过的设备的Mac地址
[IDOFoundationCommand switchDeviceCommand:model.macAddr
isMandatory:YES //NO: 只有绑定的设备才能切换,YES: 未绑定的设备也能切换
callback:^(int errorCode) {
if (errorCode == 0) {
/**
成功初始化切换设备信息
蓝牙需要设置重连=>YES
连接鉴权设置=>NO
*/
[IDOBluetoothManager shareInstance].isReconnect = YES;
[IDOBluetoothManager shareInstance].isDetectionAuthCode = NO;
if (!__IDO_CONNECTED__) {//未连接则执行扫描
[IDOBluetoothManager startScan];
}else {//已连接则断开连接,启动自动扫描连接
[IDOBluetoothManager cancelCurrentPeripheralConnection];
}
}else {
//切换失败
}
}];
//监听设备连接回调,连接失败则切换设备失败,连接成功则执行下面命令
/**
切换设备连接成功,需要检测设备加密授权码,如果当前设备不支持鉴权,则返回错误码6,
请按执行正常完成绑定操作。
*/
[IDOFoundationCommand switchDeviceDetectionEncryptionAuthCallback:^(int errorCode) {
if(errorCode == 0) { //鉴权设备切换成功
}else if(errorCode == 6){ //普通设备切换成功
[IDOFoundationCommand swithOrdinaryDeviceComplete];
}else if(errorCode == 1041) {
//授权码不一致=>设备被其他手机绑定
}else if(errorCode == 1035) {
//设备没有绑定
}else { //失败
}
}];
Swift:
//传入的Mac地址是已经绑定过的设备的Mac地址
IDOFoundationCommand.switchDeviceCommand("", isMandatory: true) { (errorCode) in
if errorCode == 0 {
/**
成功初始化切换设备信息
蓝牙需要设置重连=>YES
连接鉴权设置=>NO
*/
IDOBluetoothManager.shareInstance().isReconnect = true
IDOBluetoothManager.shareInstance().isDetectionAuthCode = false
if !IDOBluetoothEngine.shareInstance().managerEngine.isConnected {
//未连接则执行扫描
IDOBluetoothManager.startScan()
}else {
//已连接则断开连接,启动自动扫描连接
IDOBluetoothManager.cancelCurrentPeripheralConnection()
}
}else {
//切换失败
}
}
//监听设备连接回调,连接失败则切换设备失败,连接成功则执行下面命令
/**
切换设备连接成功,需要检测设备加密授权码,如果当前设备不支持鉴权,则返回错误码6,
请按执行正常完成绑定操作。
*/
IDOFoundationCommand.switchDeviceDetectionEncryptionAuthCallback { (errorCode) in
if errorCode == 0 {
//鉴权设备切换成功
}else if errorCode == 6 {
//普通设备切换成功
IDOFoundationCommand.swithOrdinaryDeviceComplete()
}else if errorCode == 1041 {
//授权码不一致=>设备被其他手机绑定
}else if errorCode == 1035 {
//设备没有绑定
}else {
//失败
}
}
4.5 Demo解绑功能入口