LW003 iOS 软件开发包指南
- 此 SDK 仅支持基于 LW003 的设备。
设计说明
- 我们将 SDK 和设备之间的通信分为两个阶段:扫描阶段,连接阶段。为了便于理解,让我们来看看相关的类以及它们之间的关系。
MKLBCentralManager
:全局管理器,检查系统蓝牙状态,监听状态更改,最重要的是扫描和连接设备;
MKLBInterface
:设备成功连接后,可以通过 MKLBInterface
中的接口读取设备数据;
MKLBInterface+MKLBConfig
:设备成功连接后,您可以通过 MKLBInterface+MKLBConfig.h
中的接口配置设备数据;
扫描阶段
在此阶段中,MKLBCentralManager
将扫描和分析 LW003 设备的广告数据。
连接阶段
开发者需要输入连接密码,并通过调用 connectPeripheral:password:sucBlock:failedBlock:
来连接。
开始使用
开发环境
- Xcode9+,由于基于Swift4.0的DFU和Zip Framework,请使用Xcode9或更高版本进行开发;
- iOS12,我们将最低iOS系统版本限制为12.0;
引入到项目
CocoaPods
SDK-LB可以通过CocoaPods获取。要安装,只需将以下行添加到您的Podfile中,然后导入<MKLoRaWAN-B/MKLBSDK.h>
pod 'MKLoRaWAN-B/SDK'
-
!!!在iOS 10及以上版本中,Apple增加了对蓝牙的授权控制,您需要在项目的“info.plist”文件中添加以下字符串:“Privacy - Bluetooth Peripheral Usage Description - “您的描述””。如下截图所示。
-
!!!在iOS 13及以上版本中,Apple对Bluetooth API增加了权限限制。您需要将字符串添加到项目的“info.plist”文件中:“Privacy-Bluetooth Always Usage Description-“您的使用描述””。
开始开发
获取Manager的共享实例
首先,开发者应该获取Manager的共享实例
MKLBCentralManager *manager = [MKLBCentralManager shared];
1. 开始扫描任务以查找附近的设备,请按照以下步骤操作:
- 1. 设置扫描代理并完成相关代理方法。
manager.delegate = self;
- 2. 您可以通过以下方式启动扫描任务
[manager startScan];
- 3. 同时,您可以通过以下方式停止扫描任务
[manager stopScan];
2. 连接到设备
MKLBCentralManager
包含连接设备的函数。
/// Connect device function
/// @param trackerModel Model
/// @param password Device connection password,8 characters long ascii code
/// @param sucBlock Success callback
/// @param failedBlock Failure callback
- (void)connectPeripheral:(nonnull CBPeripheral *)peripheral
password:(nonnull NSString *)password
sucBlock:(void (^)(CBPeripheral *peripheral))sucBlock
failedBlock:(void (^)(NSError *error))failedBlock;
3. 获取状态
通过该管理器,您可以获取手机的当前蓝牙状态和设备连接状态。如果您想监控这两个状态的变化,可以注册以下通知来实现
- 当手机的蓝牙状态变化时,将发布
mk_lb_centralManagerStateChangedNotification
。您可以通过以下方式获取状态
[[MKLBCentralManager shared] centralStatus];
- 当设备连接状态变化时,将发布
mk_lb_peripheralConnectStateChangedNotification
。您可以通过以下方式获取状态
[MKLBCentralManager shared].connectState;
4. 监控设备的扫描数据。
当设备连接时,开发者可以通过以下步骤监控设备的扫描数据
- 1. 使用以下方法打开数据监控
[[MKLBCentralManager shared] notifyStorageDataData:YES];
- 2. 设置代理并完成相关代理方法。
[MKLBCentralManager shared].dataDelegate = self;
#pragma mark - mk_lb_storageDataDelegate
- (void)mk_lb_receiveStorageData:(NSString *)content {
NSInteger number = [MKBLEBaseSDKAdopter getDecimalWithHex:content range:NSMakeRange(8, 2)];
if (number == 0) {
//The last piece of data, you can get the total number of pieces of data stored.
NSString *sum = [MKBLEBaseSDKAdopter getDecimalStringWithHex:content range:NSMakeRange(10, 4)];
return;
}
/*
The maximum length of content is 182 bytes.
*/
//Content can call the following method "parseScannerTrackedData:" for parsing
}
+ (NSArray *)parseScannerTrackedData:(NSString *)content {
content = [content substringFromIndex:10];
NSInteger index = 0;
NSMutableArray *dataList = [NSMutableArray array];
for (NSInteger i = 0; i < content.length; i ++) {
if (index >= content.length) {
break;
}
NSInteger subLen = [MKBLEBaseSDKAdopter getDecimalWithHex:content range:NSMakeRange(index, 2)];
index += 2;
NSString *subContent = [content substringWithRange:NSMakeRange(index, subLen * 2)];
NSDictionary *dateDic = [self parseDateString:[subContent substringWithRange:NSMakeRange(0, 14)]];
NSString *tempMac = [[subContent substringWithRange:NSMakeRange(14, 12)] uppercaseString];
NSString *macAddress = [NSString stringWithFormat:@"%@:%@:%@:%@:%@:%@",
[tempMac substringWithRange:NSMakeRange(10, 2)],
[tempMac substringWithRange:NSMakeRange(8, 2)],
[tempMac substringWithRange:NSMakeRange(6, 2)],
[tempMac substringWithRange:NSMakeRange(4, 2)],
[tempMac substringWithRange:NSMakeRange(2, 2)],
[tempMac substringWithRange:NSMakeRange(0, 2)]];
NSNumber *rssi = [MKBLEBaseSDKAdopter signedHexTurnString:[subContent substringWithRange:NSMakeRange(26, 2)]];
NSString *rawData = [subContent substringFromIndex:28];
index += subLen * 2;
NSDictionary *dic = @{
@"dateDic":dateDic,
@"macAddress":macAddress,
@"rssi":rssi,
@"rawData":rawData,
};
[dataList addObject:dic];
}
return dataList;
}
+ (NSDictionary *)parseDateString:(NSString *)date {
NSString *year = [MKBLEBaseSDKAdopter getDecimalStringWithHex:date range:NSMakeRange(0, 4)];
NSString *month = [MKBLEBaseSDKAdopter getDecimalStringWithHex:date range:NSMakeRange(4, 2)];
if (month.length == 1) {
month = [@"0" stringByAppendingString:month];
}
NSString *day = [MKBLEBaseSDKAdopter getDecimalStringWithHex:date range:NSMakeRange(6, 2)];
if (day.length == 1) {
day = [@"0" stringByAppendingString:day];
}
NSString *hour = [MKBLEBaseSDKAdopter getDecimalStringWithHex:date range:NSMakeRange(8, 2)];
if (hour.length == 1) {
hour = [@"0" stringByAppendingString:hour];
}
NSString *min = [MKBLEBaseSDKAdopter getDecimalStringWithHex:date range:NSMakeRange(10, 2)];
if (min.length == 1) {
min = [@"0" stringByAppendingString:min];
}
NSString *second = [MKBLEBaseSDKAdopter getDecimalStringWithHex:date range:NSMakeRange(12, 2)];
if (second.length == 1) {
second = [@"0" stringByAppendingString:second];
}
return @{
@"year":year,
@"month":month,
@"day":day,
@"hour":hour,
@"minute":min,
@"second":second,
};
}
5. 监控设备断开连接的原因。
注册mk_lb_deviceDisconnectTypeNotification
通知以监控数据。
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(disconnectTypeNotification:)
name:@"mk_lb_deviceDisconnectTypeNotification"
object:nil];
- (void)disconnectTypeNotification:(NSNotification *)note {
NSString *type = note.userInfo[@"type"];
/*
After connecting the device, if no password is entered within one minute, it returns 0x01. After successful password change, it returns 0x02, the device has no data communication for two consecutive minutes, it returns 0x03, and the shutdown protocol is sent to make the device shut down and return 0x04.
*/
}
更改日志
-
20210316 修复解析器方法;
-
20210316 第一个版本;