Adapting Android BluetoothChat example to single server, multiple clients - android

I'm attempting to adapt the BluetoothChat example that comes with the SDK to a single-server, multiple-client configuration. I'm having issues and hope someone can help.
I've created two distinct apps, a server and a client, rather than have a single app function as both server and client, as in the example. So the upon starting the app, the server creates a bluetooth service, which creates a new AcceptThread, which listens for incoming connections. I made a couple of modifications to the example code so that the AcceptThread continues to listen, even after the first connection is made (the example stops listening after the one connection is made, because it only needs to establish connections between two devices). I created an ArrayList to handle the multiple connections. Each time a new connection is attempted from a remote device, a new ConnectedThread is created and added to the list.
The first client connects just fine.
When the second client attempts to connect, the connection fails. The log output is:
D/BluetoothEventLoop( 140): Device property changed: <device address> property: Connected value: true
I/BluetoothService( 140): ACL connected, mAclLinkCount = 2
D/BluetoothEventLoop( 140): Device property changed: <device address> property: Connected value: false
I/BluetoothService( 140): ACL disconnected, mAclLinkCount = 1
So it looks like the second connection attempt is successful, but then it is immediately disconnected. This looks like it's happening at a low level, outside anything the code is handling. This happens with a number of different types of test devices, so I don't think it's due to a particular firmware or hardware issue.
Can anyone help with this?
Also, can anyone point to an example of a server->multiple client connection example using bluetooth with Android? I came across an Air Hockey example:
http://code.google.com/p/apps-for-android/source/browse/#svn%2Ftrunk%2FBTClickLinkCompete%2Fsrc%2Fnet%2Fclc%2Fbt
But this project is very glitchy on my test devices and the actual game won't display properly. Does anyone know if there is an example of BluetoothChat extended to handle multiple connections?
UPDATE:
In attempting to solve this issue, I believe I have successfully extended the official Android Bluetooth Chat example to work with a server connecting to multiple clients.
I've uploaded my modifications to github here.
One device acts as the server by selecting "Menu>Make discoverable". Each client then connects by selecting "Menu>Connect a device". I was successfully able to connect my Motorola Droid and HTC Eris as clients to my Samsung Galaxy Tab 10.1". Each message sent from a client displays only on the server. A message sent from the server displays on all clients.
This was a tedious process getting it to work at all. Connectivity with the HTC devices is sporadic, though. I've read elsewhere that bluetooth on HTC is buggy, so I'm chalking it up to that.
Hopefully someone else will find this example useful. If you have any questions, suggestions, or other feedback, please let me know.

I tested your code. As, you said connecting HTC as master was a problem. I made Samsung S3 as master and HTC as client. it worked. But i tried again with a Sony Ericsson phone. No other device could successfully act as master.
S3 has Blutooth version 4.0 other have earlier version. Do you think the problem is because of Bluetooth stack?
I need to use this concept in a project where the devices will be custom made. Trying to find out weather the problem is related to vendors ( HTC, Sony) or its related to bluetooth stack. and i don't know how to find out. This is not documented. :(
Has anyone faced this?

Related

WebRTC android implementation not working when one of the device is connected to WiFi

I have developed a video streaming app that streams video from one android device to another (and not the other way around). When both devices are on their individual LTE network the app works fine. But when one of the devices is connected to WiFi the app fails in the handshaking (exchange of offer and answer) and shows this in logs
D/null localPeerCreation:
onIceConnectionChange() called with:
iceConnectionState = [CLOSED]
(in some cases there is FAILED in place of CLOSED)
I don't know why this is happening that in case of individual LTE it works but with WIFI it doesn't. I have been trying to debug this problem for quite some time but I can't point my finger at anything because there are also very fewer references for android implementation of WebRTC. Please Help.
Edit: I was starting to send the Ice candidates before Xirsys responded with the candidates so the TURN server was never used. So I changed my app such that any handshaking will only happen after the candidates have been received from Xirsys. I know it was pretty dumb.
I am not familiar with Xirsys' TURN servers, but it might not cover all cases need to connect an LTE both clients.
I would check into the following.
What transport types are supported (UDP, TCP and TLS)
Does the TURN server generate listeners for IPv4 and IPv6?
What is the network look like on both devices (IPv4/IPv6, behind a NAT?)
If you can share the candidates on both sides and your RTCConfiguration that would help a lot. Make sure to anonymize your URLs and IPs!

HM-10 as BLE Listener: setting up a brief connection from it

I need to estabilish a brief communication between a BLE master and a BLE peripherical device.
Until I have to use the mobile phone I have no problems, since I select the bluetooth device from Android, I can connect to it and then I can receive data simply writing them to HM-10.
BUT, this approach is battery expensive, since the BLE scanning is more battery expensive than the BLE advertising (right?). So I want that the mobile becames an advertisor and the HM-10 takes the ROLE of Slave.
BUT, I still don't understand if it is possible to connect to other devices: I tried AT+CONN and it always fails (AT+CONF). Is there any way to send brief data from the HM-10 to the advertisor, even without connecting?
Even my firmware looks slightly different from the readme! For example,AT+LN is present in the documentation but it appears to not exist. Does a better version of the firmware for this device exist, or, are sources available somewhere? I installed the HM-Soft V705 (lastest).
Thanks
UPDATE 1:
#ChrisStratton Ok, I have some intresting news: I was already following your idea (phone advertise sometimes, while HM-10 always scan and tries to connect to the service if it sees it, then HM-10 send some data, phone answer). I managed to make the phone visible from HM-10 (don't ask me how!). I am using the BLE Tool Android app to test everything. Now, the central problem now is that HM-10 won't connect to the android service even if the advertisor is connectable (see attached images).
I am sospicious that the AT+CO (that I discovered is a kind a replacement of AT+LN, but I am not sure) can help, but i don't know how.

