15
votes

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.

2
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

2 Answers

14
votes

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.

5
votes

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.