Android Bluetooth IOException: Connection refused

Good bargain here. I have two bluetooth-enabled Galaxy Nexus phones.

I wrote a Bluetooth connection management application that I use to discover and connect devices. It also displays all available UUIDs that devices can support.

If you look at http://www.bluetooth.org/Technical/AssignedNumbers/service_discovery.htm , the following standard UUIDs will be displayed from Galaxy Nexus devices.

  • 0x1116 - NAP
  • 0x112f - PBAP (Phonebook Access Profile)
  • 0x111f - HFP (hands-free)
  • 0x1105 - OPP (object profile)
  • 0x1112 - HSP (headset profile)
  • 0x110c - AVRCP
  • 0x110a - A2DP

I am trying to connect through an OPP profile ( UUID 00001105-0000-1000-8000-00805F9B34FB ) and click objects (files) between devices. I read all the Android API API documentation on how to detect, establish a pair / connection (streams, etc.) and manage all Bluetooth connections. I was able to successfully connect and talk to an outdated paid device through the SPP profile (0x1101).

However, when I try to use socket.connect() between two phones of the Nexus galaxy, a pairing dialog appears, and I press the Pair button on both devices. After that, I immediately get Connection Refused IOException . Note that after pairing has occurred, I never ask what makes sense, since the secure link is cached.

If I cannot connect to these standard profiles using these standard UUIDs, why are they displayed? How can I connect and interact with any of these profiles from my application? Is it because my application is not so trusted? What is strange is that even the Share functionality on Android does not work either. Is this something completely broken on Android?

Please avoid giving me hints to use the “well-known UUID SPP one 0x1101,” as the docs say. This is not what I want. I have a pretty good idea of ​​how this stuff works, and I'm looking for a real solution or explanation of the problem.

I saw a typical reflection solution, but I don’t understand why this is still a problem on Android? Why do people use reflection to do this job? Can we register an error on Android to fix this?

If these UUIDs are standard, any application should be able to connect and interact with them. Why is this a problem and why am I getting this exception?

Thanks in advance.

UPDATE

So, for some reason, the object began to work in the Android system. I really tried to connect through my application and it did not work. Then I went to the Contacts app and tried to split the contact that worked magically. Then I went back to my application, and now it works ... wow. This is very strange, and there must be an explanation for this.

+7
source share
2 answers

I ran into this problem and was able to find a solution that worked for me.

In my case, I use three different test devices (Nexus 5, Galaxy S4, Note 2), and for some reason, Note 2 will not connect to my Bluetooth module, and the other two will.

I found that the Bluetooth drivers are different, and to create a connection between different devices, slightly different connection methods are needed.

The three methods that I use are called "Secure", "Insecure" and "Reflection method" / "hax".

  switch(connType) { case Secure: tmpSocket = device.createRfcommSocketToServiceRecord(_uuid); break; case Insecure: tmpSocket = device.createInsecureRfcommSocketToServiceRecord(_uuid); break; case Hax: Method createSocket = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); tmpSocket = (BluetoothSocket)createSocket.invoke(device, Integer.valueOf(1)); break; } 

In my case, Secure mode worked for both the Nexus 5 and Galaxy S4, however it did not work for Note 2.

After some testing, I found that Note 2 only works using the "Insecure" mode, so to satisfy this, I basically try to connect and cycle through the various modes if necessary. When I try a different connection mode, I just ask for “reconnect”. Thus, if the connection fails using secure, I will try to use Insecure and then use the reflection method.

I did not come across the case when one of these three methods did not work.

+4
source

Have you tried using a custom profile? those. Custom UUID for your application only. It will also help you find out that you are most likely connecting only to your own application, and not to another application registered with the same profile.

From my experience, the Bluetooth connection is very bad for the first pair attempt. However, using a custom UUID helps with this.

The reflection method (I think) was originally an attempt to correct the error with a specific device, however I think that some people have found success in using it elsewhere. The device was called Spica or something similar.

As one of the comments also posted, I will also try to connect again after the failure.

Basically write code that plans to fail the first attempt, but then the code tries to reconnect after 5 seconds if a failure occurs.

These are imperfect solutions, but the Bluetooth implementation on Android is also imperfect (IMHO). Hope that helps

EDIT

Based on a question update and comments:

I agree that something is definitely a buggy. Part of the problem, in my opinion, depends on the BT drivers, and each one has a different BT stack with different quirks. I also found a question that uses both the AND custom UUID reflection method and other standard methods. It seems extreme to me, but it covers all the bases. Unfortunately, as application developers, we have no control over the low-level stack / code / drivers.

I found with my two Bluetooth sharing apps that the first pairing is always difficult .

I am glad to know this not only to me.

+2
source

All Articles