Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 CoreBluetooth retrievePeripheralsWithIdentifiers not Retrieving

I am having an issue updating my CoreBluetooth code from iOS 6 to iOS 7. I can scan for Peripherals and make connections, however, I am not able to reconnect a Peripheral using the new CoreBluetooth methods offered within iOS 7. Here is a look at how I am trying to accomplish the reconnection:

- (void)retrievePeripheral:(NSString *)uuidString
{

NSUUID *nsUUID = [[NSUUID UUID] initWithUUIDString:uuidString];

if(nsUUID)
{        
    NSArray *peripheralArray = [centralManager retrievePeripheralsWithIdentifiers:@[nsUUID]];

    // Check for known Peripherals
    if([peripheralArray count] > 0)
    {
        for(CBPeripheral *peripheral in peripheralArray)
        {
            NSLog(@"Connecting to Peripheral - %@", peripheral);

            [self connectPeripheral:peripheral];
        }
    }
    // There are no known Peripherals so we check for connected Peripherals if any
    else
    {
        CBUUID *cbUUID = [CBUUID UUIDWithNSUUID:nsUUID];

        NSArray *connectedPeripheralArray = [centralManager retrieveConnectedPeripheralsWithServices:@[cbUUID]];

        // If there are connected Peripherals
        if([connectedPeripheralArray count] > 0)
        {
            for(CBPeripheral *peripheral in connectedPeripheralArray)
            {
                NSLog(@"Connecting to Peripheral - %@", peripheral);

                [self connectPeripheral:peripheral];
            }
        }
        // Else there are no available Peripherals
        else
        {
            // No Dice!
            NSLog(@"There are no available Peripherals");
        }
    }
}

}

Where uuidString is the saved Peripheral UUID.

I am always getting to the NSLog statement where there are no available Peripherals. I suppose I am missing something very obvious and someone can point me in the right direction.

In addition, I have read other posts about the iOS 7 update issues with CoreBluetooth and have tried reseting the BLE device and the iOS device but to no avail.

Thanks in Advance! audible

like image 389
Audible Avatar asked Oct 02 '13 18:10

Audible


2 Answers

It turns out that my mistake dealt with how I was converting the Peripheral's identifier into an NSString format before sending a message to my retrievePeripheral method.

This was the incorrect way:

NSString *uuidString = [NSString stringWithFormat:@"%@", CFUUIDCreateString(NULL, (__bridge CFUUIDRef)([peripheral identifier]))];

This was the correct way (that worked in my scenario):

NSString *uuidString = [NSString stringWithFormat:@"%@", [[peripheral identifier] UUIDString]];

According to the Apple Documentation the NSUUID Class is not toll-free bridged with CoreFoundation's CFUUIDRef as described below:

Note: The NSUUID class is not toll-free bridged with CoreFoundation’s CFUUIDRef. Use UUID strings to convert between CFUUID and NSUUID, if needed. Two NSUUID objects are not guaranteed to be comparable by pointer value (as CFUUIDRef is); use isEqual: to compare two NSUUID instances.

For those of you who like a visual representation of what this means like I do here is an example:)

The Peripheral information printed within the Console:

<CBPeripheral: 0x14699430 identifier = DD2468AB-1865-B926-7FA4-AE3755D479D8, Name = "IDYNAMO EMV-A27F", state = disconnected>

The incorrect way of converting the Peripheral's identifier into an NSString yields:

1865B926-7FA4-AE37-55D4-79D800000000

The correct way of converting the Peripheral's identifier into an NSString yields:

DD2468AB-1865-B926-7FA4-AE3755D479D8

I hope this helps someone on their BLE journey and feel free to correct me if I am wrong in any way as I am still learning this stuff as well.

Thanks!

audible

like image 89
Audible Avatar answered Nov 01 '22 16:11

Audible


This is an old post, but for others looking for answers.

The second/next call to retrieveConnectedPeripherals actually needs the Service UUID (CBUUID type), not the iOS first-time-connected-NSUUID. What is retrieved is a list och peripherals that has that service UUID implemented. For example if you have a number of activity armbands and all have your needed heartbeat service among other things, you will be retrieveing all 3 peripherals. Also in first call to KNOWN peripherals, you have to store the NSUUID between the app starts since that ID is created by iOS at first connect. Hope that helps

like image 41
Windshield Avatar answered Nov 01 '22 16:11

Windshield