0

我们使用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想打我的头在我的显示器上,我非常沮丧与这个问题。请指导我们。我的代码有什么问题

+0

在'centralManager:didConnectPeripheral:'中,你的if/else测试中哪行被调用? – Larme

+0

@Larme it call else line [peripheral discoverServices:nil];永远的时间 –

+0

实现每个委托功能,并尝试查看其余的其中一个是否被调用。 – Aris

回答

0

我最近完成了蓝牙功能基于VOIP的应用程序。 我为此使用了corebluetooth框架。此外,我使用MPVolumeview(Airplay)在pickerview中以行动表的形式显示可用的蓝牙。 来到corebluetooth帧工作,首先#IMPORT corebluetooth框架,然后 为Corebluetoothmanager和初始化呼叫之后创建实例,以下的委托方法

  • (无效)centralManagerDidUpdateState:(CBCentralManager *)中央{ }

除了这个委托方法外,没有其他的委托方法在iOS 8.0以上调用。所以,在上面的委托方法中,你可以跟踪iPhone/iPad蓝牙是否激活。如果它处于活动状态,请检查audioroute蓝牙或扬声器或Microbuiltinphone。如果它的蓝牙然后添加MPVolumeView到你的视图来显示蓝牙图标。

此外,随着此,使用Audioroutechange通知跟踪外部蓝牙状态开/关。据此可以知道外部蓝牙设备的状态。

我希望这个描述能帮助你很多。

相关问题