android - 通过蓝牙检测附近的另一个安卓设备

标签 android bluetooth mac-address proximity

好的,我这里有一个奇怪的问题。我正在开发一款 Android 游戏,我希望 Android 手机能够检测到彼此的存在。

搜索其他玩家的设备将知道其他玩家设备的蓝牙 mac 地址(来自游戏数据库),但是这些设备不会配对并且设备不会处于可发现模式。此外,可能只会找到少数设备 - 因此扫描 mac 地址并不是什么大问题。

我不需要连接到设备,我只需要能够回答一个简单的问题:附近有这个 mac 地址的设备吗?

允许在其他用户的屏幕上显示配对对话框...我不关心他们选择的结果是什么...我只需要知道他们的设备是否在那里。

如有任何帮助,我们将不胜感激!

最佳答案

此用例可能非常适合最近发布的 Nearby API。查看附近消息 developer overview

Nearby 拥有自己的运行时权限,使您无需在 list 中添加 BLUETOOTH_ADMIN 或类似内容。它利用多种技术(经典蓝牙、BLE、超声波)在 iOS 和 Android 上运行。可以选择仅使用超声波调制解调器,这会将范围缩小到大约 5 英尺。

我在下面包含了一个部分示例,您可以在 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(...);
}

免责声明我在 Nearby API 上工作

关于android - 通过蓝牙检测附近的另一个安卓设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6353188/

相关文章:

c# - 当我的代码不存在时,我如何让我的代码创建一个 sqlite 数据库?

android - iosched 13 应用程序中的导航是否正确?

android - 是否可以制作一个连接到我的 android 应用程序 Activity ( map )的磨损应用程序

bluetooth - FlutterBlue 特性

android - 无法在模拟器中访问蓝牙

jquery - 如何使用 jQuery 查找 PC MAC 地址

android - setWebViewClient 与 setWebChromeClient 有什么区别?

java - 安卓自动蓝牙连接

python - 正则表达式(Python)-匹配MAC地址

java - 如何在java中获取接入点的MAC地址?