我們使用core_bluetooth.framework
.Bluetooth設備一個藍牙devices.We有數據。我們需要獲取數據的藍牙設備我iPhone.We試過這樣didDiscoverServices和didDiscoverCharacteristicsForService永遠不會調用
-(void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"Discovered peripheral %@ (%@)",peripheral.name,peripheral.identifier.UUIDString);
if (![self.discoveredPeripherals containsObject:peripheral]) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.discoveredPeripherals addObject:peripheral];
[self.tableview insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.discoveredPeripherals.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
});
}
[central connectPeripheral:peripheral options:nil];
}
-(void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
peripheral.delegate = self;
if(peripheral.services)
[self peripheral:peripheral didDiscoverServices:nil]; //already discovered services, DO NOT re-discover. Just pass along the peripheral.
else
[peripheral discoverServices:nil];
}
-(void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
for(CBService* svc in peripheral.services)
{ NSLog(@" service %@",svc.description);
if(svc.characteristics)
[self peripheral:peripheral didDiscoverCharacteristicsForService:svc error:nil];
else
[peripheral discoverCharacteristics:nil forService:svc];
}
}
-(void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
for (CBCharacteristic *characteristic in service.characteristics) {
NSLog(@"Discovered characteristic %@(%@)",characteristic.description,characteristic.UUID.UUIDString);
NSLog(@"Characteris is %@",[characteristic description]);
}
}
我們得到了設備名稱和設備UUID號碼。但我們的問題是,我們從來沒有打電話給didDiscoverServices和didDiscoverCharacteristicsForServices.I想打我的頭在我的顯示器上,我非常沮喪與這個問題。請指導我們。我的代碼有什麼問題
在'centralManager:didConnectPeripheral:'中,你的if/else測試中哪行被調用? – Larme
@Larme it call else line [peripheral discoverServices:nil];永遠的時間 –
實現每個委託功能,並嘗試查看其餘的其中一個是否被調用。 – Aris