本文介绍了iOS 8.0蓝牙外设管理器不为addService提供回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个蓝牙外设管理器。我正在使用[self.peripheralManager addService:myService]向这个外设管理器添加一些服务。

I have created a bluetooth peripheral manager. I am adding some services to this peripheral manager using [self.peripheralManager addService:myService].

对于iOS7.0我得到了这个方法的回调 - (void) peripheralManager:(CBPeripheralManager *)外围设备didAddService :( CBService *)服务错误:(NSError *)错误

For iOS7.0 I am getting a callback for this method - (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error

但对于iOS8.0我没有得到任何回调方法,我无法建立连接。

But for iOS8.0 I am not getting any callback for the same method and I am not able to establish a connection.

以下是我遵循的步骤:


  1. 使用
    创建外围管理器[[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:@ {CBPeripheralManagerOptionShowPowerAlertKey:@YES}];

2.使用
创建服务myService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:@AAAA] primary:YES];

2.Create a service usingmyService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:@"AAAA"] primary:YES];


  1. 使用
    创建特征myCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:@BBBB] properties:CBCharacteristicPropertyIndi​​cate value:nil permissions:CBAttributePermissionsReadable];

  1. Create a characteristic using myCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:@"BBBB"] properties:CBCharacteristicPropertyIndicate value:nil permissions:CBAttributePermissionsReadable];

使用
myService.characteristics = characteristic

Add this characteristic to the service usingmyService.characteristics = characteristic

使用
[peripheralManager addService:myService];

Add this service to the peripheral manager using [peripheralManager addService:myService];

将此服务添加到外围设备管理器上述步骤的回调发生在

The callback for this above step occurs in


  • (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error :( NSError *)错误;

在iOS8中,我们没有收到此回调。

In iOS8 we are not receiving this callback.

外围经理确实更新状态的代码如下所示。

The code for peripheral manager did update state is shown below.

-(void) peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
    NSLog(@"Changed State : %d", peripheral.state);
    if (peripheral.state != CBPeripheralManagerStatePoweredOn) {
        [self deviceDisconnected];
        return;
    }
}

我使用以下方式添加服务:

I am adding the service using:

[self.peripheralManager addService:myService];

[self.peripheralManager addService:myService];

此方法是否已在iOS8.0中弃用或此错误可能是由于其他一些原因?

Is this method deprecated in iOS8.0 or this error might be due to some other reasons?

推荐答案

@Paulw在评论中正确回答了这个问题。以下是答案

@Paulw has answered this correctly in the comments. Below is the answer

在初始化蓝牙硬件(开机状态)之前ios7允许的操作中ios7和ios8之间有变化并发出警告。在ios8中它只是失败了。

There is change between ios7 and ios8 in that ios7 permitted operations before the Bluetooth hardware was initialised (power on state) and issued a warning. In ios8 it just fails.

所以我们必须等待 peripheralManagerDidUpdateState:回调才能添加任何服务。

So we must wait for the peripheralManagerDidUpdateState: callback before adding any service.

这篇关于iOS 8.0蓝牙外设管理器不为addService提供回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 01:46