core Bluetooth(蓝牙4.0)
生活随笔
收集整理的這篇文章主要介紹了
core Bluetooth(蓝牙4.0)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
藍牙4.0以低功耗著稱,一般也叫BLE(Bluetooth Low Energy)。
目前主要應用的場景有:智能家居、運動手環(huán)和室內導航等。
利用core Bluetooth框架可以實現蘋果設備與第三方藍牙設備進行數據的交互。在CoreBluetooth框架中,有兩個主要的角色:周邊和中央 ,整個框架都是圍繞這兩個主要角色設計的,他倆之間有一系列的回調交換數據。core Bluetooth的核心框架圖如下:
其中左邊是中心,其中CBService?類代表服務,CBCharacteristic?類代表特征。右邊是周邊,其中CBMutableService?類代表服務,CBMutableCharacteristic?類代表特征。
注意一點就是:設備中包含服務,服務中包含特征。我們可以根據服務的唯一標示(UUID)找出設備中的服務,然后根據特征的唯一標示(UIID)從相應的服務中找出對應的特征,然后執(zhí)行相應的操作。(就像中國包含省份,省份中有包含城市,我們可以根據省份的名稱(類似于服務的UUID)來找到對應的省份,根據城市名稱(類似于特征的UUID)從對應省份中找出正確的城市)。
core Bluetooth的主要開發(fā)步驟:
1. 建立中心設備,并成為自己的代理; //1創(chuàng)建中心管理者 CBCentralManager *mgr = [[CBCentralManager alloc] init]; //成為自己的代理 mgr.delegate = self; 2. 使用中心設備掃描外設,并將掃描到的外設保存; //2利用中心設備掃描(數組指定)外部設備(若數組為空,表示掃描所有的)[mgr scanForPeripheralsWithServices:(NSArray *) options:nil]; 3. 根據外設的唯一標示,從保存的外設中連接外設; //3根據外設的標示,連接外設 [self.mgr connectPeripheral:(CBPeripheral *) options:nil]; 4. 如果連接外設成功,則掃描外設中的服務; /** 連接外設成功調用 */ - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{// 掃描外設中得服務 [peripheral discoverServices:nil]; }/** 連接外設失敗調用 */ - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{} 5. 實現外設的代理,如果找到正確的服務,則掃描服務中的特征; /** 只要掃描到服務就會調用 */ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{// 獲取外設中所有掃描到得服務NSArray *services = peripheral.services;遍歷服務:if (正確的服務){// 從需要的服務中查找需要的特征 [peripheral discoverCharacteristics:nil forService:service];} } 6.?根據特征的UUID從服務中所有特征中正確的特征; /** 只要掃描到特征就會調用 */ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {// 拿到服務中所有的特診NSArray *characteristics = service.characteristics;遍歷特征:if (正確的特征) {與外設交互代碼;} } 7. 利用特征與外設做數據交互; 8. 斷開連接。 具體代碼實現: 1 /*存儲所有的外設*/ 2 @property (nonatomic, strong) NSMutableArray *peripherals; 3 /*中心服務管理者*/ 4 @property (nonatomic, strong) CBCentralManager *mgr; 5 6 /*懶加載數組*/ 7 - (NSMutableArray *)peripherals{ 8 if (_peripherals == nil) { 9 _peripherals = [NSMutableArray array]; 10 } 11 return _peripherals; 12 } 13 14 - (void)viewDidLoad { 15 [super viewDidLoad]; 16 17 //1創(chuàng)建中心管理者 18 self.mgr = [[CBCentralManager alloc] init]; 19 20 //成為自己的代理 21 mgr.delegate = self; 22 23 //2利用中心設備掃描(數組指定的)外部設備 24 /* 如果指定數組為空表示掃描所有的設備 */ 25 [mgr scanForPeripheralsWithServices:nil options:nil]; 26 } 27 28 #pragma mark - CBCentralManagerDelegate代理方法 29 /*發(fā)現外設的時候調用*/ 30 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI{ 31 32 // 判斷如果數組中不包含當前掃描到得外部設置才保存 33 if (![self.peripherals containsObject:peripheral]) { 34 35 //讓外設成為自己的代理 36 peripheral.delegate = self; 37 38 // 保存掃描到得外部設備 39 [self.peripherals addObject:peripheral]; 40 } 41 } 42 43 /** 模擬連接所有的外設 */ 44 - (void)connect{ 45 //從保存的外設中取出所有的外設連接(也可以連接指定的外設) 46 for (CBPeripheral *peripheral in self.peripherals) { 47 /** 連接外設 */ 48 [self.mgr connectPeripheral:peripheral options:nil]; 49 } 50 } 51 52 /** 53 * 連接外設成功調用 54 */ 55 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{ 56 // 掃描外設中得服務 57 [peripheral discoverServices:nil]; 58 } 60 61 /** 62 * 連接外設失敗調用 63 */ 64 - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{ 65 } 66 67 #pragma mark - CBPeripheralDelegate代理方法 68 /** 69 * 只要掃描到外設的服務就會調用 70 * 71 * @param peripheral 服務所在的外設 72 */ 73 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{ 74 // 獲取外設中所有掃描到得服務 75 NSArray *services = peripheral.services; 76 for (CBService *service in services) { 77 // 拿到需要的服務 78 if ([service.UUID.UUIDString isEqualToString:@"123456789abc"]) { 79 // 從需要的服務中查找需要的特征 80 // 從peripheral中得service中掃描特征 81 [peripheral discoverCharacteristics:nil forService:service]; 82 } 83 } 84 } 85 /** 86 * 只要掃描到特征就會調用 87 * 88 * @param peripheral 特征所屬的外設 89 * @param service 特征所屬的服務 90 */ 91 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error 92 { 93 // 拿到服務中所有的特診 94 NSArray *characteristics = service.characteristics; 95 // 遍歷特征, 拿到需要的特征處理 96 for (CBCharacteristic *characteristic in characteristics) { 97 if ([characteristic.UUID.UUIDString isEqualToString:@"edg"]) { 98 與外設的做數據交互的部分代碼 99 } 100 } 101 }
?
轉載于:https://www.cnblogs.com/xiaocai-ios/p/5301793.html
總結
以上是生活随笔為你收集整理的core Bluetooth(蓝牙4.0)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022开什么店才能赚钱 选择这几种就
- 下一篇: 法定节假日加班三薪变补休不合法,劳动者有