Android Bluetooth Cross Platform Interoperability - android

I have a Bluetooth service that I programmed for .Net on a Windows machine and I would like my Android 2.1 phone to connect to it. The server is listening for the same UUID which the Android is using to connect. But the connection is failing.
When I try to connect to devices that are not listening for that UUID, I get an exception with the message "Service discovery failed", but when I try to connect to the server that is listening for the right UUID a message box pops up saying:
"There was a problem pairing with bluetooth device."
And I get an exception with the message "Connection timed out." So it looks like the server and the Android are communicating, but there is some sort of failure during handshaking. I know that the Android requires that the server is paired with the phone and also encrypts the communication channel. Does anyone know which specifications are used to do this? I would love to get my server to respond properly to the connection attempt.
Thanks!

I figured out what was going wrong. The problem is that you have to pair the remote device with the Android before you try to make the connection because the pairing dialog does not always work. I am not sure if the bug is on the Android side or on the other device's side, but I was never shown the dialog that asks for the authentication code or that displays the authentication code on either device. So it looks like there are some compatibility issues out there. But if you explictly pair the devices beforehand, then the connection works.

You're right. I'm having the same issue here while trying to connect to an RS232 device with BluetoothChat sample program.
1- If not bonded, I get two errors in a row :
03-03 13:38:20.020: ERROR/BluetoothService.cpp(2077): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session) // Probably because we're calling cancelDiscovery() as a "security" (it's already been cancelled at some point beforehand)
03-03 13:38:20.040: ERROR/BluetoothEventLoop.cpp(2077): onCreateDeviceResult: D-Bus error: org.bluez.Error.AlreadyExists (Device already exists) // This I don't understand
2- Once bonded through Bluetooth Settings, no problem. The connection is made perfectly and the program works.
I tried to check out the Android Source by myself but, given that I'm a newbie, it's kind of tough. If some experienced guy could work it out, it'd be great.

To answer Tomas - The SPP (Serial Port Profile) profile is broken on several HTC Android models, even 2.1 versions, but it is not consistent among the models.
HTC Desire - Does not work.
HTC Legend - Bad, but works for a while?
HTC Hero with custom ROMs - Some work well, some not at all and some badly when doing sound at the same time.

All the previous answers are on the mark; essentially, pairing is required before connect.
However, I was getting the error even after pairing. (I had first tried to connect without pairing).
The workaround was to reboot the phone once. And now it is able to connect to the paired device fine. Sounds simple, but I spent quite some time debugging it. Hopefully useful to someone out there.

Related

How to use go-ble to connect from android to linux

I'm trying to implement a BLE connection between two devices:
A Linux app, as the server, that must advertise services and characteristics
An Android smartphone, as the client, that must connect to the server and read characteristics
The issue comes from my Linux app. When I connect my smartphone to my laptop using my android app and bluetoothctl, it works like a charm. However, when I do the same with my Linux app instead of bluetoothctl, it doesn't works (it actually does, but very rarely).
As my Linux app is written in Go, I use go-ble as the bluetooth package. I have the same issue with their minimal code sample, this is the reason I don't write more code here, but I can't figure out why.
I think I just don't know how to use their library correctly, but the documentation isn't very explicit, so if anyone could help...
EDIT
On my smartphone, when using my apps, I see the laptop when scanning, so The HCI device is up. But when attempting to connect with connectGatt(), I get a 133 status code, which is GATT_ERROR.
One probable issue, is I'm not closing the connection, see this thread, but then how could it works with bluetoothctl?
Bluetoothctl is a bluetooth client working either with BLE and BR\EDR bluetooth. It was working because my android app was connecting through "Classic Bluetooth" with gattConnect.
I solved it by specifying the transport protocol to use, replacing:
device.connectGatt(context, false, gattCallback)
with:
device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_LE)

Adapting Android BluetoothChat example to single server, multiple clients

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?

Android Bluetooth connection issue

I have a EVO (running Android 2.2) and a Sumsung Intercept (running 2.1). I'm trying create a simple chatting application (similar to the BluetoothChat example) but I cannot get the two handsets to connect. I am able to get them through the discovery process such that each handset is able to discover the other, but when I try to connect I always get a "Service discovery failed" error.
What makes this even more interesting is I'm never able to initiate the connection from the 2.2 -> 2.1 (EVO to the Intercept handsets). But, if the EVO attempts to connect first (which will always get the above referenced error), then I attempt to connect from the Intercept, I will at least get the PIN code / Accept Pairing dialogs on both handsets. But, if I attempt to send anything (the write() method) I will get an error stating the 2 handsets are not connected.
Any help would be greatly appreciated.
One of the handsets has to be a listener (waiting with a bluetooth socket) and the other has to make a connection to the one with the socket.
In other words, you appear to be trying to initiate a connection from both devices while neither one is in a state where it can receive an incoming connection (which is the function of a bluetooth socket).

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

Bluetooth Service Name not shown by HTC Wildfire

I'm working on an app that needs to provide a Bluetooth ServerSocket to some other device.
The device I have to work with establishes the connection ignoring the uuid and only looking for devices with correct Service Name.
I open the BluetoothServerSocket with the following command:
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("PWAccesP", uuid);
My problem is, that the ServiceName is not published by the Android SDP. When I query the available services using sdptool, I get the following output:
...
Service RecHandle: 0x10008
Service Class ID List:
UUID 128: 00001101-0000-1000-8000-00805F9B34FB
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 6
So the Service is available, but the service name is not listed and so my device can't connect.
I'm working with an HTC Wildfire with Android 2.1.
My question now is, is this a problem of the HTC Bluetooth stack, is it a problem of the Wildfire or is it a problem of Android 2.1?
Unfortunately I have no other phones available to check so I ask the community to help me out here... Has anybody encountered this before or knows how to get the Service name to show up?
Btw:
I was able to verify that the BluetoothSocketServer connects using a different device that connects by uuid rather than by service name.
Thanks and regards,
Michael
After updating my Wildfire device to Android 2.2, the Bluetooth Service Name is now connected correct. If the fix came from the Bluetooth stack or Android 2.2 is not clear to me but doesn't matter since it's working...
Just tried to work some more with the PWAccessP devices on Wildfire with 2.2 (and service name correctly shown in sdptool). Unfortunately I also wasn't able to establish a connection to the device.
I have two different PWAccessP devices here and both are not connecting to the Wildfire.
Additionally I tried to connect them to my laptop using a server socket perl script I found somewhere. I was able to connect to my laptop using a third device, but the PWAccessP's didn't. I have a short activity shown by my BT-manager but I guess that's only the sdp part, but no pairing/connection is established.
However, I have a running app for Blackberry that successfully co-operates with the PWAccessP-devices, so it must somehow be possible. (I have the source code but it's not really helping for android...)
If somebody finds a solution to this problem, please, please let us know.
Regards,
Michael
And a final answer from my side (if somebody else is still working on this):
I tried it now on a Nexus S with Android 2.3.x and I finally was able to connect the PWAccessP devices. Took me some time to pair and the phone has to be visible all the time, but at least I got it working.
Regards,
Michael
I suspect that the problem lies in the particular hardware below Android, as there are at least some Android phones known to work with these devices (e.g. Motorola Droid).
Anybody tried giving up on the Android API and just using Java with JSR-82 (Bluetooth extensions) to receive data from these devices on Android? It may be true that this hooks in differently than the Android APIs, and might just work...

Categories

Resources