iOS開發藍芽 藍芽4.0的各種踩過的坑,希望你們少踩點,ios踩點
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 *)peripheral characteristic:(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 *)peripheral characteristic:(CBCharacteristic *)characteristic value:(NSData *)value{ //只有 characteristic.properties 有write的許可權才可以寫 if(characteristic.properties & CBCharacteristicPropertyWrite){ /* 最好一個type參數可以為CBCharacteristicWriteWithResponse或type:CBCharacteristicWriteWithResponse,區別是是否會有反饋 */ [peripheral writeValue:value forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse]; }else{ NSLog(@"該欄位不可寫!"); }}
11.希望能幫到人,讓別人少走點坑。