androiduniqueidentifiergoogle-nearbydeviceidgoogle-nearby-connections

Permanent device ID between Nearby Connections?


I am currently working on a P2P chat application where the user discovers nearby devices and connects to them. When the other devices accept the connection request, they are added to the user's contacts list. What I want to do is re-establish those connections whenever the application is launched. For example, on the first launch of the application I connected to 3 devices, they were added to my contacts list and then I closed the app. I save the contacts list locally using Room. When I restart the application I want to automatically reconnect my device to my contacts if they are still nearby and display a green dot for showing that they are available for chatting.

Now the problem is that I noticed that the endpointId provided by the Connections API keeps changing for the same device between connections. I have been looking for a good solution for this but unfortunately I couldn't come up with anything. I thought about identifying nearby devices by their name but this won't work if there are devices using the same name or if one of my contacts changed name in the meantime. So, I need a unique identifier that won't change between connections for each device so I can save it in Room and identify the devices nearby to decide whether they are or not part of my contacts list.

Can anyone give me a suggestion of a procedure that I can apply in this case?


Solution

  • Exchange AndroidId of devices when they are paired. you can use it as name or any other specification of paired devices.

    String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);