0

我们有一个蓝牙设备。我们使用core_bluetooth.framework.Bluetooth 设备有数据。我们需要为我的 iPhone 获取蓝牙设备的数据。我们试过这样

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

4

1 回答 1

0

我最近为基于 VOIP 的应用程序完成了蓝牙功能。为此,我使用了 corebluetooth 框架。我还使用 MPVolumeview (Airplay) 在pickerview 中显示可用的蓝牙作为操作表。来到corebluetooth框架,首先#Import corebluetooth框架,然后为Corebluetoothmanager创建实例,在初始化调用之后,遵循委托方法

  • (void)centralManagerDidUpdateState:(CBCentralManager *)central { }

除了这个委托方法,iOS 8.0 以后没有其他委托方法调用。因此,在上述委托方法中,您可以跟踪 iPhone/iPad 蓝牙是否处于活动状态。如果其处于活动状态,请检查蓝牙或扬声器或 Microbuiltinphone 的音频路由。如果是蓝牙,则将 MPVolumeView 添加到您的视图以显示蓝牙图标。

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

我希望这个描述对你有很大帮助。

于 2015-10-08T07:33:05.890 回答