无感解锁

更新时间:2023-09-19 03:00:52

物联网领域的电动车和汽车等出行设备都支持蓝牙功能,如果用户靠近出行设备后,能够通过蓝牙自动解锁设备,则让解锁过程变得十分方便。

与之前需要用户拿出手机,打开 App,确保设备在线,打开面板再单击解锁的流程相比,涂鸦出行 SDK 的 蓝牙无感解锁 简化了整体的解锁流程。仅需要用户通过 App 首次打开靠近解锁功能,即可下次手机与车靠近时,为用户自动打开车辆或解除设防状态。

无感解锁类型

  • 涂鸦出行 SDK 提供 BLE_HID 和 BT 两种无感解锁类型。
  • 不同出行设备支持的无感解锁类型不同。请通过查询设备支持无感解锁类型接口,获取设备无感解锁控制类。

BLE_HID

BLE_HID 无感解锁类型通过低功耗蓝牙 HID(Human Interface Device)协议实现无感解锁。

无感解锁

在设备与手机系统层 HID 服务绑定过程中,设备端由于蓝牙服务变更,会重启服务,并重新连接手机。这一过程中 App 端可以做等待提示,如果超过 15 秒未回调绑定状态,直接进行绑定状态查询。

BT

BT 无感解锁类型通过经典蓝牙配对实现无感解锁。

无感解锁

无感解锁管理类

获取无感解锁管理类

示例代码

ThingSmartODInductiveUnlock *manager = [ThingSmartODInductiveUnlock sharedInstance];

获取设备无感解锁类型

接口说明

/**
 * @brief Get the {@link InductiveUnlockType} of device
 *
 * @param devId device ID
 * @param completionBlock inductiveUnlockType
 * @param errorBlock error message
 */
- (void)getInductiveUnlockType:(NSString *)devId
                    completion:(void(^)(InductiveUnlockType type))completionBlock
                         error:(void(^)(NSError* error))errorBlock;

InductiveUnlockType 数据模型

枚举值 描述
InductiveUnlockTypeNone 不支持无感解锁
InductiveUnlockTypeBLEHID BLE_HID 类型
InductiveUnlockTypeBT BT 类型

示例代码

[[ThingSmartODInductiveUnlock sharedInstance] getInductiveUnlockType:devId completion:^(InductiveUnlockType type) {
    NSLog(@"result: %ld", type);
} error:^(NSError * _Nonnull error) {
    NSLog(@"error: %@", error.localizedDescription);
}];

监听无感解锁开关状态变化

接口说明

typedef NS_ENUM(NSInteger, UnlockStatus) {
    UnlockStatusTurnOn = 0,
    UnlockStatusTurnOff = 1,
};

@protocol ThingSmartODInductiveUnlockDelegate <NSObject>

- (void)listenUnlockStatusCallback:(UnlockStatus)status;

@end

示例代码

// set up proxy
[ThingSmartODInductiveUnlock sharedInstance].delegate = self;

// implementing the protocol
#pragma mark - ThingSmartODInductiveUnlockDelegate

- (void)listenUnlockStatusCallback:(UnlockStatus)status {
      NSLog(@"status: %ld", status);
}

BLE_HID 类型控制类

获取 BLE_HID 类型控制类

示例代码

ThingODHidInductiveUnlock *hidManager = [ThingODHidInductiveUnlock sharedInstance];

是否具有 HID 能力

用于检查是否为低功耗蓝牙 HID 设备,移除和重置已蓝牙配对的 HID 设备时需要保证设备本地在线。

接口说明

/**
 * @brief Get the {@link hid bind ability} of device
 *
 * @param devId device ID
 */
- (BOOL)supportHIDAbility:(NSString *)devId;

示例代码

BOOL hidDevice = [[ThingODHidInductiveUnlock sharedInstance] supportHIDAbility:devId];

获取无感解锁开关状态

接口说明

/**
 * @brief Get the {@link unlock status} of device
 *
 * @param devId device ID
 */
- (BOOL)getUnlockStatus:(NSString *)devId;

示例代码

BOOL unlockStatus = [[ThingODHidInductiveUnlock sharedInstance] getUnlockStatus:devId];

获取 HID 绑定状态

接口说明

/**
 * @brief Get the {@link hid bind status} of device
 *
 * @param devId device ID
 */
- (BOOL)getHidBindStatus:(NSString *)devId;

示例代码

BOOL bindStatus = [[ThingODHidInductiveUnlock sharedInstance] getHidBindStatus:devId];

开启无感解锁

开启无感解锁需要结合手机蓝牙配对进行,出行 SDK 提供了开启解锁接口和蓝牙配对监听代理方法。开启解锁接口成功回调后会进入蓝牙配对流程,代理方法会回调 HID 绑定状态。

如果用户取消配对,无感解锁状态会被关闭。

接口说明

/**
 * @brief Turn on the inductive unlock
 *
 * @param devId device ID
 * @param finishedBlock unlock result
 * @param errorBlock error message
 */
- (void)turnOnHidInductiveUnlock:(NSString *)devId
                        finished:(void(^_Nullable)(void))finishedBlock
                           error:(void(^)(NSError* error))errorBlock;

