安卓蓝牙聊天示例应用--多个连接[英] Android Bluetooth Chat sample app - multiple connections

本文是小编为大家收集整理的关于安卓蓝牙聊天示例应用--多个连接的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

是否有可能设置Android蓝牙聊天示例应用程序一次连接多人,并有一个迷你聊天室?这将是什么?

推荐答案

tl; dr版本:蓝牙糟透了,不要使用它,使用wifi,而不是web后端支持.

我在社会无线网络研究项目的利益中彻底调查了这个问题.我的一般建议是:它不适用于两个以上/三个人. Bluetooth刚刚使用无线对等网络设计的.

一般来说,似乎是Android设备上包含的廉价蓝牙控制器(尤其是 HTC的设备,IIRC)一次都不真正处理任何超过两三个连接.我不确定是否这是硬件或固件问题,但我可以叙述一些基本的轶事.我正努力在2011年初的SDK级别(即,没有固件修改)来实现这个想法,并且能够获得同行以获得两个额外的连接(即,三个设备,每个连接到另外两个设备)在连接突然死亡之前,在一个小时到一个小时的工作时间,并且插座将变得无法使用,需要重新连接.不幸的是,20分钟是一个上限,一般来说,不可能得到任何可靠的其他设备.

项目的目标是支持多个人在背景中默默地互动互动,但这从未实现过,而是我们抛弃蓝牙并与WiFi一起去,这更好地工作了很多.在摘要中,我认为人们将蓝牙视为可靠的对等通信的可靠同行的可能介质,但它并没有真正设计:它更像是用于小型设备之间短程通信的介质(思考耳机).

要意识到,如果要执行此操作,可以修复您可以连接的最大设备数量,因为根据蓝牙规范,微微网最多支持七个设备. (参见Wikipedia文章.)

所需的更改很简单:为每个设备使用不同 uuid.这可以使用频带外交换机制或简单方案来实现多种方式,或者在越来越多的方式分配UUID,以及连接到网络时,请连续尝试.

以下是一些相关的Google小组线程:

我记得发布更详细的一个细节如何做到这一点(用代码)我可能会挖掘......,如果我能找到它.它应该是2010年底或2011年初.

所以答案是,在摘要中,是的,你可以通过使用多个uuids(使用一个,它是它,而且您必须尝试使用​​某些赋值后尝试来执行此操作协议).但是,在实践中,经过大量的试验和错误,这并不真正适用于您可能想要使用它的东西,而且它是一个 lot 更好地使用互联网后端.顺便说一下,这也是一个不错的原因,大多数用户真的不喜欢打开他们的蓝牙,因为害怕他们的电池被排水..

其他推荐答案

在这里离开这一点,以防它有助于别人.

我能够在官方蓝牙教程中进行自定义聊天室A>并修改一点. 不幸的是,我无法提供我的大部分代码,但主要的想法是:

每个设备都作为服务器和客户端起作用.启动聊天时,设备启动其服务器线程.服务器线程与官方相同,但在接受连接时不会结束.它只是保持收听.

客户端线程与教程中相同.

两个服务器和客户端线程管理连接相同.我创建了分隔的线程以接受消息以下教程和一个发送它们.

private void manageConnectedSocket(BluetoothSocket socket) {        
    //create thread responsible for sending messages.
    SendingThread w = new SendingThread(socket);
    MainActivity.addSendingThread(w);
    //Creates listener for messages to accept.       
    MainActivity.addListener(socket);
}    

现在在主活动中始终在用户单击"发送"按钮时,每个工作人员(发送线程)向远程设备发送消息.侦听是异步运行的.

重要:

  1. 当邮件发送失败时,您需要处理异常,并在检测到丢弃设备时删除设备的发送和接收线程.在我的情况下,我使用了众所周知的UUID"00001101-0000-1000-8000-00805F9B34FB".每个设备.
  2. 您需要在atempts之间等待3秒以连接到客户端,因为某些设备具有弱蓝牙硬件,并且拒绝连接为客户端.

其他推荐答案

