Android: I can't get bluetooth paired device - android

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

Related

Auto connect with already paired BT device

Goal:
I'm trying to connect my Android phone to an already paired bluetooth device when its in proximity.
Issue : I am novice to BT technology. I am pairing up my Android phone with a BT device using classic bluetooth API (SPP) as BT device is intent to send large data to mobile app.
The requirement/issue is : Mobile device should automatically connect with already paired BT device when in range. Note that we are using classic BT API and not the GATT.
How does the car audio system automatically connects with driver's phone. I think its the car's BT system who initiate pairing request with phone when car is turn on?
There are a lot of loose ends here. To reconnect to an already paired SPP device depends upon the behavior of the device. If it is passive (always in the slave role) you will have to actively connect to it. I have a passive SPP medical device and to auto reconnect to the device I have to 'continuously' do a discovery in the background (by continuously I mean to a discovery for 5 seconds every 10 seconds or something like that). When I detect the device I create an SPP socket and invoke the connect() method. The alternative is to have a UI that has the user invoke the connect() method when using the device. Hopefully the device is nice and becomes discoverable when it is ready to connect. If it is not discoverable then you need to (yuulk) poll it with connect attempts.
If you have an active device, you can set up a listener socket so the peer can connect to you and you avoid all that background discovery work. At least that is what I have had to deal with.
Yeah by periodic discovery of already paid BT device we can detect and auto pair with them but ar the cost of battery. This is what I had done.

Can I connect bluetooth device automatically with the device which already paired without showing choose dialog

I am developing a solution for connection between OBD2(Vehicle ECU scanner) with the mobile app.
I want that when ever that OBD device is found and it is already paired then the application automatically connect with the device and create a socket for transmission of data . without showing the dialog box to choose a paired device.
You can connect to any paired Bluetooth device, to do so, you have to know device's address and UUID. Embedded devices may have some defined uuid (for example, hc05 adapter), you can get that uuid from documentation.
The tricky part here is that you have to monitor failure and loss of connection so you can retry again. I'm implementing that feature in my home project, fell free to see code: https://github.com/AlexShutov/LEDLights

How to check a Bluetooth remote device has been connected with a different phone in Android?

My android application has to connect to a remote Bluetooth device whose Bluetooth address is known to the app. The application will not be able to connect to the device if it is already connected with a different phone as the remote device can accept connection from a single phone at atime. In such cases, the app has to display an alert of "device is already paired with a different phone". My question is how the app can detect whether the remote device is already connected to a different phone ? I know BluetoothAdapter.startDiscovery() can be used to check whether the device is discoverable. But I am not using this as the app knows the bluetooth address of the device, also I don't want to use it as it is a time consuming process. So is there an alternative method to detect it?

How to build this android app (Bluetooth technology)

I am a little confused on how to build this android app. Here's how it should work. I have a list of paired bluetooth devices. Lets say paired devices are:
1. A bluetooth head set.
2. A mobile phone.
And my app should automatically connect only to my headset when ever it comes in range.
Thats the basic functionality. I have been doing some research on bluetooth technology and its implementation. I came across the bluetooth chat application, when I started using the application I realized that both phones should have the app on it in order to connect and exchange messages. I tried connecting to my headset but it doesnt connect. And also when I removed the bluetooth chat app from one of my device and tried to connect it from a device which had bluetooth chat app, the devices couldnt connect.
If you have already worked on bluetooth this might look very simple. But how can I connect to a bluetooth device from my app when ever its in range even if the other device doesnt have my app.
Thanks.
This will be a 2 sections answer:
-To connect to a Bluetooth Headset, check my answer on here
-To Always connect to that device, you can save device address to memory then create a service with BroadCastReciever that listens for Bluetooth Connectivity. and check which device gets connected and compare it address to the one save in memory once its finds it connected call ibth.connect(device). ibth is inside the above link.

Make Android discoverable to paired bluetooth devices?

I have a Bluetooth module that I'd like to automatically connect with my Android phone. Here's what I've done so far:
The phone and Bluetooth module are paired.
I have an app which creates a BluetoothServerSocket with listenUsingInescureRfcommWithServiceRecord(<uuid>), and then calls accept() on that socket (this is all done in a service).
This works when the Bluetooth module tries connecting to the phone's Bluetooth MAC address.
This also works when under "Bluetooth settings" on the phone, I check the box next to my device to make the phone visible to all devices.
This does not work when the phone is not discoverable to everyone. When the check box is unchecked, it says under my phone "Only visible to paired devices" - my understanding of this is that the phone should be visible to my Bluetooth module, since they are indeed paired... Does anyone have any advice as to why this wouldn't be the case?

Categories

Resources