Detect another nearby Android device via Bluetooth

Well, I have a bit of a strange question here. I am working on a game in Android where I would like Android phones to detect each other's presence.

A device looking for other players will know the bluetooth MAC addresses on the devices of other players (from the game database), however the devices will not be paired and the devices will not be in detection mode. In addition, there will be only a few devices that could be found - so this is not very important for scanning through MAC addresses.

I don’t need to connect to devices, I just need to answer one simple question: is this a device with this mac address nearby?

It’s permissible to have a pairing dialog on another user screen ... I don’t care what the result of their selection is ... I just need to know if their device is there.

Any help would be greatly appreciated!

+4
source share
1 answer

This use case may be appropriate for the recently released API. See Recent Posts Developer Review

Nearby has its own runtime permission, which allows you to add BLUETOOTH_ADMIN or similarly to your manifest. It works on iOS and Android using several technologies (Classic Bluetooth, BLE, ultrasound). There is the option to use only an ultrasonic modem, which reduces the range to 5 feet.

I have included a partial example below, you can find a more complete sample on github

// Call this when the user clicks "find players" or similar // In the ResultCallback you'll want to trigger the permission // dialog Nearby.Messages.getPermissionStatus(client) .setResultCallback(new ResultCallback<Status>() { public void onResult(Status status) { // Request Nearby runtime permission if missing // ... see github sample for details // If you already have the Nearby permission, // call publishAndSubscribe() } }); void publishAndSubscribe() { // You can put whatever you want in the message up to a modest // size limit (currently 100KB). Smaller will be faster, though. Message msg = "your device identifier/MAC/etc.".getBytes(); Nearby.Messages.publish(googleApiClient, msg) .setResultCallback(...); MessageListener listener = new MessageListener() { public void onFound(Message msg) { Log.i(TAG, "You found another device " + new String(msg)); } }); Nearby.Messages.subscribe(googleApiClient, listener) .setResultCallback(...); } 

Disclaimer I'm working on an API

+2
source

All Articles