Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CBPeripheralManager connection callback

I'm trying to set up a bluetooth connection between two iOS devices. The idea is that one device offers a service and multiple devices connect to it:

Device A (service): User slides through a presentation

Device B (multiple clients): User watches the presentation, no interaction

In my understanding the one which offers the service should advertise it and respectively be the peripheral by creating a CBPeripheralManager and implement the CBPeripheralManagerDelegate -> Device A

On the other side, there are multiple central devices which scan for peripherals using the CBCentralManager and implementing the CBCentralManagerDelegate. After a connection is established they send read and write requests to the peripheral, using the CBPeripheral class and the CBPeripheralDelegate -> Device B

And now I get confused: From what I understood device A can not send information directly to device B and doesn't even recognize if a connection has been established or lost. But the clients (device B) can read and write data and get connect and disconnect notifications.

So my first idea was to swap the roles. Now the central device offers the service by scanning for devices which advertise that they want to use the service (?!?) and multiple peripherals can connect to one central device. Already sounds like it has to be wrong, right? And here comes the trouble: Now the periphals (users who watch the presentation) do not get notified anymore if a connection has been established or lost (aaahhhhh).

Now to the question: Is there a good way to make an iOS device a peripheral and recognize connects and disconnects (which should include disconnects by distance etc)?

ADDED: In WWDC 2012 Session 705 (Link to presentation slides) the methods central:didConnect and central:didDisconnect are mentioned. But even in the iOS 7 beta documentation of CBPeripheralManagerDelegate the methods are not mentioned. Maybe this is a dead end and Apple is laughing at me right now...

Thanks for any help!

Benjamin

like image 780
benjamin.ludwig Avatar asked Feb 15 '23 23:02

benjamin.ludwig


1 Answers

You should make Device A (the presenter) the peripheral, advertising the service, and Device B (the viewers) the central, which subscribes to the peripheral's service.

You can use the CBPeripheralManagerDelegate to get notifications when a central has connected to your peripheral. Here's the documentation of the CBPeripheralManager delegate methods: CBPeripheralManagerDelegate Protocol Reference

This is the method you need to implement to recognize when a central (Device B, a viewer) has connected to your peripheral (Device A, the presenter):

- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic

This is the method you need to recognize when a central (Device B, a viewer) has disconnected from your peripheral (Device A, the presenter):

- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic

When you need to notify the subscribed centrals (Device B, the viewers) that the slide has changed, use this method (newValue would be some kind of indicator of the updated position in the presentation, like slide number):

[self.peripheralManager updateValue:newValue forCharacteristic:yourCharacteristic onSubscribedCentrals:nil];

If you want to look through a simple demo of Bluetooth LE sharing with devices acting as both centrals and peripherals, you can check out this project: SimpleShare

Hope that helps! Let me know if I can clarify something.

Laura

like image 174
Laura Avatar answered Feb 18 '23 13:02

Laura