为多台设备适配 Android 蓝牙聊天 [英] Adapting Android Bluetooth Chat for multiple devices

查看:20
本文介绍了为多台设备适配 Android 蓝牙聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个关于 Android 蓝牙的大学项目,我正在尝试了解如何管理多个连接设备之间的通信.最后我要开发一个多人蓝牙游戏.

I'm doing a college project on Bluetooth for Android, and I'm trying to understand how to manage communication between multiple connected devices. Eventually I'm going to develop a multiplayer Bluetooth Game.

目前我已经调整了 Android 的示例应用 BluetoothChat 来连接我的三部 Nexxus One 手机.1 连接到 2 谁连接到 31 成功地向 2 发送了它的消息.3 也成功地向 2 发送了它的消息.2 可以成功地将其消息发送到 1 和 3,因为它与两者共享一个 ConnectedThread.但我不知道如何处理从 1 到 3 的通信.

Currently I've adapted Android's sample app BluetoothChat to connect my three Nexxus One phones. 1 connects to 2 who connects to 3 1 sends its messages successfully to 2. 3 sends its messages successfully to 2 as well. 2 can send its messages successfully to 1 and 3, as it shares a ConnectedThread with both. But I can't figure out how to handle getting communication from 1 to 3.

有没有人有多个设备之间通信的任何例子,或者自己已经做到了?谢谢

Does anyone have any examples of communication between multiple devices or has done this themselves? Thanks

推荐答案

一种方法是用发送者和接收者注释你的消息,这样当 2 收到消息时,它知道将它传递给 3.当 3 收到时一条消息,它检查接收者属性以知道它来自设备 1.这个额外的层允许您通过其他设备发送和接收消息,并且仍然能够知道它来自谁.

One way is to annotate your messages with the sender and receiver, so that when 2 gets the message, it knows to deliver it on to 3. When 3 gets a message, it checks the receiver attribute to know it is from device 1. This extra layer allows you to send and receive messages through other devices and still be able to know who it is from.

不过,首先,您需要弄清楚如何让每台设备都了解网络上的所有其他设备.如果您只是在一条线上连接,例如 1-2-3-4,那么每次设备进入网络时,您都可以通过网络发送更新的列表,但是如果 2 掉线了会怎样?你只是退出游戏吗?等待它重新配对?在这种情况下,最好查看 peer-to-peer 网络,或典型的客户端服务器架构,在这种架构中,您让一台设备成为主机,特别是如果它打算以后成为多人游戏.
希望有帮助!

First, though, you'll need to figure out how to make every device know about every other device on the network. If you're just connecting in a line, like 1-2-3-4, then every time a device enters the network, you could send an updated list through the network, but what happens if 2 drops out? Do you just quit the game? Wait for it to be re-paired? In this case, it may be better to look at a peer-to-peer network, or the typical client server architecture where you let one device be the host, especially if this is intended to later be a multiplayer game.
Hope that helps!

这篇关于为多台设备适配 Android 蓝牙聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