Run iOS 6 device as a BLE peripheral

core-bluetooth, ios, peripherals

Solution

There is also a high quality free app called LightBlue that you can use to test your code with. It should be able to pick up all devices advertising in peripheral mode and it can even turn itself into an advertising peripheral if you want to make sure your device is working properly.

Problem

As we know, iOS 6 support running devices (iPhone 4s and above, and new iPad) as a BLE peripheral. There is a demo in WWDC 2012 Session 705 called "advanced core bluetooth". I asked for the source code from Apple. They sent me a modified version of source code (BTLE_Transfer_Draft). Then I: - Run the app in iPhone 5 (iOS 6) in "Peripheral Mode" and start "Advertising" - Run the app in new iPad (iOS 5.1.1) in "Central Mode" The problem is that the peripheral is never been discovered at all. So I use other testing applications including some downloaded from App Store. All failed to discover peripherals. I think the problem should be in BTLE_Transfer_Draft. Because I'm not sure whether I'm allowed to present the whole source code. So I just show the "peripheral mode" part here: ``` - (void)viewDidLoad { [super viewDidLoad]; // Start up the CBPeripheralManager _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil]; } - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral { // Opt out from any other state if (peripheral.state != CBPeripheralManagerStatePoweredOn) { return; } // We're in CBPeripheralManagerStatePoweredOn state... NSLog(@"self.peripheralManager powered on."); // ... so build our service. // Start with the CBMutableCharacteristic self.transferCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable]; // Then the service CBMutableService *transferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID] primary:YES]; // Add the characteristic to the service transferService.characteristics = @[self.transferCharacteristic]; // And add it to the peripheral manager [self.peripheralManager addService:transferService]; } /** Start advertising */ - (IBAction)switchChanged:(id)sender { if (self.advertisingSwitch.on) { // All we advertise is our service's UUID [self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]] }]; } else { [self.peripheralManager stopAdvertising]; } } ``` The BLE is in powered on status and the startAdvertising is called. But the BLE central can never discover it. Post updated: According to mttrb's suggestion I added "CBAdvertisementDataLocalNameKey" when I startAdvertising. But my service is still can't be discovered by most of the apps including some apps from app store. The only one app can discover my service is an app from app store called "BLE scanner". My question is: does this mean my application is working as a peripheral? But why my own code can't discover the service? How am I supposed to debug it ? My code in Central Mode is like this: ``` - (void)viewDidLoad { [super viewDidLoad]; // Start up the CBCentralManager _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; } - (void)centralManagerDidUpdateState:(CBCentralManager *)central { if (central.state != CBCentralManagerStatePoweredOn) { return; } [self.centralManager scanForPeripheralsWithServices:nil options:nil]; } - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { ...... } - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { if (error) { NSLog(@"Error discovering services: %@", [error localizedDescription]); return; } } - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { // Deal with errors (if any) if (error) { NSLog(@"Error discovering characteristics: %@", [error localizedDescription]); return; } } - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { NSLog(@"Peripheral Disconnected"); self.discoveredPeripheral = nil; } ``` The didDiscoverPeripheral and didDiscoverServices are never called. What could be wrong? Any idea? Thanks

Original source