iOS-蓝牙开发
公司的藍牙開發主要運用在門禁,掃描附近的藍牙設備,將公司的藍牙設備集成一個數組并展示列表,點擊連接藍牙,接連成功之后掃描設備特征值并獲取設備的uuid,并寫入指令開門。
CBCentralManager
初始化藍牙
//初始化并設置委托和線程隊列,最好一個線程的參數可以為nil,默認會就main線程_manager = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_main_queue()];}初始化調用centralManagerDidUpdateState 我們需要實現這個方法來確保本地設備支持BLE。-(void)centralManagerDidUpdateState:(CBCentralManager *)central{switch (central.state) {case CBCentralManagerStateUnknown:NSLog(@">>>CBCentralManagerStateUnknown");break;case CBCentralManagerStateResetting:NSLog(@">>>CBCentralManagerStateResetting");break;case CBCentralManagerStateUnsupported:NSLog(@">>>CBCentralManagerStateUnsupported");break;case CBCentralManagerStateUnauthorized:NSLog(@">>>CBCentralManagerStateUnauthorized");break;case CBCentralManagerStatePoweredOff:NSLog(@">>>CBCentralManagerStatePoweredOff");break;case CBCentralManagerStatePoweredOn:NSLog(@">>>CBCentralManagerStatePoweredOn");//開始掃描周圍的外設[_manager scanForPeripheralsWithServices:nil options:nil];break;default:break;} }掃描附近的藍牙設備,篩選出公司的藍牙設備,將公司的藍牙設備整理成數組。
#pragma mark----- 外設設備連接狀態判斷 //掃描到設備會進入方法 -(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{//判斷掃描到的藍牙設備是否符合公司的藍牙設備if([peripheral.name containsString:@"SMART-GQ-B00000"]){NSLog(@"++++++++++++++%@",peripheral.name);} }連接外圍設備
//連接直連設備[_manager connectPeripheral:cbperi options:nil];當連接成功后,會回調方法centralManager:didConnectPeripheral:。在這個方法中,你可以去記錄當前的連接狀態等數據。
連接藍牙成功
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{NSLog(@">>>連接到名稱為(%@)的設備-成功",peripheral.name);[peripheral setDelegate:self];[peripheral discoverServices:nil];//連接藍牙成功后開始掃描特征值 }連接藍牙失敗
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{[SVProgressHUD dismiss];[self.navigationController.view makeToast:@"連接設備失敗"];NSLog(@">>>連接到名稱為(%@)的設備-失敗,原因:%@",[peripheral name],[error localizedDescription]); }斷開藍牙
//主動斷開藍牙設備[_manager cancelPeripheralConnection:_discoveredPeripheral];-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{[SVProgressHUD dismiss];[self.view makeToast:@"藍牙連接斷開!"];NSLog(@"error11--%@",error); }連接成功之后掃描特征值 didDiscoverServices
#pragma mark----- 掃描外設服務 -(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{for (CBService *service in peripheral.services) {[service.peripheral discoverCharacteristics:nil forService:service];} }掃描成功之后 進入didDiscoverCharacteristicsForService方法
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{for (CBCharacteristic *characteristic in service.characteristics){if([_ver isEqualToString:@"2"])//2版本的開門規則{//截取設備uuidNSString *UID6 =[UUIDSERVICE6 substringWithRange:NSMakeRange(4, 4)];//轉化為大寫NSString*UUIDString6 =[UID6 uppercaseString];if ([characteristic.UUID.UUIDString isEqualToString:UUIDString6]) {// 拿到特征,和外圍設備進行交互 保存寫的特征_writeDataCharacteristic = characteristic;NSString*writeString = @"1687#01#01";NSData* data = [writeString dataUsingEncoding:NSUTF8StringEncoding];[peripheral writeValue:data forCharacteristic:_writeDataCharacteristic type:CBCharacteristicWriteWithoutResponse];}}}讀取 characteristic 數據
[peripheral readValueForCharacteristic:characteristic]; [peripheral setNotifyValue:YES forCharacteristic:characteristic];讀取characteristic返回
//獲取外設發來的數據,不論是read和notify,獲取數據都是從這個方法中讀取。 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{if (error) {NSLog(@"There is a error in peripheral:didUpdateValueForCharacteristic:error: which called:%@",error);return;}NSLog(@"characteristic data is:%@ ",characteristic.value);NSLog(@"characteristic data length is %ld",characteristic.value.length); }寫入指令
[peripheral writeValue:data forCharacteristic:_writeDataCharacteristic type:CBCharacteristicWriteWithoutResponse];寫入指令返回
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic: (CBCharacteristic *)characteristic error:(NSError *)error {if(characteristic == _writeDataCharacteristic && error == nil){} }總結
- 上一篇: linux下的安装:openssl
- 下一篇: iOS蓝牙开发(三)实现外设功能