这个用例可能很适合最近发布的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
工作了些研究后,看来这可能通过在Android中使用SDP和一些隐藏的方法是可能的:http://wiresareobsolete.com/wordpress/2010/11/android-bluetooth-rfcomm/我希望我能够使用这种SDP技术,即使在设备是不可发现的。 – ebarch 2011-06-15 20:42:38
毕竟这次..你有任何更新整个蓝牙接近度问题?我知道有这种能力,但即使它是近似计算,我也会喜欢拥有或实现我自己的方法'calcDistanceOfBluetoothDevice'! – 2012-12-16 21:05:41