Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a CBPeripheralManager know it is connected or disconnected by a central device in Core Bluetooth?

On the central end, the CBPeripheral delegate would be called while connected or disconnected. But how does a CBPeripheralManager know that it is connected or disconnected by a central device? Besides, is it possible for a peripheral device to decline a connection request from a central device?

like image 425
M. Wu Avatar asked Oct 22 '14 14:10

M. Wu


1 Answers

You don't receive a specific notification when a central connects to peripheral service provided by your app. You can infer a connection from the following CBPeripheralManagerDelegate methods being called -

  • didSubscribeToCharacteristic
  • didReceiveReadRequest
  • didReceiveWriteRequest

If you have received a subscription via didSubscribeToCharacteristic then you can infer a disconnection when you receive a corresponding call to didUnsubscribeFromCharacteristic. If the central is not using subscriptions then you have no indication that they have disconnected - you simply won't get any more read/write requests.

You cannot decline a connection from a central. You can set an encryption requirement on one or more of your characteristics. This will then initiate a pin-based pairing process when a central first attempts to read/write/notify on that characteristic.

You could also implement some form of authentication process where a central needed to respond to a challenge/write a password to a characteristic etc before you respond to that central's other read/write requests.

like image 195
Paulw11 Avatar answered Oct 07 '22 00:10

Paulw11