Peripheral and central at the same time on iOS Peripheral and central at the same time on iOS objective-c objective-c

Peripheral and central at the same time on iOS


Yes, it can be in both roles at the same time. You just have to initialize a CBPeripheralManager and a CBCentralManager. As soon as the peripheral manager is initialized and you receive the POWER ON state the device starts acting as a peripheral. You can add your services at this point and receive connections from other devices. At the same time you can use the central manager to scan and initiate connections to other peripherals.

Note that you cannot connect to your own device even if it acts as a peripheral.

For your errors, I suggest:

  1. Turn off scanning before initiating a connection. That is, scan, find peripheral, stop scan, connect. Connection and scanning do not like each other.
  2. Use a dedicated queue for handling bluetooth events, not the main queue. [[CBCentralManager alloc] initWithDelegate:self queue:my_dedicated_bluetooth_q]
  3. Unfortunately, the stack sometimes become unstable. Even restarts are possible. But this usually happens only under heavy loads or several simultaneous connections. Hopefully, this will be improved in iOS7.
  4. The unfamous Unknown error started to appear for several developers recently. Judging from your description there are probably a number of reasons why your setup may fail and it would require much more info that what fits well into a SO question.

For more info I suggest you search the bluetooth-dev mailing list archives https://lists.apple.com/archives/Bluetooth-dev or send a mail Bluetooth-dev@lists.apple.com. The community provides great help if you approach with reasonable questions like this.


As per my understanding one device can work with one mode at a time . That is if the device is working in the peripheral mode then it you cant work it as a central mode .If you see some standard examples like BTLE transfer or lilke Light Blue those are working in one mode at a time .


Firstly, what do you mean "the same time"? If you mean the device advertising to other devices while it scanning for other devices, it can not.But you can create two threads which share same lock to advertising and scanning.Before scanning, stop advertising, before advertising, stop scanning.I tested on my iPhone 4s and iPad air, worked well.