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.希望能幫到人,讓別人少走點坑。