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?
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 . https://appinventor.mit.edu/explore/ai2/cup-game 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).
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!
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
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)