15

Can iOS (iPhone or iPad) app have Core Bluetooth (BTLE) central manger and peripheral manager objects at same time?

Can they operate asynchronously, or does main app thread need to make them share (switch back and forth).

Sharing conceptual algorithm would be: disable peripheral manager, enable central manager and do central functions, and then, disable central mgr, enable peripheral mgr, and do peripheral functions (that is, send automatic nofications, and wait for and respond to remote characteristic commands), repeat...

BACKGROUND GORY DETAILS: We have a local system with multiple iOS devices and multiple non-iOS devices that need to inter-communicate by BTLE. The non-iOS devices all use Broadcom BCM20732 Bluetooth LE chip. But hardware is not ready yet, so I'm using iOS devices to emulate the non-iOS, which requires simultaneous central AND peripheral functionality, ie. 1. act as central to periodically interrogate multiple other non-iOS devices in system. 2. act as peripheral to respond to requests for data from iOS user interface devices.

rmaddy
  • 298,130
  • 40
  • 468
  • 517
Doug Null
  • 7,131
  • 14
  • 55
  • 116
  • I am designing a gaming app, and the two BTLE connected devices have to switch acting as central and peripheral automatically, any suggestions for this? – Ashwin G Oct 13 '16 at 20:21

2 Answers2

14

I got it working. I just started with the Apple "BTLE central peripheral transfer", then first delted the -35 db bug that it has (search for "-35" then delete the if(){ return }), then I combined both the central.m and the peripheral.m into a single UIViewController .m file, added a UISwitch to select one of two service UUID's, and modified the peripheral sender to automatically increment the text field (after init'ing it to ASCII '0').

I had two iPad mini's continuously each sending the incrementing number to the other side. It got up to over 900 transfers and then hung. But I've seen the Apple "BTLE c p transfer" always hang after a few minutes, requiring iPad restart to continue. I ended app at both iPad's and cycled power, re-started app, and they got up to 1600 increments, and then hung.

To solve the hang, I'll add resource control to prevent central and peripheral managers from connecting at same time, as per Abo's recommendation.

Doug Null
  • 7,131
  • 14
  • 55
  • 116
  • 4
    August 2013 update: communication between multiple iOS devices, each using both central and peripheral works bug-free. Have done longevity tests and it doesn't break after hours of operation. TIP: I do a short delay before switching from central to peripheral and vice versa, otherwise there can be problems. – Doug Null Aug 14 '13 at 12:51
  • 2
    TIP2: NSLOG()'s and printf()'s within your code Bluetooth callback events will sink your ship. Create a spooler function that stores these messages, then have your main thread print 'em out. – Doug Null Aug 14 '13 at 12:53
  • 4
    I was wrong: NSLog() and printf() are okay within the callback events. – Doug Null Sep 27 '13 at 21:23
5

Yes they can but if your phone is now in peripheral mode and is connected, you have to make sure that it disconnects before switching to central mode and trying to connect with other devices and vice versa.

Aboelseoud
  • 556
  • 1
  • 7
  • 22
  • But the peripheral does not have a command to disconnect. Only a central can disconnect. So, centrals should always disconnect from peripherals that other centrals will be polling? – Doug Null Sep 27 '13 at 21:38
  • Yes, you have to somehow send a message to the central device, to disconnect. – Aboelseoud Oct 05 '13 at 20:19