iOS - 核心蓝牙,检测到的服务没有任何特征

时间:2015-03-11 06:18:43

标签: ios bluetooth ios8

我的蓝牙代码存在问题,该代码在iOS7上工作但不在iOS8上。我的中心代码是在“peripheral didDiscoverServices”方法中检测外设的服务。但它没有发现任何特征。例如,在下面的代码中,当我打印service.characteristics时,我得到nil。

- (void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {

if (error) {
    NSLog(@"peripheral:didDiscoverServices: error = %@",error.debugDescription);
    return;
}

for (CBService *service in peripheral.services) {
    if ([service.UUID isEqual:[CBUUID UUIDWithString:kHotseatServiceUUID]]) {
        [peripheral discoverCharacteristics:nil forService:service];
        break;
    }
}}

我可以确认我正在为iOS8正确创建服务(我希望)。我只在检测到蓝牙电源打开后才创建并添加服务到外围管理器,请参阅下面的代码。任何人都有任何想法,我应该看看?非常感谢。

- (void) peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {

if (peripheral.state != CBPeripheralManagerStatePoweredOn) return;

if (peripheral.state == CBPeripheralManagerStatePoweredOn) {
    [self buildTheHotseatService];  // this function is where I create & add the service, see below.
    [self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey: @[[CBUUID UUIDWithString:kHotseatServiceUUID]] }];
}}

- (void) buildTheHotseatService {

// build the service
CBMutableService *hotseatService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:kHotseatServiceUUID] primary:YES];

// build the service characteristic
self.characteristicForStart = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:kHotseatCharacteristicUUIDForActivityStartDate] properties:CBCharacteristicPropertyRead value:[self.dataForCharacteristics objectForKey:kHotseatServiceCharacteristicKeyForActivityStart] permissions:CBAttributePermissionsReadable];
self.characteristicForTime = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:kHotseatCharacteristicUUIDForActivityTime] properties:CBCharacteristicPropertyRead value:[self.dataForCharacteristics objectForKey:kHotseatServiceCharacteristicKeyForActivityTime] permissions:CBAttributePermissionsReadable];
self.characteristicForDelay = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:kHotseatCharacteristicUUIDForActivityDelayTime] properties:CBCharacteristicPropertyRead value:[self.dataForCharacteristics objectForKey:kHotseatServiceCharacteristicKeyForActivityDelay] permissions:CBAttributePermissionsReadable];
self.characteristicForHaulProfileIdentifier = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:kHotseatCharacteristicUUIDForActivityHaulProfileIdentifier] properties:CBCharacteristicPropertyRead value:[self.dataForCharacteristics objectForKey:kHotseatServiceCharacteristicKeyForActivityHaulProfileIdentifier] permissions:CBAttributePermissionsReadable];
self.characteristicForType = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:kHotseatCharacteristicUUIDForActivityType] properties:CBCharacteristicPropertyRead value:[self.dataForCharacteristics objectForKey:kHotseatServiceCharacteristicKeyForActivityType] permissions:CBAttributePermissionsReadable];
self.characteristicForAuthenticationCode = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:kHotseatCharacteristicUUIDForActivityAuthenticationCode] properties:CBCharacteristicPropertyRead value:[self.dataForCharacteristics objectForKey:kHotseatServiceCharacteristicKeyForActivityAuthenticationCode] permissions:CBAttributePermissionsReadable];
self.characteristicForTruck = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:kHotseatCharacteristicUUIDForTruck] properties:CBCharacteristicPropertyRead value:[self.dataForCharacteristics objectForKey:kHotseatServiceCharacteristicKeyForTruckIdentifier] permissions:CBAttributePermissionsReadable];
self.characteristicForLoader = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:kHotseatCharacteristicUUIDForLoader] properties:CBCharacteristicPropertyRead value:[self.dataForCharacteristics objectForKey:kHotseatServiceCharacteristicKeyForLoaderIdentifier] permissions:CBAttributePermissionsReadable];
self.characteristicForInputReading = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:kHotseatCharacteristicUUIDForInputReading] properties:CBCharacteristicPropertyRead value:[self.dataForCharacteristics objectForKey:kHotseatServiceCharacteristicKeyForInputReading] permissions:CBAttributePermissionsReadable];
self.characteristicForInputType = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:kHotseatCharacteristicUUIDForInputType] properties:CBCharacteristicPropertyRead value:[self.dataForCharacteristics objectForKey:kHotseatServiceCharacteristicKeyForInputType] permissions:CBAttributePermissionsReadable];

self.characteristicForStartReading = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:kHotseatCharacteristicUUIDForStartReading] properties:CBCharacteristicPropertyRead value:[self.dataForCharacteristics objectForKey:kHotseatServiceCharacteristicKeyForStartReading] permissions:CBAttributePermissionsReadable];
self.characteristicForEndReading = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:kHotseatCharacteristicUUIDForEndReading] properties:CBCharacteristicPropertyRead value:[self.dataForCharacteristics objectForKey:kHotseatServiceCharacteristicKeyForEndReading] permissions:CBAttributePermissionsReadable];

// add the characteristic to the service
hotseatService.characteristics = @[self.characteristicForType, self.characteristicForTime, self.characteristicForStart, self.characteristicForHaulProfileIdentifier, self.characteristicForDelay, self.characteristicForAuthenticationCode, self.characteristicForTruck, self.characteristicForLoader, self.characteristicForInputType, self.characteristicForInputReading, self.characteristicForStartReading, self.characteristicForEndReading];

[self.peripheralManager addService:hotseatService];

}

0 个答案:

没有答案