1.集成準(zhǔn)備:
(一)導(dǎo)入SDK:
由于本SDK沒有使用其他的第三方,基本用原生構(gòu)造册招,無需其他配置友鼻,直接將LDBluetooth.framework拖入工程即可。
不過一些版本的xocde可能需要手動添加一下Linked Frameworks and Libraries,如圖所示:
2.基本功能集成使用:
(一)引入頭文件:
在需要使用的類中引入頭文件:#import <LDBluetooth/LDBluetooth.h >
(二)使用藍(lán)牙操作類:
由于蘋果原生的CoreBluetooth的代理方法使用起來颠毙,比較復(fù)雜副瀑,不夠清晰舱污,于是SDK在其基礎(chǔ)上做了一些封裝想幻,將基本的代理方法的調(diào)用封在一個操作類里(LDBluetoothOperation)粱栖,類中有所有代理方法的對應(yīng)block。使用時脏毯,只需實(shí)例化一個操作類闹究,對相應(yīng)的block賦值,再將操作對象注冊給管理類(LDBluetoothManager)食店,具體代碼示例:
#import "ViewController.h"
#import <LDBluetooth/LDBluetooth.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
LDBluetoothOperation *opt = [[LDBluetoothOperation alloc] init];
opt.managerDidDiscoverPeripheral = ^(CBPeripheral *p) {
// 此處可進(jìn)行相應(yīng)操作
};
[[LDBluetoothManager defaultManager] registerBluetoothOperation:opt];
// 由于SDK必須保證掌握手機(jī)藍(lán)牙的可用性,所以該方法一定必須要調(diào)用渣淤。
[[LDBluetoothManager defaultManager] listenCBMangerStateChange:^(CBManagerState state) {
}];
}
(三)使用門禁相關(guān)的功能:
門禁的所有功能都已封裝好,無需使用上文(二)中所提的操作類叛买,使用過程也比較簡單明了砂代,首先要有個實(shí)例化的管理類LDBluetoothManager,這個使用者可以自行創(chuàng)建或直接使用SDK的單例 [LDBluetoothManager defaultManager] 率挣,然后要用該管理類監(jiān)聽手機(jī)的藍(lán)牙狀態(tài),代碼如下:
// 由于必須保證掌握手機(jī)藍(lán)牙的可用性,所以該方法一定必須要調(diào)用露戒。
[[LDBluetoothManager defaultManager] listenCBMangerStateChange:^(CBManagerState state) {
}];
接著需要掃描設(shè)備椒功,直接使用SDK的掃描代碼:
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.label.text = @"掃描設(shè)備...";
_dataSource = [NSMutableArray array];
[_manager scanPeripheralsWithSeconds:2000 callBack:^(CBPeripheral *p) {
if (![self.dataSource containsObject:p]) {
[self.dataSource addObject:p];
[self.tableView reloadData];
}
[hud hideAnimated:YES];
}];
1.藍(lán)牙開門:直接調(diào)用SDK中的開門方法即可捶箱,不過要注意在開門過程中如需停止,可調(diào)用:
[[LDBluetoothManager defaultManager] cancelConnectedPeripheral:self.peripheral];
開門的具體代碼如下:
[[LDBluetoothManager defaultManager] openTheDoorWithPeripheral:self.peripheral completion:^(BOOL success, LDError *error) {
isOpening = NO;
if (success) {
[weakSelf showAlertWithTitle:@"開門成功" message:@"門已開啟"];
weakSelf.tipsLabel.text = @"門已開啟";
}else{
// 開門發(fā)生的錯誤處理动漾,這里只簡單描述幾個丁屎,根據(jù)開發(fā)者需要,自行描述旱眯。
NSString *errMsg = @"";
switch (error.errorCode) {
case LDBLEErrorCodeFailToGetOpenKey:
errMsg = @"從服務(wù)器獲取開門秘鑰失敗";
break;
case LDBLEErrorCodeTimeout:
errMsg = @"連接超時";
break;
case LDBLEErrorCodeUnknow:
errMsg = @"未知錯誤";
break;
default:
errMsg = @"概括來說就是藍(lán)牙傳輸錯誤";
break;
}
[weakSelf showAlertWithTitle:@"開門失敗" message:errMsg];
weakSelf.tipsLabel.text = @"開門失敗";
}
}];
2.下發(fā)卡:需要對某個具體設(shè)備進(jìn)行操作晨川,獲取這個設(shè)備的DeviceNumber(連接獲取讀寫特征等步驟都已封裝在其中,不需要其他調(diào)用删豺。)共虑,獲取到后再將需要派發(fā)的卡和刪除的卡寫入到設(shè)備(需要注意的是,setupICCardWithPeripheral
這個方法中傳入的需要增加addCardNumbers
和需要刪除cancelCardNumbers
的卡呀页,必須是NSMutableArray妈拌,我這里的處理是往設(shè)備中寫入成功的卡會從該數(shù)組中移除,所以結(jié)束之后蓬蝶,需要判斷原來的NSMutableArray中還剩余幾個沒有被寫入的尘分。),并從設(shè)備獲取到相關(guān)的開門記錄丸氛,該方法中有過程回調(diào)和結(jié)果回調(diào)培愁,過程回調(diào)是為了詳細(xì)的說明配置門禁到了具體的步驟,其中具體過程用枚舉LDSetupICCardStepType
判斷(注意:因為增刪卡是使用者自己傳入缓窜,所以只返回了有效的剩余數(shù)left
竭钝,總數(shù)total
默認(rèn)給的是0,需要使用者自行處理雹洗。)香罐,結(jié)果回調(diào)要記得調(diào)用[[LDBluetoothManager defaultManager] finishSetupICCard]來結(jié)束結(jié)束配置。:
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.label.text = @"正在獲取設(shè)備號";
LDBluetoothManager *manager = [LDBluetoothManager defaultManager];
[manager getDeviceNumberWithPeripheral:_peripheral completion:^(NSString *deviceNum, LDError *error) {
if (error) {
NSLog(@"獲取設(shè)備號失斒敝住:%@",error);
dispatch_async(dispatch_get_main_queue(), ^{
[hud hideAnimated:YES];
});
[self showAlertWithMessage:[NSString stringWithFormat:@"獲取設(shè)備號失敱用!:%@",error]];
[manager finishSetupICCard];
return;
}
self.navigationItem.title = [NSString stringWithFormat:@"設(shè)備號:%@",deviceNum];
if (!self.dataSource) {
[self showAlertWithMessage:@"請先下載卡信息"];
self.dataSource = [NSMutableArray array];
}
NSMutableArray *adds = [NSMutableArray array];
NSMutableArray *cancels = [NSMutableArray array];
NSMutableArray *cardList = [NSMutableArray array];
for (LDCard *card in self.dataSource) {
if ([card.devHostNum isEqualToString:deviceNum]) {
if ([card.cardStatus intValue]) {
[cancels addObject:card.cardNum];
}else{
[adds addObject:card.cardNum];
}
[cardList addObject:card];
}
}
NSLog(@"%@-----%@",adds,cancels);
hud.label.text = @"正在一鍵配置門禁";
[manager setupICCardWithPeripheral:_peripheral deviceNumber:deviceNum addCardNumbers:adds cancelCardNumbers:cancels progress:^(LDSetupICCardStepType stepType, NSUInteger left, NSUInteger total) {
} completion:^(NSArray *data, LDError *error) {
[[LDBluetoothManager defaultManager] finishSetupICCard];
if (error) {
NSLog(@"上傳記錄失敗%ld",error.errorCode);
[self showAlertWithMessage:[NSString stringWithFormat:@"發(fā)生了錯誤:%ld",error.errorCode]];
}
if (data.count) {
// 拼裝數(shù)據(jù)
NSArray<LDRecord *> *recordList = data;
NSMutableArray *swipeRecord = [NSMutableArray array];
NSMutableDictionary *param = [NSMutableDictionary dictionary];
param[@"version"] = @"1.0";
for (LDRecord *model in recordList) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@"devHostNum"] = model.deviceNum;
dict[@"cardNum"] = [model.cardNum substringWithRange:NSMakeRange(0, 8)];
dict[@"swipeDatetime"] = model.date;
dict[@"inOut"] = model.inOut;
[swipeRecord addObject:dict];
}
param[@"swipeRecord"] = swipeRecord;
AFHTTPSessionManager *mgr = [[AFHTTPSessionManager alloc] init];
mgr.requestSerializer = [[AFJSONRequestSerializer alloc] init];
[mgr.requestSerializer setValue:@"5e810181-c592-48b6-9ae5-458a26bfbc7f" forHTTPHeaderField:@"guid"];
[mgr.requestSerializer setValue:@"70396E7391D4EC1CB1FE2A54C63867D1" forHTTPHeaderField:@"password"];
[mgr POST:@"http://192.168.31.125:7935/api/card/uploadRecord" parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
[self showAlertWithMessage:[NSString stringWithFormat:@"成功上傳開門記錄:%@",swipeRecord]];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
[self showAlertWithMessage:@"網(wǎng)絡(luò)失敗,無法上傳開門記錄"];
}];
}else{
[self showAlertWithMessage:@"無記錄可傳sΤ伞5┣!"];
}
NSMutableArray *Ids = [NSMutableArray array];
NSMutableArray *failures = [NSMutableArray array];
NSLog(@"%@-----%@",adds,cancels);
for (LDCard *card in cardList) {
if (![adds containsObject:card.cardNum] && ![cancels containsObject:card.cardNum]) {
[Ids addObject:card.identity];
}else{
[failures addObject:card.identity];
}
}
AFHTTPSessionManager *mgr = [[AFHTTPSessionManager alloc] init];
mgr.requestSerializer = [[AFJSONRequestSerializer alloc] init];
[mgr.requestSerializer setValue:@"5e810181-c592-48b6-9ae5-458a26bfbc7f" forHTTPHeaderField:@"guid"];
[mgr.requestSerializer setValue:@"70396E7391D4EC1CB1FE2A54C63867D1" forHTTPHeaderField:@"password"];
[mgr POST:@"http://192.168.31.125:7935/api/card/updateCard" parameters:@{@"version":@"1.0",@"identityList":Ids} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
[self showAlertWithMessage:[NSString stringWithFormat:@"派發(fā)增刪卡-----成功:%@,-------失敗%@",Ids,failures]];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
[self showAlertWithMessage:@"網(wǎng)絡(luò)失敗寸宏,無法派發(fā)增刪卡"];
}];
dispatch_async(dispatch_get_main_queue(), ^{
[hud hideAnimated:YES];
});
// [self cancelDo];
}];
}];
以上代碼中的網(wǎng)絡(luò)請求宁炫,是用來處理后臺控制派發(fā)卡的,基本的邏輯都在代碼中SDK的接口也基本都有注釋氮凝,更加詳盡的內(nèi)容可以參考demo羔巢,如仍有不清楚的請隨時向開發(fā)人員提問,(工作QQ:884287502)