2011-06-15 123 views
4

好的,我在这里有一个奇怪的问题。我正在开发Android游戏,希望能够让Android手机检测到彼此的存在。通过蓝牙检测附近的另一个Android设备

搜索其他玩家的设备会知道其他玩家的设备(来自游戏DB)的蓝牙MAC地址,但设备不会配对,设备也不会处于可发现模式。此外,只有少数设备可能被发现 - 因此扫描mac地址并不是什么大不了的事情。

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

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

任何帮助将不胜感激!

+0

工作了些研究后,看来这可能通过在Android中使用SDP和一些隐藏的方法是可能的:http://wiresareobsolete.com/wordpress/2010/11/android-bluetooth-rfcomm/我希望我能够使用这种SDP技术,即使在设备是不可发现的。 – ebarch 2011-06-15 20:42:38

+0

毕竟这次..你有任何更新整个蓝牙接近度问题?我知道有这种能力,但即使它是近似计算,我也会喜欢拥有或实现我自己的方法'calcDistanceOfBluetoothDevice'! – 2012-12-16 21:05:41

回答

2

这个用例可能很适合最近发布的Nearby API。查看附近消息developer overview

附近有它自己的运行时权限,可以避免将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(...); 
} 

免责声明一个更完整的示例中,我在附近的API

相关问题