Distance between two devices

I have been experimenting with the location sensor and have a question.

If two phones have the same app, what would be the best way of seeing how close they are to each other?

I know I could probably have one app/device save it's location data on a global database on a mysql database, and the other could then calculate the distance, but I am wondering if there is a better way to do this - something similar to a proximity sensor.

To explain, I am working on a simple game where I would like a player to be able to pass tokens in real time to another player, but only if they are standing next to each other. Is this possible?

For your purposes

Welcome Ross.

This is not really practical using gps hardware. Read about Accuracy and the smallest distance a gps receiver can resolve in Using the Location Sensor

If you want to 'try', here is an example that would work if the distances are greater than a few meters and uses a real time database Social Distancing ... a CloudDB / Location Marker Tutorial and tells you how close users are to one another.

So what to do:
1.) use a proximity sensor if all player's Android's have the hardware ProximitySensor There are video examples.
I'm not sure whether it is possible to determine which device it is near so it might not really be practical to use a proximity sensor either.

2). you might be able to use NearField sensor . NFC Cup Game for AI2 You would have to provide each player and purchase readable, writable, NFC tags. It will tell you if the NFC tag is range of your device (if I remember, the tags need to be within a very short distance of the Android).

1 Like

I have 2 possible solutions:

  1. Using BluetoothLE:
  • Make the users enter their Bluetooth Address at installation and put it in a CloudDB (I'm afraid if it has privacy issues)
  • The users phone will continuously try to connect to any bluetooth device within its range whose address is in the database
  • Once connected, they will keep streaming (random) data between each other & check the RSSI strength of the signal.
  • If the strength passes a certain threshold you can be quite sure that the devices must be really close.
  1. QR Code:
  • One user generates a QR Code and the other scans it, hence conforming proximity.

I haven't ever worked with BLE so I can't conform whether it is possible or not.

1 Like

Thank you both for the ideas.. I have been looking into the practicality of all of them.

Unfortunately, I still cannot work out a very practical solution. Let me explain the game slightly further. A player can give "lives" (tokens) to another player, but I would like this to be done without needing internet access. The process would be something similar to:

Player one selects how many lives to give player two.
The two devices start to communicate.
The lives are removed from device one and added to device two.
A confirmation needs to be received by each device before the new totals are tallied.

NFC originally looked to be best to achieve this, but I have read that NFC is being deprecated by Google, and in fact the cup game did not work on my phones. (A Samsung S7 and S10)

Using QR/barcodes was an option, but for the "transaction" to totally complete, there would need to be a lot of scans from both devices - not very practical.

Bluetooth is a possibility - but from what I can gather, a search of all discoverable devices needs to be done first and then both devices paired. Lots of time required up front with lots of user interaction. Unless it is possible to (eg) scan a barcode and know what device to connect to?

I wanted something almost as quick and easy as (eg) paying for an item in a shop with your phone.

If you have any suggestions, they would be massively appreciated!

Why lot of scans? Only 2, I guess.

It's possible. When the user installs the app for the first time, ask the user to enter their Bluetooth MAC address, which can be found in Settings>About Phone>Statuses (which can be annoying to the user, but he has to do it only once. Also you can use ActivityStarter to navigate the user there). This address is saved in TinyDB.

If you wonder why do we ask the user, and instead do it programmatically, click HERE

:link: Access to Hardware Identifier

To provide users with greater data protection, starting in this release (Android 6.0), Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi and Bluetooth APIs. The WifiInfo.getMacAddress() and the BluetoothAdapter.getAddress() methods now return a constant value of 02:00:00:00:00:00.


When 2 user want to connect, one of them clicks Generate QR and other clicks Scan QR. The QR code contains the Bluetooth Address of that device. So after QR scanning the other device knows which device to connect with (thus no wasting time on discovering available devices)