/* 建立中心設備 掃描外設(Discover Peripheral) 連接外設(Connect Peripheral) 掃描外設中的服務和特征(Discover Services And Charateristics) 利用特征與外設做數據交互(Explore And Interact) 斷開連接... ...
/* 建立中心設備 掃描外設(Discover Peripheral) 連接外設(Connect Peripheral) 掃描外設中的服務和特征(Discover Services And Charateristics) 利用特征與外設做數據交互(Explore And Interact) 斷開連接(Disconnect) */ #import "ViewController.h" #import <coreBluetooth/CoreBluetooth.h> @interface ViewController ()<CBCentralManagerDelegate, CBPeripheralDelegate> @property (nonatomic, strong) CBCentralManager *mgr; /** 發現的外設數組*/ @property (nonatomic, strong) NSMutableArray *peripheralArray; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 1.建立中心設備 // queque:如果說你傳空,在主隊列 self.mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; // 2.掃描外設 } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; // 最後一步,斷開連接 [self.mgr stopScan]; } #pragma CBCentralManagerDelegate /* state: CBManagerStateUnknown = 0, 未知 CBManagerStateResetting, 重置 CBManagerStateUnsupported, 不支持 CBManagerStateUnauthorized, 未經授權 CBManagerStatePoweredOff, 沒有啟動 CBManagerStatePoweredOn, 開啟 */ - (void)centralManagerDidUpdateState:(CBCentralManager *)central { NSLog(@"state = %zd", central.state); // 藍牙開啟 if (central.state == CBManagerStateUnknown) { NSLog(@"未知的藍牙"); } if (central.state == CBManagerStatePoweredOn) { // 2.掃描外設 [self.mgr scanForPeripheralsWithServices:nil options:nil]; } } /** 3.連接外設(Connect Peripheral) @param peripheral 外設 @param advertisementData 相關的二進位數據 @param RSSI 信號強度 */ - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI { // 1.記錄外設,必須要有數組進來行存儲 if ([self.peripheralArray containsObject:peripheral]) { [self.peripheralArray addObject:peripheral]; } // 2.隱藏步驟,tableview表格列表(省略了) // 3.連接外圍設備 [self.mgr connectPeripheral:peripheral options:nil]; // 4.設置外設的代理 peripheral.delegate = self; } // 連接到外設之後,會調用這個代理方法 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { // 掃描服務 // 傳nil代表掃描所有服務 [peripheral discoverServices:nil]; } #pragma CBPeripheralDelegate // 外設掃描裡面的服務 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { // 獲取外設的服務 for (CBService *service in peripheral.services) { if ([service.UUID.UUIDString isEqualToString:@"123"]) { // uuid一致,那就開始掃描特征 [peripheral discoverCharacteristics:nil forService:service]; } } } // 當發現到特征的時候會調用 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { // for (CBCharacteristic *characteristic in service.characteristics) { if ([characteristic.UUID.UUIDString isEqualToString:@"123456"]) { // [peripheral readValueForDescriptor:<#(nonnull CBDescriptor *)#>]; // [peripheral writeValue:<#(nonnull NSData *)#> forDescriptor:<#(nonnull CBDescriptor *)#>]; } } } - (NSMutableArray *)peripheralArray { if (!_peripheralArray) { _peripheralArray = [NSMutableArray array]; } return _peripheralArray; }