BT连接最多可支持7-10个连接.所以你将在该范围内限制.我认为它专为主设备的扩展而不是随机通信

来源:在Google

上搜索"蓝牙编程">

本文地址:https://www.itbaoku.cn/post/102330.html

问题描述

Is it possible to set up the Android Bluetooth Chat sample app to connect more than one person at a time, and have a mini chat room? What would that entail?

推荐答案

tl;dr version: Bluetooth sucks for this, don't use it, use wifi instead, probably backed by a web backend.

I have investigated this issue thoroughly throughout the years in the interests of a social wireless network research project. My general advice is: it doesn't work with more than two / three people. Bluetooth just isn't designed with wireless peer to peer networks in mind.

In general, it seems that the cheap Bluetooth controllers included on Android devices (especially HTC's devices, iirc) don't really handle any more than two or three connections at a time. I'm unsure if this is a hardware or firmware problem, but I can recount some basic anecdotes. I was working to implement this idea at the SDK level (i.e., without firmware modifications) around the beginning of 2011, and was able to get a peer to get two additional connections (i.e., three devices, each connecting to the other two) to work for a period of a few minutes to an hour before the connections would suddenly die and the socket would become unusable, requiring reconnection. Unfortunately, 20 minutes was an upper bound, and generally it was impossible to get connections to more than one other device at all reliably.

The goal of the project was to support multiple people interacting with each other silently in the background, but this never materialized, instead we ditched Bluetooth and went with wifi instead, which worked much much better. In the abstract, I think people view Bluetooth as a possible medium for reliable peer to peer communication, but it wasn't really designed that way: it's more of a medium used for short range communication between small devices (think headsets).

Be aware that if you want to do this, the maximum number of devices to which you can connect is fixed, because as per the Bluetooth spec, a piconet supports a maximum of seven devices. (See the wikipedia article.)

The required change is simple: you use a different UUID for each device. This can be implemented a number of ways, using an out of band exchange mechanism, or simple scheme where you assign UUIDs in an increasing fashion and when connecting to the network, try each in succession.

Here are some relevant Google groups threads:

I remember posting a more elaborate one detailing how to do this (with code) that I might dig up as well.., if I can find it. It should be from late 2010 or early 2011.

So the answer is, in the abstract, yes, you can try to do this, by using multiple UUIDs (after you use one, that's it, and you have to try another using some assignment protocol). However, in practice, after a lot of trial and error, this doesn't really work for what you probably want to use it for, and it's a lot better to go with an internet backend instead. By the way, this is also good for another reason, most users don't really like to turn on their Bluetooth for fear of their battery being drained..

其他推荐答案

Leaving this here, in case it helps someone else.

I was able to make my custom chat room following official bluetooth tutorial and modifying it a little. Unfortunately, I cannot provide most of my code, but main idea is:

Every device is acts both as server and as a client. When Chat is started, device starts its server thread. Server thread is the same as official but doesn't ends when accept connection. It just keep listening.

Client thread is identical as in tutorial.

Both server and client thread manages connection same. I created separated threads for accepting messages following this tutorial and one for sending them.

private void manageConnectedSocket(BluetoothSocket socket) {        
    //create thread responsible for sending messages.
    SendingThread w = new SendingThread(socket);
    MainActivity.addSendingThread(w);
    //Creates listener for messages to accept.       
    MainActivity.addListener(socket);
}    

Now in main activity always when user click send button, for each worker (sending thread) send message to remote device. Listening is running asynchronously.

IMPORTANT:

  1. You need to handle exceptions when message send fails and remove sending and recieving thread for device when you detect it is disconected. In my case I used well known UUID "00001101-0000-1000-8000-00805f9b34fb". For every device.
  2. You need to wait 3 second between atempts to connect as client because some devices has weak bluetooth hardware and it is refusing connect as client.

其他推荐答案

Bt connection is supporting up to 7 -10 connections. So you will be limited in that range. I think it is designed for extensions of main device and not for random comunication

Source: search "bluetooth programming" on google