代理说明

typedef NS_ENUM(NSInteger, HidBindStatus) {
    HidBindStatusBind = 0,
    HidBindStatusUnbind = 1
};

@protocol ThingODHidInductiveUnlockDelegate <NSObject>

- (void)listenHidBindStatusCallback:(HidBindStatus)status;

@end

示例代码

// set up proxy
[ThingODHidInductiveUnlock sharedInstance].delegate = self;

// the method of turn on the HID inductive unlock
[[ThingODHidInductiveUnlock sharedInstance] turnOnHidInductiveUnlock:devId finished:^{
    NSLog(@"Enter the hid unlocking process");
} error:^(NSError * _Nonnull error) {
    NSLog(@"error: %@", error.localizedDescription);
}];

// implementing the protocol
#pragma mark - ThingODHidInductiveUnlockDelegate

- (void)listenHidBindStatusCallback:(HidBindStatus)status {
    NSLog(@"status: %ld", status);
}

如果移除代理([ThingODHidInductiveUnlock sharedInstance].delegate = nil;),listenHidBindStatusCallback 方法将不会收到回调。

关闭无感解锁

接口说明

/**
 * @brief Turn off the inductive unlock
 *
 * @param devId device ID
 * @param finishedBlock unlock result
 * @param errorBlock error message
 */
- (void)turnOffHidInductiveUnlock:(NSString *)devId
                         finished:(void(^_Nullable)(void))finishedBlock
                            error:(void(^)(NSError* error))errorBlock;

示例代码

[[ThingODHidInductiveUnlock sharedInstance] turnOffHidInductiveUnlock:devId finished:^{
    NSLog(@"Turn off successfully");
} error:^(NSError * _Nonnull error) {
    NSLog(@"error: %@", error.localizedDescription);
}];

设定设防距离

接口说明

/**
 * @brief Record Fortify Distance
 *
 * @param devId device ID
 * @param finishedBlock record result
 * @param errorBlock error message
 */
- (void)recordFortifyDistance:(NSString *)devId
                     finished:(void(^_Nullable)(void))finishedBlock
                        error:(void(^)(NSError* error))errorBlock;

示例代码

[[ThingODHidInductiveUnlock sharedInstance] recordFortifyDistance:devId finished:^{
    NSLog(@"Record successfully");
} error:^(NSError * _Nonnull error) {
    NSLog(@"Error: %@", error.localizedDescription);
}];

设定解防距离

接口说明

/**
 * @brief Record Disarm Distance
 *
 * @param devId device ID
 * @param finishedBlock record result
 * @param errorBlock error message
 */
- (void)recordDisarmDistance:(NSString *)devId
                    finished:(void(^_Nullable)(void))finishedBlock
                       error:(void(^)(NSError* error))errorBlock;

示例代码

[[ThingODHidInductiveUnlock sharedInstance] recordDisarmDistance:devId finished:^{
    NSLog(@"Record successfully");
} error:^(NSError * _Nonnull error) {
    NSLog(@"error: %@", error.localizedDescription);
}];

BT 类型控制类

获取 BT 类型控制类

示例代码

ThingODBTInductiveUnlock *btManager = [ThingODBTInductiveUnlock sharedInstance];

获取无感解锁开关状态

接口说明

/**
 * @brief Get the {@link paired status} of device
 *
 * @param devId device ID
 */
- (BOOL)checkPairedStatus:(NSString *)devId;

示例代码

[[ThingODBTInductiveUnlock sharedInstance] checkPairedStatus:device.devId];

开启无感解锁

开启无感解锁需要结合手机蓝牙配对进行,SDK 提供了开启解锁接口和蓝牙配对监听代理方法。开启解锁接口成功回调后需要引导用户进入手机 设置 > 蓝牙 页面进行设备配对,代理方法会回调蓝牙配对状态。

如果用户取消配对,无感解锁开关状态会被关闭。

接口说明

/**
 * @brief Turn on the inductive unlock
 *
 * @param devId device ID
 * @param finishedBlock unlock result
 * @param errorBlock error message
 */
- (void)turnOnBTInductiveUnlock:(NSString *)devId
                       finished:(void(^_Nullable)(void))finishedBlock
                          error:(void(^)(NSError* error))errorBlock;

示例代码

[[ThingODBTInductiveUnlock sharedInstance] turnOnBTInductiveUnlock:devId finished:^{
    NSLog(@"Enter the hid unlocking process");
} error:^(NSError *error) {
    NSLog(@"error: %@", error.localizedDescription);
}];

关闭无感解锁

接口说明

/**
 * @brief Turn off the inductive unlocking
 *
 * @param devId device ID
 * @param finishedBlock unlock result
 * @param errorBlock error message
 */
- (void)turnOffBTInductiveUnlock:(NSString *)devId
                        finished:(void(^_Nullable)(void))finishedBlock
                           error:(void(^)(NSError* error))errorBlock;

示例代码

[[ThingODBTInductiveUnlock sharedInstance] turnOffBTInductiveUnlock:devId finished:^{
    NSLog(@"Turn off successfully");
} error:^(NSError * _Nonnull error) {
    NSLog(@"error: %@", error.localizedDescription);
}];

