日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS-蓝牙开发

發布時間:2023/12/20 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS-蓝牙开发 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

公司的藍牙開發主要運用在門禁,掃描附近的藍牙設備,將公司的藍牙設備集成一個數組并展示列表,點擊連接藍牙,接連成功之后掃描設備特征值并獲取設備的uuid,并寫入指令開門。
CBCentralManager

//系統藍牙設備管理對象,可以把他理解為主設備,通過他,可以去掃描和鏈接外設CBCentralManager *_manager;

初始化藍牙

//初始化并設置委托和線程隊列,最好一個線程的參數可以為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){} }

總結

以上是生活随笔為你收集整理的iOS-蓝牙开发的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。