How to get connection status of Bluetooth device for Android - android

How to get the connection status of a bluetooth device for android i.e. "connected", "disconnected", "disconnecting" or "connecting" for the API level 7 onwards?
I am dying searching a lot on this issue but found nothing on regard of this. Neither am I able to disconnect a device which is not connected in my app because I don't know if it is connected or not.
I can know if a device is connected in my App using the Intent.ACTION_ACL_CONNECTED and Intent.ACL_DISCONNECTED , and I get the interrupt or alert when I get a device connected or disconnected.

Unfortunately, there is currently no way to detect whether a bluetooth device is connected. You can only detect connect/disconnect events.

Related

Android: I can't get bluetooth paired device

I can discover devices and createbond. It's working fine.
But when the user close the app and open again I'm already in bonded state and I only need open serial socket but I can't find the device!
GetBondedDevice gives all the device connected before and also the active one.
Because of that I can't understand is it realy connected and ready to open serialsocket!
Serial socket is not always active. I'm closing onDestroy or onPause to stop communicate.
To simplfy the question: I want to check if given mac address is paired and ready to open serial socket.
As you mentioned, paired devices and connected devices are two different things as follows:-
Paired devices: these are devices that have exchanged security keys and are therefore 'linked' to your Android device (this makes the reconnection easier). These can be either connected or unconnected.
Connected devices: these are devices that have an active channel with your Android device that can be used to exchange data.
In your case, to get devices that have an active connection to your device, you can use the getConnectedDevices or getConnectionState.
You can find more references here:-
How can I programmatically tell if a Bluetooth device is connected
Android: List connected Bluetooth devices
List connected Bluetooth devices

In Android Oreo, how to disconnect a connected BluetoothDevice, when my device is the advertiser

We have been dependant on Android's BluetoothLeAdvertiser.stopAdvertising to disconnect all BLE connections. However, in Oreo, this call does not disconnect the devices anymore. Furthermore, even if I close the app, the bluetooth connection still stays up. This is not the expected behaviour.
It seems there are some bluetooth issues in Oreo: https://productforums.google.com/forum/#!msg/phone-by-google/iJeDphsoOJ4/FzR9f2w_BwAJ These might be related?
I dont see anything new in the API-s that might be related to this: https://blog.bluetooth.com/what-bluetooth-developers-should-know-about-android-o
How in Android Oreo can I disconnect from a connected device when my device is the one advertising and accepting connections?

Android 6.0 won't connect to BLE device anymore after turning bluetooth off and back on again

I've written an app that scans for bluetooth smart devices, you can pick one from a list, connect to it and exchange some data. All went fine until Android 6.0.
First, i fixed the new permission system so the app requests the location permission at runtime to be able to scan for devices. With that the app worked like it used to, but after turning bluetooth off and back on again the app won't connect anymore but always returns disconnected state with status code 133 in the gatt callback.
I then tried several things:
rebooted the device
re-installed the app
turn off wifi, enable / disable flight mode
checked with NRF master control panel, same issue with same statuscode
This all didn't solve the problem. The only way i was able to get it working again was the option "Network Settings Reset" from the menu "Backup & reset". Both development phones available here (Moto G3 and Samsung S6) feature this option, haven't seen it before Android 6.0.
My question is: are there others experiencing this problem and is there a fix or workaround for this? This could cause a lot of troubles, it so common to turn off /on bluetooth and the problem also appears after been in flight-mode.
Ok, so after a while i tackled this. I was working with NRF ble devices and could not connect but i was still able to connect to TI sensorTag devices.
There were some differences between the advertisements of the devices, some flags were not enabled for the NRF device and also the NRF device had no device name set. After changing this, i was able to connect again. Turned out that devicename must be enabled but can be left empty.
It seems in the Android 6.0 ble stack there is a change in how the connection to a device is made, the device is not connectable anymore if not the right advertisement settings are used after bluetooth is turned off and on again.
Remember Bluetooth has 4 states, ensure to check when it's already ON and not TURNING_ON:
if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_ON) {
...
}
Else you can't connect yet to the gatt server...

Reconnecting an Android app to a paired bluetooth device

I am developing an Android app that connects beautifully to a Bluetooth device. If that device is turned off, the BT connection is lost, understandably. I would like to reconnect with the device when it is turned back on or comes back in range. Is there an intent sent out by the system when it detects a paired BT device that I can catch with a broadcastreceiver or some other way?
I dont think there is an automatic way to do that.
However, you can have a service scanning periodically for devices and notifying your application when this specific device is found.

is it ok to connect 2 devices concurrently to connect Bluetooth in Android?

Iam connecting 3G phone for my Android device for internet connection and another Socket(SPP) for CAN signal receiving.Whenever I manually connect to the Bluetooth tethering via 3G phone, i can browse internet perfectly and speed is good.Whenever programmatically connect to the 3G connection, i couldn't browse internet.i checked in shell and observing that in my mobile 'device is connected' message displayed and E symbol appears.No problem for me to connect Socket.But I got problem to use 3G phone.For this iam using APN/DUN mode.I checked both but no use.Is there any performance issue here or anything wrong to connect different devices by using Bluetooth API in android.Why this happends while connecting Mobile programmatically.Please give me guidance.
Regards,
Rajendar
Yes, you can have several open connections at a time.
In fact, I've used my hands-free bluetooth on my phone, as it is connected to my ELM327 SPP device streaming at full speed. I didn't notice any hiccups.
As for losing connectivity when you are connected to multiple devices, it's most likely an issue with the DUN provider you're using.
Also it's worth stating that, on Verizon/3g phones, when you use the phone as a phone, it suspends all 3g data services until the call is terminated.
The way L2CAP defines the LT_ADDR allows connecting devices up to 7. Your connection problems is something else.

Categories

Resources