Bart, here is some sample code that should get you started.
In the header file where you will run the following code, make sure it is a CBCentralManagerDelegate, and #import <CoreBluetooth/CoreBluetooth.h>
In viewDidLoad:
Code:
mgr =
[[CBCentralManager alloc] initWithDelegate:self queue:nil];
[mgr scanForPeripheralsWithServices:nil options:nil];
Implement the following code to identify discovered BLE Devices. You will notice I have some code to identify Michael's Arduino BLE Shield, which is identified as "BLE Shield". I also created a mutable dictionary called "peripherals" which I can use to store peripherals that were found during the search, by their pUUID, so there will never be dupes.
Code:
- (void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI {
NSLog(@"Peripheral: %@ \n", peripheral);
NSLog(@"pUUID: %@", [NSString stringWithCString:[self UUIDToString:[peripheral UUID]] encoding:NSStringEncodingConversionAllowLossy]);
NSString *pUUID = [NSString stringWithCString:[self UUIDToString:[peripheral UUID]] encoding:NSStringEncodingConversionAllowLossy];
[peripherals setObject:peripheral forKey:pUUID];
NSLog(@"Identified Device: %@",[advertisementData objectForKey:@"kCBAdvDataLocalName"]);
if([[advertisementData objectForKey:@"kCBAdvDataLocalName"] isKindOfClass:[NSString class]]) {
if([[advertisementData objectForKey:@"kCBAdvDataLocalName"] isEqualToString:@"BLE Shield"]) {
[self connectPeripheral:peripheral];
}
}
}
My connectPeripheral method uses the CentralManager connect method to connect.
Code:
- (void) connectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Connecting to peripheral with UUID : %@\r\n",[NSString stringWithCString:[self UUIDToString:peripheral.UUID] encoding: NSStringEncodingConversionAllowLossy]);
[mgr connectPeripheral:activePeripheral options:nil];
}
In the delegate method for connected peripherals, we search for available services:Code:
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
printf("Connection to peripheral with UUID : %s successfull\r\n",[self UUIDToString:peripheral.UUID]);
activePeripheral = peripheral;
[activePeripheral discoverServices:nil];
}
Once a service is discovered, the delegate method for that is called. From there, we call discoverCharacteristics, which I have wrapped in a method I made called getAllCharacteristicsFromArduino:Code:
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
if (!error) {
printf("Services of peripheral with UUID : %s found\r\n",[self UUIDToString:peripheral.UUID]);
[self getAllCharacteristicsFromArduino:peripheral];
}
else {
printf("Service discovery was unsuccessfull !\r\n");
}
}
-(void) getAllCharacteristicsFromArduino:(CBPeripheral *)p{
for (int i=0; i < p.services.count; i++) {
CBService *s = [p.services objectAtIndex:i];
printf("Fetching characteristics for service with UUID : %s\r\n",[self CBUUIDToString:s.UUID]);
[p discoverCharacteristics:nil forService:s];
}
}
This will allow you to discover all the characteristics from the BLE Shield. That should get you started. Once you get that far, I can walk you through some more advanced stuff (i.e. reading/wrting to the shield)