How to use android as ANCS content provider?

I'm trying to demo android side service (content provider in ANCS lingo), that communicates with embedded device that supports Apple Notification Service (ANCS) notification consumer. Reason for this is, that the embedded device only supports ANCS. As the ANCS BLE side things are publiced by Apple, this should be possible and I've already added that service + associated characterics to android side gatt server. Embedded device is nordic nRF52 and SW is nordic ANCS demo. Demo works OK with iPhone and apparently with the desktop version of nRF control panel (if added with nordic supplied config and if you have windows machine, which I don't).
However I cannot seem to be able to bond the device with android, that's required before connection. The advertisement is in limited discovery mode (before bonding) so I cannot find it via regular scanning from settings. Also bonding from java code doesn't work. With mBluetoothDevice.createBond() I get ACTION_BOND_STATE_CHANGED callback with state 10 (BluetoothDevice.BOND_NONE) with old state 11(BluetoothDevice.BOND_BONDING). Apparently I would need to exchange keys here, but no idea how to do that.
Also dunno if anyone has done this same exercise before, but any other tips would also be appreciated! Thanks!
Problem was that the phone BLE stack was corrupted or in some invalid state. REboot and removing battery solved the issue with the bonding.

Android signalr network change timeout

Hi everyone I have a question for you. I have a project that is using xamarin forms and in this project we are using the signalr client. Whenever I switch from Wi-Fi to cellular data my signal are times out. However the weird thing is that no statechanged events ever fire on a client side not even the disconnect, and I can still send messages with the proxy to other clients but it doesn't receive messages from the other clients. I had a question last week where servicestack was timing out when I switched from Wi-Fi to mobile and the solution was to put a connection header in the request that was set to close. I tried that with the signalr connection but it doesn't work. I've also tried to use the project modernhttpclient and that doesn't work either. On the server side I've even tried saying the keepalive to know so that way it only uses the connection timeout and the disconnect timeout.
Some other weird information about this problem is that it doesn't happen on all Android devices. It seems to work on the Samsung and a different Android device possibly a Nexus device. The reason I know this is that my coworkers have these phones and it works perfectly fine for them. However my device, which is a samsung galaxy s6 edge comma does not work with the code that was implemented for reconnecting when did not work changes

Bluetooth connection only works first time

I’m trying to develop an Android application for a medical device using Bluetooth (SPP). I’ve used the BluetoothChat as a starting point (also see earlier post). Now I think I’m facing a weird problem and I’m also having trouble describing it. Testing on a Nexus S.
Upon the completion of measurement the medical device checks its memory for the Bluetooth address and other information about the previously paired device. If found it will try to initiate a connection (while the application on the phone is listening for a connection) and then transfer data and otherwise it starts an inquiry procedure.
The first time it works fine: pairing procedure, establishing connection and transferring data. When the data is received the application replies with an ACK package and the connection is terminated.
The rest of the times it does not work: after a measurement I can see in the LogCat that the phone receives a signal (just like the first time) from the device, so I know the device saved the phones Bluetooth address. It seems like the system ignores the signal (or at least nothing happens), no connection is established and thus no data transferred even though I'm using accept-method of BluetoothServerSocket the same way as when it works. If I remove the pairing record from the phone this signal starts the pairing procedure and the devices get paired again, BUT they still won’t establish a connection nor transfer data. I guess the message about the signal represents a try to establish a connection but something is wrong. The message in the log is the same as when the connection is successful and can be seen in the LogCat:
03-22 14:21:55.335: ERROR/BluetoothEventLoop.cpp(114): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/4123/hci0/dev_00_A0_96_2D_05_E8
It's rather confusing it's an ERROR message and present in the log when it works as well.
The weird thing is that it works the first time they are successfully paired, but never again afterwards. When the data is transferred and the device receives an ACK it saves the Bluetooth address "and other useful information". The only way I can reproduce the successful ‘first time’ is by pairing the medical device with another device and then afterwards pair it with the application on the phone again.
I’m confused about what is actually causing this problem. Does anyone out there have any clue?
I’m afraid my problem is a bit to specific to be familiar to others if case the problem is caused by the medical device and not the phone. I was thinking it might be related to the Link Key or Channel? But on the other hand, as I said, if I remove the pairing from the phone, I'll get a pairing request again.
The medical device is old, using Bluetooth 1.2. Now it strikes me that I should have a look at if there are any connection-related issues because of this. Could it be that a device using Bluetooth 1.2 have another policy of what information to store when it is paired?
For the moment I feel I'm struggling with a problem I don't fully understand, but if I'll understand later I'll of course get back with my conclusions. I hope I didn't forget important details.
Thanks in advance /F
Hi fredricus My problem is similar to yours i m running a bluetooth service which keeps on listening for data when the bluetooth is on.u have solved the problem of connecting android device to a medical device as u mentioned that u have used reflection.But in case if medical initiates connection will it be able to connect to android device.
Sounds like the problem is with the medical device, that it is not able to re-establish connection (i.e. re-authenticate) with already paired device.
I dont think that being being 1.2 should matter. The procedures are designed to be backward compatible so that all bluetooth versions work with each other.
You may have to get a bluetooth sniffer trace to check what might be going on over the air that will validate if the issue is on the medical device side.
Another option is to check if android has any way to turn on more detailed logging of the Bluetooth interactions and enable that. Default log of Android messages are very high level and does not provide much detail

Categories

Resources