4

I faced a problem about CoreBluetooth Framework.

As Example, I have two devices and each device performs both advertising and scanning. So, I use CBCentralManager and CBPeripheralManager on each.

So, to simplify problem, let's assume that Device1 is broadcasting and Device2 scanning.

When CBCentralManager on Device2 discover the Device1, callback centralManager:didDiscoverPeripheral:advertisementData:RSSI: is called and I can get Device1 identifier by calling [peripheral.identifier UUIDString].

How then I can get my local device (Device2) Identifier? I mean in case that both devices are advertising, Device1 will discover Device2 too and get it's peripheral identifier, but How can I get exact this identifier (some kind of self-address) on Device2?

P.S. I know that this Identifier is not unique for each device and can change over the time, it's not a problem.

Regexident
  • 29,441
  • 10
  • 93
  • 100
Roman F
  • 135
  • 2
  • 7
  • Did you find any solution for this case, I need an Identify Device 1 and when device 2 scan I know it was device 1 – Bassem Tourky Mar 23 '20 at 20:37
  • @Bassem I'm in the same situation, and the way I'm handling it is thus: Each peripheral always sends at least one common type of object. It includes a UUID generated by the peripheral. When the central receives it, it can then associate that peripheral-generated UUID with the ID of the peripheral. Then, for example, if the devices disconnect, when a notification comes that the services are invalid, those items can be cleaned up. – Victor Engel Aug 18 '20 at 21:05

1 Answers1

11

Unfortunately you can't do this. The identifier returned by [peripheral.identifier UUIDString] is auto generated by iOS on the receiving device. It is seeded by the transmitter Mac address (which does not change) and the time the device starts being seen by iOS (which does change).

Because of the second factor is not predictable and the fact that the id generation algorithm is not published, you cannot predict what this id will be on the transmitter side.

Bottom line, if you want a predictable unique id, you cannot use this field

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • Thanks for the answer! Is it possible to send this value from receiving device somehow? @davidgyoung – Roman F Nov 14 '14 at 14:04
  • 1
    You could have an identifier as a GATT characteristic, but you would need to establish a BLE connection to read it. That is a little slow and error prone, but possible. – davidgyoung Nov 15 '14 at 00:44
  • Thanks, @davidgyoung. But can I connect to peripheral in Background? Can I accept this connection too or it requires foreground state? – Roman F Nov 15 '14 at 22:48
  • Unfortunately, no. "... if your app has not specified the bluetooth-peripheral background mode, the contents of its services become disabled when it is in the background or in a suspended state; any remote central trying to access the service’s characteristic value or characteristic descriptors receives an error." -- https://developer.apple.com/library/IOs/documentation/CoreBluetooth/Reference/CBPeripheralManager_Class/index.html – davidgyoung Nov 16 '14 at 02:25
  • Thanks, @davidgyoung. But what if I specify it, will I able to connect devices in bg then? Because I've tried but with no result( – Roman F Nov 16 '14 at 15:04
  • @davidgyoung Can you tell me if that identifier can change over the time? Can I use this identifier to reconnect my peripheral? – sdespont Jan 18 '16 at 11:30
  • The identifier can change over time, yes, although in my experience it is stable on a per session basis. The implementation undocumented, but it may be stable until you reboot the phone. I do not believe there are any APIs to connect based on this identifier alone. – davidgyoung Jan 18 '16 at 11:59