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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

iOS开发蓝牙 蓝牙4.0的各种踩过的坑,希望你们少踩点

發布時間:2024/8/26 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS开发蓝牙 蓝牙4.0的各种踩过的坑,希望你们少踩点 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.首先建立這個三個參數

@property (nonatomic,strong)CBCentralManager * manager; @property (nonatomic,strong)CBPeripheral * peripheral; @property (nonatomic,strong)CBCharacteristic *writeDataCharacteristic

2、初始化CBCentralManager

-(CBCentralManager *)manager {if (!_manager ){_manager = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) options:nil]; } return _manager; }

3.開始掃描藍牙

[self.manager scanForPeripheralsWithServices:nil options:@{CBCentralManagerRestoredStateScanOptionsKey:@(YES)}];

?

4.當藍牙狀態改變的時候就會調用這個方法

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {switch (central.state) {case CBCentralManagerStateUnknown://設備不支持的狀態 NSLog(@">>>CBCentralManagerStateUnknown"); { dispatch_async(dispatch_get_main_queue(), ^{ ALERTVIEW(@"設備不支持"); }); } break; case CBCentralManagerStateResetting: //正在重置狀態 { dispatch_async(dispatch_get_main_queue(), ^{ ALERTVIEW(@"正在重置狀態"); }) ; } NSLog(@">>>CBCentralManagerStateResetting"); break; case CBCentralManagerStateUnsupported: //設備不支持的狀態 { dispatch_async(dispatch_get_main_queue(), ^{ ALERTVIEW(@"設備不支持的狀態"); }); } NSLog(@">>>CBCentralManagerStateUnsupported"); break; case CBCentralManagerStateUnauthorized: // 設備未授權狀態 { dispatch_async(dispatch_get_main_queue(), ^{ ALERTVIEW(@"設備未授權狀態"); }); } NSLog(@">>>CBCentralManagerStateUnauthorized"); break; case CBCentralManagerStatePoweredOff: //設備關閉狀態 { dispatch_async(dispatch_get_main_queue(), ^{ ALERTVIEW(@"設備關閉狀態"); }); } NSLog(@">>>CBCentralManagerStatePoweredOff"); break; case CBCentralManagerStatePoweredOn: NSLog(@">>>CBCentralManagerStatePoweredOn"); //開始掃描周圍的外設 [self.manager scanForPeripheralsWithServices:nil options:@{CBCentralManagerRestoredStateScanOptionsKey:@(YES)}]; break; default: break; } }

5.連接外圍設備

- (void)connect:(CBPeripheral *)peripheral {// 連接外圍設備[self.manager connectPeripheral:peripheral options:nil]; }

6.連接玩設備就調用成功的方法,然后開始掃描

//連接到Peripherals-成功 //掃描外設中的服務和特征 連接上外圍設備的時候會調用該方法 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { NSLog(@">>>連接到名稱為(%@)的設備-成功",peripheral.name); //設置的peripheral委托CBPeripheralDelegate //@interface ViewController : UIViewController [peripheral setDelegate:self]; //掃描外設Services,成功后會進入方法:-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{ [peripheral discoverServices:nil]; } //連接到Peripherals-失敗 -(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { NSLog(@">>>連接到名稱為(%@)的設備-失敗,原因:%@",[peripheral name],[error localizedDescription]); }

7.發現服務和設備的服務設備services

//發現服務 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{NSLog(@"Discovered services for %@ ", peripheral.name); if (![self.dataSourceArray containsObject:peripheral]) { [self.dataSourceArray addObject:peripheral]; NSLog(@"%@",peripheral); dispatch_async(dispatch_get_main_queue(), ^{ [_tableView reloadData]; }); } } /** * 發現外圍設備的服務會來到該方法(掃描到服務之后直接添加peripheral的services) */ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { NSLog(@"發現外圍設備的服務"); for (CBService *serivce in peripheral.services) { NSLog(@"====%@------%@+++++++",serivce.UUID.UUIDString,self.peripheral.identifier); if ([serivce.UUID.UUIDString isEqualToString:UUIDSTR_ISSC_PROPRIETARY_SERVICE]) { // characteristicUUIDs : 可以指定想要掃描的特征(傳nil,掃描所有的特征) [peripheral discoverCharacteristics:nil forService:serivce]; } } }

8.找到了設備的服務然后掃描特征

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {NSLog(@"發現外圍設備的特征");for (CBCharacteristic *characteristic in service.characteristics) { NSLog(@"====%@------+",characteristic.UUID.UUIDString); if ([characteristic.UUID.UUIDString isEqualToString:UUIDSTR_ISSC_TRANS_TX]) { // 拿到特征,和外圍設備進行交互 [self notifyCharacteristic:peripheral characteristic:characteristic]; } } for (CBCharacteristic *characteristic in service.characteristics) { NSLog(@"====%@------+",characteristic.UUID.UUIDString); if ([characteristic.UUID.UUIDString isEqualToString:UUIDSTR_ISSC_TRANS_RX]) { // 拿到特征,和外圍設備進行交互 保存寫的特征 self.writeDataCharacteristic = characteristic; } } }

?

9.最后一步要寫上通知這個一定要寫上

//設置通知 -(void)notifyCharacteristic:(CBPeripheral *)peripheralcharacteristic:(CBCharacteristic *)characteristic{//設置通知,數據通知會進入:didUpdateValueForCharacteristic方法[peripheral setNotifyValue:YES forCharacteristic:characteristic];[self.manager stopScan]; } //取消通知 -(void)cancelNotifyCharacteristic:(CBPeripheral *)peripheral characteristic:(CBCharacteristic *)characteristic{ [peripheral setNotifyValue:NO forCharacteristic:characteristic]; }

?

10.大功告成只差一步就是寫數據

//寫數據 -(void)writeCharacteristic:(CBPeripheral *)peripheralcharacteristic:(CBCharacteristic *)characteristicvalue:(NSData *)value {//只有 characteristic.properties 有write的權限才可以寫 if(characteristic.properties & CBCharacteristicPropertyWrite){ /* 最好一個type參數可以為CBCharacteristicWriteWithResponse或type:CBCharacteristicWriteWithResponse,區別是是否會有反饋 */ [peripheral writeValue:value forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse]; }else{ NSLog(@"該字段不可寫!"); } }

11.希望能幫到人,讓別人少走點坑。

轉載于:https://www.cnblogs.com/yuejunjie/p/5282321.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的iOS开发蓝牙 蓝牙4.0的各种踩过的坑,希望你们少踩点的全部內容,希望文章能夠幫你解決所遇到的問題。

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