Bluetooth connection between 2 iOS devices

22,582

Solution 1

As spamsink commented, one device needs to act as peripheral, and one as central in order for them to communicate.

There is a great sample app from Apple that does that. Also, check out WWDC 2012 sessions 703 - CoreBluetooth 101 and 705 - Advanced CoreBluetooth for great explanation and examples of CoreBluetooth framework usage.

Also note, for device to be in peripheral mode, it needs to be updated to iOS 6.0 or later.

Solution 2

Well, my understanding of Bluetooth Low Energy (BLE) general was poor. As the accepted answer pointed out, one device has to act as Central and other one has to act as peripheral for communication to take place.

A good example source code for iOS to iOS and iOS to Mac OS BLE communication is here.

Some important points to consider

  1. on iOS 5.0 -> iPhone can only act as Central, so communication between 2 iOS devices is not possible.
  2. on iOS 6.0 -> iPhone can act as peripheral too.. So for communication to take place, atleast one device has to be running on iOS 6.0 (and probably later).
  3. First iPhone device with BLE hardware added is iPhone 4S. So even if iPhone 4 can run iOS 5 ,BLE communication is not possible on it.

Well some information..

Solution 3

if you call scanForPeripherals function in didUpdateState delegate then it works because delegate functions can't return.

Share:
22,582
Krishnabhadra
Author by

Krishnabhadra

After having dates with C, C++, 8051, AVR, Linux, QT now concentrating on mobile application development(iOS and Android). Still plays with C now and then, can't live without. Now making swift progress with Swift. A post graduate in Electronics, switched career to become a application software developer. Now a proud owner of iOS gold badge :) SOreadytohelp

Updated on January 28, 2020

Comments

  • Krishnabhadra
    Krishnabhadra over 4 years

    I am trying out Core Bluetooth framework introduced in iOS 5.0. According to many threads (one of many) on StackOverflow itself:

    1. Core Bluetooth framework can be used to communicate with ANY hardware, which has Bluetooth Low Energy (4.0) hardware support.
    2. We can forget about Made For iPhone/iPod (MFI) program, if you are using Core Bluetooth technology.

    I have an iPhone 5, iPhone 4S, Google Android Nexus 7 with me, and I am sure at least first 2 has hardware support for BLE.

    My Question is

    Well, I tried below given code on my iPhone 4S/iPhone 5, but it failed to scan and find the iPhone5/iPhone 4S sitting near by. I can confirm, both devices had bluetooth turned ON. The delegate method didDiscoverPeripheral never getting called. What might be the reasons? Am I missing something?

    This is my code (stripped down to a small test project).

    ViewController.h

    @interface ViewController:UIViewController<CBCentralManagerDelegate, CBPeripheralDelegate{
    }
    @property (strong, nonatomic) CBCentralManager *mCentralManager;
    @end
    

    ViewController.m

    @implementation ViewController
    @synthesize mCentralManager;
    
    - (void)viewDidLoad{
        [super viewDidLoad];
        mCentralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
        [self scanForPeripherals];
    }
    
    - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
        NSLog(@"Received periferal :%@",peripheral);
    }
    
    - (int) scanForPeripherals {
        if (self.mCentralManager.state != CBCentralManagerStatePoweredOn)
        {
            NSLog(@"self.mCentralManagerState : %d",self.mCentralManager.state);
            return -1;
        }
        //Getting here alright.. bluetooth is powered on.
        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
        //Documentation says passind nil as device UUID, scans and finds every peripherals
        [self.mCentralManager scanForPeripheralsWithServices:nil options:options];
        return 0;
    }
    @end