iOS藍芽開發(二)在裝置端實現Central角色
阿新 • • 發佈:2018-12-11
若想在裝置上實現Central角色的功能,主要有以下步驟:
2.搜尋周圍廣播的裝置
3.與一個外設進行連線,並探索外設提供的服務
4.向外設傳送讀寫characteristic的請求,如果有需要訂閱characteristic值得更新,來跟蹤資料的變化。
myCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil]; [myCentralManager scanForPeripheralsWithServices:nil options:nil]; //CBCentralManager代理方法 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { NSLog(@"Discovered %@", peripheral.name); self.discoveredPeripheral = peripheral; ... }
選擇需要的外設並建立連線,並設定外設的代理為self,用於與外設的互動
[myCentralManager connectPeripheral:peripheral options:nil];
- (void)centralManager:(CBCentralManager *)central
didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Peripheral connected");
peripheral.delegate = self;//
...
}
搜尋外設提供的服務並讀取資料
[peripheral discoverServices:nil]; - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { for (CBService *service in peripheral.services) { NSLog(@"Discovered service %@", service); ... } ... }
停止搜尋外設
[myCentralManager stopScan];
從服務中獲取特徵
NSLog(@"Discovering characteristics for service %@", interestingService); [peripheral discoverCharacteristics:nil forService:interestingService]; - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { for (CBCharacteristic *characteristic in service.characteristics) { NSLog(@"Discovered characteristic %@", characteristic); ... } ... }
從特徵中讀取資料
NSLog(@"Reading value for characteristic %@", interestingCharacteristic);
[peripheral readValueForCharacteristic:interestingCharacteristic];
- (void)peripheral:(CBPeripheral *)peripheral
didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic
error:(NSError *)error {
NSData *data = characteristic.value;
// parse the data as needed
...
}
如果需要獲取動態變化的特徵,需要訂閱特徵變化的通知
[peripheral setNotifyValue:YES forCharacteristic:interestingCharacteristic];
- (void)peripheral:(CBPeripheral *)peripheral
didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic
error:(NSError *)error {
if (error) {
NSLog(@"Error changing notification state: %@",
[error localizedDescription]);
}
...
}
向外設寫資料
NSLog(@"Writing value for characteristic %@", interestingCharacteristic);
[peripheral writeValue:dataToWrite forCharacteristic:interestingCharacteristic
type:CBCharacteristicWriteWithResponse];
- (void)peripheral:(CBPeripheral *)peripheral
didWriteValueForCharacteristic:(CBCharacteristic *)characteristic
error:(NSError *)error {
if (error) {
NSLog(@"Error writing characteristic value: %@",
[error localizedDescription]);
}
...
}
需要注意一下事項:
由於藍芽是共享無線電硬體裝置的來發送和接受資料的,可能其他無線通訊也需要使用無線電硬體,例如Wifi,或者其他App也正在使用藍芽,因此,應該儘量避免長時間使用無線電。可以通過以下方式來對其進行優化
1.只在需要時進行藍芽外設搜尋,當建立與外設的連線後,應停止搜尋。呼叫stopScan方法
3.準確的獲取外設的資料,而不是搜尋外設提供的所有的資料,[peripheral discoverServices:@[firstServiceUUID, secondServiceUUID]];
4.當不再獲取資料時,取消對資料的訂閱,斷開與外設的連線
[myCentralManager cancelPeripheralConnection:peripheral];
重連外設
Figure 5-1 A sample reconnection workflow