简体   繁体   English

使用蓝牙获取两个 android 设备之间的距离

[英]Getting distance between two android devices using bluetooth

I founds lots of similar questions on SO.我在 SO 上发现了很多类似的问题。 But most of them didn't work or out of date.但他们中的大多数都没有工作或过时。

I'm trying to build an android application which uses Bluetooth to scan nearby devices, I want the application to alert the user when a phone is nearly 2 meters away.我正在尝试构建一个 android 应用程序,该应用程序使用蓝牙扫描附近的设备,我希望该应用程序在手机接近 2 米时提醒用户。

So I'm trying to get the distance using the following method found on SO.所以我正在尝试使用在 SO 上找到的以下方法来获取距离。

protected double calculateDistance(float txPower, double rssi) {

    if (rssi == 0) {
        return -1.0; // if we cannot determine distance, return -1.
    }

    double ratio = rssi * 1.0 / txPower;

    if (ratio < 1.0) {
        return Math.pow(ratio, 10);
    } else {
        double accuracy = (0.89976) * Math.pow(ratio, 7.7095) + 0.111;
        return accuracy;
    }
}

The method is returning a weird small number, I don't think it's even the distance because it just keeps on changing even when the phone is in the same location.该方法返回一个奇怪的小数字,我认为它甚至不是距离,因为即使手机位于同一位置,它也会不断变化。

Any idea about how to get an accurate distance?关于如何获得准确距离的任何想法?

For those who are looking for the distance between devices.对于那些正在寻找设备之间距离的人。 After lots of research and searching, I found out that getting the accurate or exact distance is not something easy to implement.经过大量的研究和搜索,我发现获得准确或准确的距离并不是一件容易实现的事情。 Android and Apple are still working on stuff like this. Android 和 Apple 仍在研究类似的东西。

What I used as a workaround since I was in a hurry for a solution, is something called Nearby Messages API .因为我急于寻求解决方案,所以我用作解决方法的是一种叫做Nearby Messages API的东西。

It is a publish-subscribe API, available for both Android and iOS.它是一个发布-订阅 API,可用于 Android 和 iOS。 Which catch the nearby devices that are subscribed to a specific Message using a specific MessageListener .它使用特定的MessageListener捕获附近订阅特定Message的设备。 And you can add some Strategy to the Listener in order to know if the distance is really near EARSHOT or not.您可以向侦听器添加一些Strategy ,以了解距离是否真的接近EARSHOT

It is exactly what I needed, so that worked for me.这正是我所需要的,所以这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM