通过蓝牙检测附近的另一台 Android 设备

发布于 2024-11-15 14:56:11 字数 307 浏览 4 评论 0原文

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

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

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

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

任何帮助将不胜感激!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木緿 2024-11-22 14:56:11

此用例可能非常适合最近发布的 Nearby API。查看附近消息开发者概述

附近有自己的运行时权限,使您无需添加 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(...);
}

免责声明 I使用附近 API

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文