Android Bluetooth Chat sample app - multiple connections

会有一股神秘感。 提交于 2019-11-30 07:49:21

问题


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?


回答1:


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:

  • Bluetooth peer to peer networks
  • Multiple connections on Android Bluetooth

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..




回答2:


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.



回答3:


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



来源:https://stackoverflow.com/questions/10963614/android-bluetooth-chat-sample-app-multiple-connections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!