Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get my app to send Bluetooth pairing request via NFC

I am trying to get two Android devices (M & O) to Bluetooth pair via NFC and have only just now managed to get it to partially work after weeks of Googling, trial-and-error and more Googling. At the moment, I have got the devices to successfully pair when the following NDEF message is sent:

// Kotlin code where the 0x7F's are replaced with the sender's BT MAC in little-endian order
val msg = NdefMessage(NdefRecord.createMime("application/vnd.bluetooth.ep.oob", byteArrayOf(0x08, 0x00, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F)));

This is only working because I've hardcoded the BT MAC address in the NDEF record. On the M device I can get the MAC programmatically, but that is no good for O devices. Also, this method requires user permission and I thought that previous working examples entirely bypassed the user

Anyway, my questions are:

  • Is there a way to get Android to generate the pairing request on my behalf, since it knows the Bluetooth MAC address but I don't (or at least wont on devices other than my own)
  • If so, will this mean that the user is no longer requested to confirm the pairing?

If I can't get Android to send the MAC then this whole exercise is pointless since I won't be able to get the MAC addresses of O devices in the wild anyway. Also, if I can't get it to pair without asking the user for permission then what is the point? The pairing process is automatically started just by opening a socket to a known BT MAC address and that (assuming I even know it) can be sent by any arbitrary/proprietary NFC message

Any help with this would be greatly appreciated because the Android NFC docs are absolutely useless on this point and every online example I've managed to find is ancient or assumes that BluetoothAdapter.getAddress() still returns a valid address

like image 442
HumanBean Avatar asked Aug 31 '25 05:08

HumanBean


1 Answers

Android 9+ doesn't allow getting the local Bluetooth adapter MAC address (stupidly so, really, makes absolutely no sense, the security/privacy gain here is minimal to none and the UX is badly hurt), therefore, you can't do the classic NFC to BT handover (SSP), without hardcoding the MAC address, which means you can't do it.

This is a terrible move by Google which made them also drop "Android Beam" (as it does the same, NFC > BT handover via NDEF SSP).

Google now came up with "NearBy Share" app to replace "Anrdoid Beam", BUT as it relies on the extremely slow Android "NearBy" API, it is also extremely slow (10 seconds to discover and connect!).

Comparing to Apple AirDrop which takes only 3-4 seconds, it is a disaster and Google will probably find that out someday and revert this populistic decision.

like image 95
NOP-MOV Avatar answered Sep 02 '25 18:09

NOP-MOV