简体   繁体   中英

Error initializing CBService

I have a previously working iPhone app which uses Bluetooth LE accessories and that I created by modifying a sample source code.

In the myriad of updates to Xcode and IOS, it has stopped building for a reason I can not debug.

It has something to do with initializing a CBService.

Building crashes on the line, saying 'init' is unavailable:

CBService *gattDBService=[[CBService alloc] init];

Which in turn points to this line in CBAttribute.h:

- (instancetype) init NS_UNAVAILABLE;

I am now using XCode 7.0 and the project is set to deployment target of 7.1.

Why has this project started doing this? Thanks, Dale

Further code snippet (re adding services to carousel):

-(void)prepareCarouselList
{
    NSArray *allService = [self UUIDArray:[[[CBManager sharedManager] serviceUUIDDict] allKeys]];

    if (proximityServices)
    {
        [proximityServices removeAllObjects];
    }
    if (findMeServices)
    {
        [findMeServices removeAllObjects];

    }

    // Check for sensor hub
    for (CBService *service in [[CBManager sharedManager] foundServices])
    {
        if ([service.UUID isEqual:BAROMETER_SERVICE_UUID])
        {
            [carouselArray addObject:[[[CBManager sharedManager] serviceUUIDDict] valueForKey:[service.UUID.UUIDString lowercaseString]]];
            [carousel_ServiceArray addObject:service];

            isSensorHubFound = YES;
            break;
        }
    }

    if (!isSensorHubFound)
    {
        for(CBService *service in [[CBManager sharedManager] foundServices])
        {
            if([allService containsObject:service.UUID])
            {
                NSInteger ServiceKeyIndex = [allService indexOfObject:service.UUID];
                CBUUID *keyID = [allService objectAtIndex:ServiceKeyIndex];

                if([service.UUID isEqual:CAPSENSE_SERVICE_UUID] || [service.UUID isEqual:CUSTOM_CAPSENSE_SERVICE_UUID])
                {
                    [self checkCapsenseProfile:service];
                }
                else if(![self checkFindMeProfile:service])
                {

                    if ([service.UUID isEqual:IMMEDIATE_ALERT_SERVICE_UUID])
                    {
                        [findMeServices addObject:service];
                    }
                    [carouselArray addObject:[[[CBManager sharedManager] serviceUUIDDict] valueForKey:[keyID.UUIDString lowercaseString]]];
                    [carousel_ServiceArray addObject:service];

                }
            }
            else
            {
                NSDictionary *tempDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"unknown",k_SERVICE_IMAGE_NAME_KEY,[ResourceHandler getServiceNameForUUID:service.UUID],k_SERVICE_NAME_KEY, nil];
                [[[CBManager sharedManager] serviceUUIDDict] setValue:tempDict forKey:[service.UUID.UUIDString lowercaseString]];
                [carouselArray addObject:tempDict];
                [carousel_ServiceArray addObject:service];
            }
        }

    }

    //To add GATT DB item
    [self addGattDBCarouselItem];

    if([carouselArray count])
    {
        [self initCarousel];
    }

}

/*!
 *  @method addGattDBCarouselItem
 *
 *  @discussion Method to add GATTDB carousel item
 *
 */

-(void)addGattDBCarouselItem
{
    [carouselArray insertObject:[[[CBManager sharedManager] serviceUUIDDict] valueForKey:GENERIC_ACCESS_SERVICE_UUID] atIndex:0];
    CBService *gattDBService=[[CBService alloc] init];;    
    [carousel_ServiceArray insertObject:gattDBService atIndex:0];
}

CBService doesn't have a init method but CBMutableService does. Try with this:

-(void)addGattDBCarouselItem
{
    [carouselArray insertObject:[[[CBManager sharedManager] serviceUUIDDict] valueForKey:GENERIC_ACCESS_SERVICE_UUID] atIndex:0];
    NSString *keyAtIndex = [[[[CBManager sharedManager] serviceUUIDDict] allKeysForObject:carouselArray[0]] objectAtIndex:0];

    CBUUID *keyID = [CBUUID UUIDWithString:keyAtIndex];
    CBMutableService *gattDBService=[[CBMutableService alloc] initWithType:keyID primary:YES];
    [carousel_ServiceArray insertObject:gattDBService atIndex:0];
}

You cannot create an instance of CBService - instances of this object are retrieved from a CBPeripheral 's services property after service discovery.

If you want to advertise a service as a peripheral then you must instantiate a CBMutableService instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM