Alex Guberman
Alex Guberman

Reputation: 203

How to get UUID from LE Bluetooth chip?

I have a chip, working on LE Bluetooth and transmitting it's UUID. I need to discover it from IOS app and get it's UUID. I know how to establish a connection between two IOS devices, but not with another chips.

Thanks!

Upvotes: 1

Views: 5267

Answers (1)

David Rinck
David Rinck

Reputation: 6986

You should check out Apple's CoreBluetooth Temperature Example.

First you will use the CBCentralManager to find the available Bluetooth Peripherals with the UUID you are looking for. It's a lengthy process that requires delegates and I can't easily give you code snippets to do this. It will look something like this.

.h file will have these. Remember to add the CoreBluetooth Framework.
#import <CoreBluetooth/CoreBluetooth.h>
CBCentralManager * manager;
CBPeripheral * connected_peripheral;

(Change your UUID accordingly):

NSArray * services=[NSArray arrayWithObjects:
                    [CBUUID UUIDWithString:@"0bd51666-e7cb-469b-8e4d-2742f1ba77cc"],
                    nil
                    ];
[manager scanForPeripheralsWithServices:services options: [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]];
[manager connectPeripheral:peripheral options:nil];

From there you know you have the right peripheral, but you still need to select it and stop the CBManager from continuing to scan for new devices.

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    [manager stopScan];

    NSArray *keys = [NSArray arrayWithObjects:
                 [CBUUID UUIDWithString:@"0bd51666-e7cb-469b-8e4d-2742f1ba77cc"],
                 nil];
    NSArray *objects = [NSArray arrayWithObjects:
                    @"My UUID to find",
                    nil];

    serviceNames = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

    [connected_peripheral setDelegate:self];
    [connected_peripheral discoverServices:[serviceNames allKeys]];

}

Now that you have told your peripheral to advertise what services it has, you'll have a delegate for parsing those services.

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
    CBService *bluetoothService;
    for (bluetoothService in connected_peripheral.services) {
        if([bluetoothService.UUID isEqual:[CBUUID UUIDWithString:@"0bd51666-e7cb-469b-8e4d-2742f1ba77cc"]])
        {
            NSLog(@"This is my bluetooth Service to Connect to");
        }
}

I wish this process was easier to explain. The best way to figure it out is to download Apple's Temperature Example and run it on your iPhone or iPad (it won't work in the simulator). Even though you probably aren't broadcasting temperature it will find your Bluetooth LE device and will parse through the services it is broadcasting. Placing breakpoints in the LeDiscovery.m file of that project should show you the steps needed to discover your Bluetooth LE chip from an iOS app.

Hope this helps!

Upvotes: 1

Related Questions