我有多個具有相同服務和特性的BLE設備。我可以掃描並連接多個設備。連接後,當我嘗試通過發送命令來區分每一個時,它不起作用。它適用於單個設備。這是像套接字連接嗎?像A服務器產生子線程並且每個客戶端可以通過線程維護連接。ios中具有相同服務和特性的多個BLE設備
請提供一些關於如何在其他設備從設備讀取數據時掃描每個設備的提示。
-(void) scanDevice {
centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[centralManager scanForPeripheralsWithServices:nil options:0];
[AppDelegate app].cbCentral = centralManager;
}
-(void) stopScan {
[[AppDelegate app].cbCentral stopScan];
}
-(void)connectToDevice:(CBPeripheral *)peripheral{
[[AppDelegate app] cbCentral].delegate = self;
[[[AppDelegate app] cbCentral] connectPeripheral:peripheral options:nil];
}
-(void)calldiscoverServicesForPeripheral:(CBPeripheral *)peripheral{
[peripheral setDelegate:self];
[peripheral discoverServices:nil];
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Connected PERIPHERAL %d",peripheral.state);
[delegate getConnectedPeripheral:peripheral];
NSLog(@"Connected peripheral %@",peripheral);
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
NSLog(@"Discovered servicea: %@", peripheral.services);
for (CBService *service in peripheral.services) {
NSLog(@"Discovered service: %@", [service.UUID data]);
[peripheral discoverCharacteristics:nil forService:service];
}
}
我會詳細說明,
我有表視圖,它包含BLE設備。這是第一次它是空的,所以我會通過調用一個類「掃描設備」來搜索設備。
此「掃描設備」類包含所有的Corebluetooth方法,如CBCentralManager分配,CBperipheral委託方法。
搜索後,我將在表格視圖中顯示設備並連接到BLE設備。我從「掃描設備」類獲取一些數據。
現在,我想搜索更多設備來連接並獲取數據。爲此,我將在「ScanDevices」類中調用[[CBCentralManager alloc] initWithDelegate:self]。此時,對於之前的設備(連接和讀取)顯示警告「不是有效的外設」,並且新設備正在連接並從設備讀取數據。
但我想一次讀取來自兩個設備上的數據
請幫我... 感謝
您應該發送讀取和寫入請求到特定的CBPeripheral對象。你可以顯示該代碼嗎? – Paulw11
請檢查問題,我編輯了代碼問題。 – Dhanunjaya
哪條線給你警告? '[代理getConnectedPeripheral:]'做什麼?你正在閱讀或通知的特點?你在哪裏做的? – Paulw11