Java 是否可以找到附近的其他手机?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21006701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to find other nearby phones?
提问by Nicholas
I'm writing an Android app that I would like to eventually port to iOS and Windows Mobile (though I know absolutely nothing about them right now). I'd like my app to be able to find other phones within a certain radius (maybe 20-30 feet) that also have the game installed so that the users can interact with others in their current physical space. Is this possible and, if so, how?
我正在编写一个 Android 应用程序,我希望最终将它移植到 iOS 和 Windows Mobile(尽管我现在对它们一无所知)。我希望我的应用程序能够找到特定半径(可能是 20-30 英尺)内也安装了游戏的其他手机,以便用户可以在他们当前的物理空间中与其他人互动。这是可能的,如果是,如何?
I plan on the app requiring an internet connection so my first thought was to use GPS like described at iOS Find Other Nearby Devices (GPS locations), but that would require continually transmitting data to the server to update the user's location using both bandwidth and battery (not to mention the server resources). GPS also seems to take a while to get any sort of accurate fix, and even though I wouldn't store historical data I wouldn't expect users to trust me with that sort of private information (as well they shouldn't!)
我计划使用需要互联网连接的应用程序,所以我的第一个想法是使用iOS 中描述的 GPS查找其他附近的设备(GPS 位置),但这需要不断地将数据传输到服务器以使用带宽和电池更新用户的位置(更不用说服务器资源了)。GPS 似乎也需要一段时间才能获得任何类型的准确修复,即使我不会存储历史数据,我也不希望用户相信我拥有那种私人信息(他们也不应该信任我!)
I know there are proprietary technologies, such as Sony's NFC, but that's far too narrow a focus to use for an app that I'd like to be device and OS agnostic. What other options are there? Assuming the user is willing to give the app the appropriate permissions, what other methods could allow one device to find another nearby one, within a 10-15 meter range, that is practical for a large (million+) userbase? Is HTML5 Geolocation a better path? Or is GPS my only real option?
我知道有一些专有技术,例如索尼的 NFC,但是对于我希望与设备和操作系统无关的应用程序来说,这太狭窄了。还有哪些其他选择?假设用户愿意为应用程序授予适当的权限,还有哪些其他方法可以让一台设备在 10-15 米范围内找到另一台附近的设备,这对于大型(百万以上)用户群来说是实用的?HTML5 地理定位是更好的途径吗?还是 GPS 是我唯一真正的选择?
I did find Use Android GPS to detect and connect with other phones, but was hoping the past few years have opened up further options.
我确实找到了Use Android GPS to detection and connect with other phone,但希望过去几年已经开辟了更多的选择。
UPDATE
更新
Thank you for all the responses so far. To answer some questions posed in the comments and answers, I thought I'd describe how I expect the apps/devices to interact.
感谢您到目前为止的所有回复。为了回答评论和答案中提出的一些问题,我想我会描述我期望应用程序/设备如何交互。
All users who install this app will have an agent running in the background to update their current geolocation and to listen for pings. Any user may open the full program and send a quick request for all other users nearby. If I go the central server route, then the server responds to that request with a list of nearby usernames. The user can then pick one of those to initiate a connection, in which case the target device makes a sound to notify of a request to connect.
安装此应用程序的所有用户都会在后台运行一个代理,以更新他们当前的地理位置并监听 ping。任何用户都可以打开完整程序并向附近的所有其他用户发送快速请求。如果我使用中央服务器路由,那么服务器会使用附近用户名列表来响应该请求。然后,用户可以选择其中之一来启动连接,在这种情况下,目标设备会发出声音以通知连接请求。
From there it's easy enough to handle the interaction between two devices. Finding those nearby users is the sticking point for me.
从那里可以轻松处理两个设备之间的交互。找到那些附近的用户是我的症结所在。
采纳答案by Adam Stelmaszczyk
Options I can see
我能看到的选项
Bluetooth(BT). I don't know, if I'm not too skeptical about it, but I see couple of problems. Short range. The biggest energy consumption. Maximum 7 peers connected at the same time (but maybe it's enough...). The big advantage is that a lot of devices support BT.
BT LE(low energy). Ok, it helps energy consumption problem. But, requires Android 4.3.
WiFi Direct(aka P2P). I gave it a shot, I'm a bit disappointed. The problem is that it needs Android 4.0 or higher. Even worse, some devices don't have required hardware. I launched
WiFiDirectDemo
from Android samples on Samsung Xperia Miro ST23i. Clicked on "search" and the only thing I got was a toast "enable WiFi P2P - in your settings". I clicked on everything in that settings, there is just normal WiFi and other stuff, no mention about Direct or P2P. Xperio Miro just doesn't support WiFi Direct.WiFi connection with a server through internet + device geolocation.Server keeps positions of users and answers questions like "who is 20 feet in my range?". The problem can be the precision of geolocation. If the walls are thick or you are in a place on earth with few satellites - it can be very inaccurate.
蓝牙(BT)。我不知道,如果我不是太怀疑的话,但我看到了几个问题。短距离。最大的能源消耗。最多同时连接 7 个对等点(但也许就足够了......)。最大的优势是很多设备都支持BT。
BT LE(低能量)。好的,它有助于解决能源消耗问题。但是,需要 Android 4.3。
WiFi 直连(又名 P2P)。我试了一下,有点失望。问题是它需要 Android 4.0 或更高版本。更糟糕的是,有些设备不需要硬件。我
WiFiDirectDemo
从三星 Xperia Miro ST23i 上的 Android 样本启动。单击“搜索”,我得到的唯一结果是“启用 WiFi P2P - 在您的设置中”。我点击了该设置中的所有内容,只有普通的 WiFi 和其他东西,没有提及 Direct 或 P2P。Xperio Miro 只是不支持 WiFi Direct。通过互联网 + 设备地理定位与服务器的 WiFi 连接。服务器保持用户的位置并回答诸如“谁在我的范围内 20 英尺?”之类的问题。问题可能是地理定位的精度。如果墙壁很厚,或者您在地球上几乎没有卫星的地方 - 它可能非常不准确。
To sum up
总结
There is greater probability that somebody will have WiFi turned on all the time than BT. BT also have some time limits after which it is turned off (they make it this way to probably save battery). So, WiFi seems a better choice than BT.
与 BT 相比,有人始终打开 WiFi 的可能性更大。BT 也有一些时间限制,之后它就会关闭(他们这样做可能是为了节省电池)。因此,WiFi 似乎是比 BT 更好的选择。
If WiFi Direct is popular enough for you - I would give it a try. If not, option number 4 seems the best. It's not ideal, I know, but at this point it's hard to come with something better (we still need technology development, WiFi Direct looks promising).
如果 WiFi Direct 对您来说足够流行 - 我会尝试一下。如果没有,选项 4 似乎是最好的。我知道这并不理想,但在这一点上很难找到更好的东西(我们仍然需要技术开发,WiFi Direct 看起来很有希望)。
回答by dfowler7437
I would use the central server approach. Each phone sends position data to the server. The server notifies the other users when they are in range. To limit used data, only send positions when the device reports movement more then X meters.
我会使用中央服务器方法。每部手机都向服务器发送位置数据。当其他用户在范围内时,服务器会通知他们。要限制使用的数据,仅当设备报告移动超过 X 米时才发送位置。
Also, consider using a Ping button. The user presses it to send the position when they wish to find someone nearby. Position data would only be sent on this ping. This would use very little power and provide good privacy.
另外,请考虑使用 Ping 按钮。当用户希望在附近找到某人时,按下它以发送位置。位置数据将仅在此 ping 上发送。这将使用很少的功率并提供良好的隐私。
It might be possible to detect details about the connected Wifi access point. If you could get the AP or Gateway's MAC address, you could send that as a kind of position. All the players on the same Gateway would be close by.
可能会检测到有关连接的 Wifi 接入点的详细信息。如果您可以获得 AP 或网关的 MAC 地址,您可以将其作为一种位置发送。同一个网关上的所有玩家都会在附近。