获取解锁距离

接口说明

/**
 * @brief get the {@link unlock distance} of device
 *
 * @param devId device ID
 */
- (NSUInteger)getInductiveUnlockDistance:(NSString *)devId;

返回参数

类型 示例 说明
Integer 1 解锁距离,取值为 1、2、3、4、5,如果返回 0 表示未开启无感解锁。

示例代码

NSUInteger distance = [[ThingODBTInductiveUnlock sharedInstance] getInductiveUnlockDistance:devId];

设置解锁距离

接口说明

/**
 * @brief Set the  {@link inductive unlock distance} of device
 *
 * @param devId device ID
 * @param distance  inductive unlock distance(range: 1-5)
 * @param finishedBlock unlock result
 * @param errorBlock error message
 */
- (void)setInductiveUnlockDistance:(NSString *)devId
                          distance:(long)distance
                          finished:(void(^_Nullable)(void))finishedBlock
                             error:(void(^)(NSError* error))errorBlock;

示例代码

[[ThingODBTInductiveUnlock sharedInstance] setInductiveUnlockDistance:devId distance:distance finished:^{
    NSLog(@"Set distance successfully");
} error:^(NSError * _Nonnull error) {
    NSLog(@"error: %@", error.localizedDescription);
}];

设备管理

获取设备控制类

示例代码

ThingSmartDevice *device = [ThingSmartDevice deviceWithDeviceId:devId];

移除设备

如果要移除已开启无感解锁功能的设备,需要先检测设备的在线状态,如果离线则不允许移除。

另外,移除成功后,需要引导用户前往手机 设置 > 蓝牙 页,手动解除蓝牙配对。

接口说明

/// Removes the device and unbinds the device from the current user.
/// @param success Called when the task is finished.
/// @param failure Called when the task is interrupted by an error.
- (void)remove:(nullable ThingSuccessHandler)success failure:(nullable ThingFailureError)failure;

示例代码

// 离线的 BLE_HID、BT 类型的设备无法被移除
BOOL BTDeviceOffline = !device.isOnline && [[ThingODBTInductiveUnlock sharedInstance] checkPairedStatus:devId];
BOOL HIDDeviceOffline = !device.isOnline && [[ThingODHidInductiveUnlock sharedInstance] supportHIDAbility:devId];
if (BTDeviceOffline) {
    [Alert showBasicAlertOnVC:self withTitle:@"Attention" message:@"The device has turned on auto unlock function, and the device needs to be unbound when the device is online"];
    return;
} else if (HIDDeviceOffline) {
    [Alert showBasicAlertOnVC:self withTitle:@"Unbind Bluetooth device" message:@"The device is a HID device, please connect the device to unbind it."];
    return;
}
// 移除设备后需要引导用户手动解除蓝牙配对
[device remove:^{
    [Alert showBasicAlertOnVC:[UIApplication sharedApplication].keyWindow.rootViewController withTitle:@"Attention" message:@"To ensure you won't receive notifications of the removed device, tap Go or go to Settings > Bluetooth to check if the device is removed from your phone."];
} failure:^(NSError *error) {
    NSLog(@"error: %@", error.localizedDescription);
}];

恢复出厂设置

如果要将已开启无感解锁功能的设备恢复出厂设置,需要先检测设备的在线状态,如果离线则不允许恢复出厂设置。

设备恢复出厂设置后,设备的相关数据会被清除掉,并重新进入待配网状态,这时需要引导用户前往手机 设置 > 蓝牙 页,手动解除蓝牙配对。

接口说明

/// Restores factory settings.
/// @param success Called when the task is finished.
/// @param failure Called when the task is interrupted by an error.
- (void)resetFactory:(nullable ThingSuccessHandler)success failure:(nullable ThingFailureError)failure;

示例代码

// 离线的 BLE_HID、BT 类型的设备无法被恢复出厂设置
BOOL BTDeviceOffline = !device.isOnline && [[ThingODBTInductiveUnlock sharedInstance] checkPairedStatus:devId];
BOOL HIDDeviceOffline = !device.isOnline && [[ThingODHidInductiveUnlock sharedInstance] supportHIDAbility:devId];
if (BTDeviceOffline) {
    [Alert showBasicAlertOnVC:self withTitle:@"Attention" message:@"The device has turned on auto unlock function, and the device needs to be unbound when the device is online"];
    return;
} else if (HIDDeviceOffline) {
    [Alert showBasicAlertOnVC:self withTitle:@"Unbind Bluetooth device" message:@"The device is a HID device, please connect the device to unbind it."];
    return;
}
// 设备恢复出厂设置后需要引导用户手动解除蓝牙配对

[device resetFactory:^{
    [Alert showBasicAlertOnVC:[UIApplication sharedApplication].keyWindow.rootViewController withTitle:@"Attention" message:@"To ensure you won't receive notifications of the removed device, tap Go or go to Settings > Bluetooth to check if the device is removed from your phone."];
} failure:^(NSError *error) {
    NSLog(@"error: %@", error.localizedDescription);
}];