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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With