2.1設置對象 @property (strong, nonatomic) CBCentralManager *CBManager; @property (strong, nonatomic) CBPeripheral *peripheral; 遵照對應的代理:CBCentralManagerDelegate,CBPeripheralDelegate 2.2實例化中心設備 //options 後表明的是是否開啓alert提示 self.CBManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:@{@"CBCentralManagerOptionShowPowerAlertKey":@NO}]; 代理方法實現監控藍牙開啓狀態以及鏈接發送數據 3.1實時監控藍牙打開狀態 - (void)centralManagerDidUpdateState:(CBCentralManager *)central{ switch (central.state) { case 5: // 第一個參數填nil表明掃描全部藍牙設備,第二個參數options也能夠寫nil,調用下方方法後會開始掃描周圍藍牙設備開始調用3.2中代理方法 [self.CBManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey : [NSNumber numberWithBool:YES]}]; break; } } 3.2 開始掃描數據,掃描到對應數據後中止掃描 -(void)centralManager:(CBCentralManager*)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { //獲取廣播數據中的名稱 NSString*localName=[advertisementData valueForKey:@"kCBAdvDataLocalName"]; if ([localName hasPrefix:@"HC"]) { _peripheral = peripheral; [self.CBManager connectPeripheral:_peripheral options:nil]; // 掃描到設備以後中止掃描 [self.CBManager stopScan]; } } 3.3鏈接到當前設備 //鏈接外設成功,開始發現服務 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { NSLog(@"成功鏈接 peripheral: %@ with UUID: %@",peripheral,peripheral.identifier); // 鏈接設備以後設置藍牙對象的代理,掃描服務 [self.peripheral setDelegate:self]; [self.peripheral discoverServices:nil]; NSLog(@"掃描服務"); } 3.3 發現服務,而後根據服務查找對應的特徵 //已發現服務 -(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { NSLog(@"發現服務."); for (CBService *s in peripheral.services) { if ([s.UUID.UUIDString isEqual:ServicesUUID]) { [peripheral discoverCharacteristics:nil forService:s]; } } } 3.4 根據服務UUID查詢對應服務的特徵 -(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { for (CBCharacteristic *c in service.characteristics) { if ([c.UUID.UUIDString isEqual:CharacteristicsUUID]) { self.characteristic = c; //把數據進行轉NSData編碼,轉數據成功後寫數據給藍牙外設 NSData *data = [self returnDataWithKey:self.BleId]; [_peripheral setNotifyValue:YES forCharacteristic:c]; [_peripheral writeValue:data forCharacteristic:self.characteristic type:CBCharacteristicWriteWithoutResponse]; } 3.5 讀取藍牙外設返回的數據 //獲取外設發來的數據,不管是read和notify,獲取數據都是從這個方法中讀取。 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { if ([characteristic.UUID.UUIDString isEqual:CharacteristicsUUID]) { NSData * data = characteristic.value; ///<d6c8d6c8 40010000 00000000 00000000 00000000> 成功返回數據 ///<d6c8d6c8 80010000 00000000 00000000 00000000> 校驗和錯誤返回數據 // 比對data數據是不是返回成功的data數據或者失敗的數據 ,成功返回,提示用戶開門成功 }
4.1鏈接藍牙設備發送數據,藍牙設備收不到,具體緣由是不清楚須要鏈接的硬件具體的服務以及服務對應的特徵;框架
4.2 發送數據是須要發送先把當前的16進制數據轉byte,而後轉NSData類型數據;ide
4.2.1 獲取到門鎖上15位ID號,15位ID字符轉16進制數據:編碼
- (NSString *)hexStringFromString:(NSString *)stringatom
4.2.2 轉換好的15位ID號進行校驗和處理,處理後拼接字符串,把拼接好的16進制字符串轉byte而後再轉NSData類型數據:spa
- (NSData *)returnDataWithKey:(NSString *)key代理
4.2.3 NSData數據轉NSString類型:code
- (NSString *)convertDataToHexStr:(NSData *)data對象
4.2.4 獲取藍牙外設發送的數據時,每次接收最大爲20字節,獲取時須要屢次獲取拼接data,而後再與對應的協議進行對比;blog