Detecting another nearby android device via Bluetooth
Asked Answered
W

1

6

Alright, I've got a bit of a weird question here. I'm working on an Android game where I'd like to be able to have Android phones detect the presence of each other.

The device searching for other players will know the bluetooth mac addresses of the other players' devices (from a game DB), however the devices will not be paired and the devices will not be in discoverable mode. Also, there will only be a handful of devices that could possibly be found - so it's not a big deal to scan through mac addresses.

I don't need to connect to the devices, I just need to be able to answer one simple question: is this device with this mac address nearby?

It is permissible to have a pairing dialog appear on the other user's screen...I don't care what the outcome of their choice is...I just need to know if their device is there.

Any help would be greatly appreciated!

Wrenn answered 15/6, 2011 at 4:56 Comment(3)
After a bit more research it appears this may be possible by using SDP and a few hidden methods in Android: wiresareobsolete.com/wordpress/2010/11/android-bluetooth-rfcomm I'm hoping I'll be able to use this SDP technique even when the devices are non-discoverable.Wrenn
After all this time.. do you have any updates on the whole bluetooth proximity issue? I know that there is the capability but I would love to have or implement my own method calcDistanceOfBluetoothDevice even if it is an approximate calculation!Bayly
is anybody succeed this? I really need this feature.Protractor
M
4

This use-case may be a good fit for the recently released Nearby API. See the Nearby Messages developer overview

Nearby has its own runtime permission saving you from adding BLUETOOTH_ADMIN or similar to your manifest. It works across iOS and Android by utilizing multiple technologies (Classic Bluetooth, BLE, ultrasound). There's an option to use only the ultrasonic modem which reduces the range to about 5 feet.

I've 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 work on the Nearby API

Machinate answered 4/9, 2015 at 23:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.