[T-Mobile, Samsung Galaxy SIII] Perseus:
Answer to the Perennial Bluetooth problems

Funny I am replying to myself...

I believe there is afterall a potential solution to the bluetooth woes.

-----------------------------Solution to Bluetooth related problems.-----------------------------

Quick background and Symptom -

Typically after an established bluetooth connection, when you either disconnect the accessory or you turn off the car, phone shows a message

"Turning off Bluetooth. "

It is basically stuck at that stage and there is no chance of re-establishing your connection unless you reboot your device. Sometimes it could also happen that the bluetooth actually does turn off for a minute or two and then turns back on again on its own. This will continue until you reboot the device.

The problem mostly appears either in this fashion or a variation of this scenario. If there is no such scenario or bluetooth problems at all in your case, you are very lucky and don't read on.

Analysis and Potential Solutions/Workarounds -

The surface symptom is that, there's this partial wakelock (PWL)

"nfc:ServiceRoutingWakelock"

that comes into play whenever a connection is established. Sometimes, you will see other PWLs such as

StartingObexPbapTransaction

DirectTrack

handleMediaEvent

BluetoothA2dpService - Take a note of this one.

Ok, so back to the original one nfc:ServiceRoutingWakelock. The others may or may not appear but this one will appear most likely. Never mind that it is called NFC. Even if your NFC was turned off completely all along like mine, it will still appear. This is due to potentially the Bluetooth and NFC antenna being co-shared. I tested extensively under varying conditions and I believe, this wakelock is the most likely the culprit for not letting Bluetooth completely turn off or have it turn back on by itself.

There is an easy way (mostly) to make it go away and not have it appear. Turn off the music or call to make sure the connection is not being used and then click on the connection to disconnect. Most of the time, this will turn off the PWL and so will your problem. If it didn't read on.

The other major reason is the way A2DP profiles are implemented by google and their usage.

Bluetooth 101 -

A2DP for the uninitiated is used by vehicles and Headsets to remotely play/pause or browse media on your device. Although this is a nice convenience, there are issues the way it comes into play.

First off, not all vehicles support it. This is attributed to the bluetooth adaptors in these vehicles. They are also present in some of the Bluetooth headsets. If the adaptor is BT v1.1 compliant, it can only support one channel. So you can either have phone calls or media playback but not both at the same time. These adaptors don't typically have A2DP either. So in this case usually the A2DP related issues are not present either. I am not confidant of the specific details here given many manufacturers implement differently.

For those who do have v1.2 compliant adaptors, they come with A2DP profiles. So it allows you to have both phone as well as media playback at the same time. When you pair your phone with such a device, after successful pairing, you will see 2 permissions on your phone under settings for that pairing. You will usually permit both, unless you experience problems with your vehicle's older adaptor.

To clear the verbage, in case of confusion,

Disconnect = explicitly disconnecting that specific connection.

Turn Off = What you referred to as Disabling Bluetooth.

A2DP Related Issues -

Whenever a v1.2 Bluetooth device is connected via bluetooth, it will automatically start playing the audio files on your phone or remote control. This causes your phone to start "Bluetooth Share", a media service. This is the second half of the A2DP that presents your phone as if its a flash storage like a usb drive. The other PWLs I mentioned above are related to this service.

Problem starts if you abruptly terminate an established connection with a v1.2 adaptor, either by turning the vehicle off or turning off phone's bluetooth or even walking out of range. You may wish to see bug thread on Google Code. The Bluetooth Storage service, having established the connection, tries to maintain or retain the connection.

This is not a bad thing. This is useful if there's a random drop off in the connection for some reason. However, depending on your kernel's implementation, sometimes this bluetooth share service does not recognize explicitly broken connection, namely turning off phone's bluetooth. So the service tries to maintain the connection and maintains that wakelock. It in turn retains control over bluetooth as designed. This results in a tug of war and you see the "Closing Bluetooth connection" message and get stuck. It also drains the battery significantly if let to continue.

Potential Workaround -

So when you get stuck with that message and are ready to reboot, try this. Kill Bluetooth share service by going into Settings > Apps > Running Apps (or all apps). That will end the tug of war and your bluetooth will really turn off. You can then turn it on as usual and re-establish. This will save you battery as well.

There is another workaround for those with vehicle connectivity.

More Details in this other bug.

It is my belief that the kernel code &/or rom code needs to take the Explicit Bluetooth turn off into consideration and release the wakelock. For slim rom, I had created a bug on their forum but as of today it has not been responded to.

My experimentation has not completed. I still do not have a satisfactory explanation to stuck connection upon connecting to non-audio bluetooth devices such as ELM II Bluetooth adaptors. So the story continues..........:rolleyes: