Control who can connect to CBPeripheralManager

I have an app with CBPeripheralManager that advertising input service (00001812-0000-1000-8000-00805f9b34fb).

Works great, but my Apple Watch (which is paired with my iPhone) on which the app runs, connects to my service.

Is there a way to control what devices are allowed to connect?

Answered by Engineer in 798117022

00001812-0000-1000-8000-00805f9b34fb is a standard HID (Human Interface Device) service UUID, and you will find that many devices might be scanning for that at any time, and could request a connection whether you want it or not.

While it is possible to perhaps identify the device by turning around the connection, and disconnect if it is not the right device, this will, at least in your case, would only cause the whole thing to loop again. And once you disconnect the watch and start advertising the HID service, the watch (which is almost always near the phone) will want to connect again, and again, and again.

It is not possible to prevent certain devices from connecting at all. Only after a connection you can know what device it is has connected.


Argun Tekant /  DTS Engineer / Core Technologies

00001812-0000-1000-8000-00805f9b34fb is a standard HID (Human Interface Device) service UUID, and you will find that many devices might be scanning for that at any time, and could request a connection whether you want it or not.

While it is possible to perhaps identify the device by turning around the connection, and disconnect if it is not the right device, this will, at least in your case, would only cause the whole thing to loop again. And once you disconnect the watch and start advertising the HID service, the watch (which is almost always near the phone) will want to connect again, and again, and again.

It is not possible to prevent certain devices from connecting at all. Only after a connection you can know what device it is has connected.


Argun Tekant /  DTS Engineer / Core Technologies

Hi Thanks for your answer. You said that after connection it's possible to know what device is connected - how? Except for non descriptive identifier (UUID - which also changes after some time) how I can tell what device is connected (name or event device type)?

And one more thing - after identifier changes, I have to pair both devices again. Is there a way to have persistent connection for already paired devices for days and have them reconnect without the need to pair them again and again?

Here is a roundabout way you may be able to obtain more information about the connecting device. Of course, as you will see, this is far from a straightforward solution, and your mileage may vary.

The identifier you get from the CBCentral object is the key. This UUID identifies a device defined internally in the Bluetooth Stack. While the CBCentral object does not expose any other properties, the stack may contain more information at that time.

So the trick is going to be, once you have this UUID, you can try to get some properties that may be exposed, had it been a peripheral. At that point you can create a CBCentralManager, and then try accessing the same device using the retrievePeripherals(withIdentifiers:) function.

That device will be exposed to CBCentralManager as a CBPeripheral, with more properties, and some could be identifying, like a name. You can use the appropriate properties from there to decide whether this is the device you want to connect originally.

As for the need for continual pairing: for a pairing to be remembered, the paired devices will need to create a bond. iOS is supposed to do this automatically, but it could be that the connecting device is not setting the bonding flag in the security request or pairing response correctly.

Argun Tekant /  DTS Engineer / Core Technologies

Control who can connect to CBPeripheralManager
 
 
Q