BluetoothGatt Object is Busy - android

I am developing an application using BluetoothGatt class and API for android devices. Application has a feature where user can disconnect one device and connect with another bluetooth device. Basically user can add or remove devices in the app.
To achieve this when user adds a new BLE device i use the same BluetoothGatt object to first disconnect the old device and than connect the new device.
Connection happens nicely but when i start reading characteristics of that device most of the time i get an error something like No Connection for "Bluetooth Address". And when i inspected the BluetoothGatt object is status is Busy.
I am really clueless on what wrong i am doing , can any body help me to understand what wrong i am doing.
Just to mention if i use only one device than everything is working fine.

I fixed this problem, i was using a service to initialize , connect gatt client object , and also the same service for processing data received from gatt client.
So intead of disconnecting and connecting the gatt client again , i unbinded the service , which made sure the gatt client is disconnected and closed. For new device connection i created a new service.
This is now working fine !!

Related

Android: Using BLE without a bespoke GATT Server

I want to use the BLE Current Time Service (0x1805) in conjunction with a BLE enabled device. My question is: Do I "need" to create an App to manage BLE connectivity and return results for the Current Time Service to my device, or does Android somehow implement a generic GATT server?
If so, how do I enable that?
So far, my BLE pairing attempts have simply errored out on the device side.
I'm not aware of a generic GATT Server on Android, but there are apps that can do the job for you. I would suggest starting with the nRF Connect app which is able to start a GATT server with a pre defined current time service.

What is the best approach to implement bluetooth connectivity with multiple devices in Android?

I have to implement bluetooth connectivity with multiple devices(4 devices) from my android java application. App will be continuously receiving data from 2 connected BT (bluetooth) devices, the other 2 BT devices can be connected on need basis & no need to maintain continuous connection. As per the requirements, app has to run continuously for 24 to 48 hours. The device with the app installed will be dedicatedly used for the app & the app will be continuously pushing the bluetooth received data to a remote server. The connected BT devices can be disconnected & reconnected any time from the app. Also if the BT connection is lost, automatic reconnect should happen. What is the best approach to implement this?
Hi i have developed a similar app for BLE Heart rate Belts (Polar H7)
what i did (may not be the best approach but it works smoothly for me as i am able to mangae 5-7 belts at a time) is i made a service to manage the connection with multiple BLE devices
in the service i keep record of all the GATT Callbacks.
so i am using one service in backgorund to keep and mange the connections and separate GATT callbacks for each device in a list or Hashmap .
If a device disconnects or new connection needs to be established i connect/disconnect accordingly and update my record of GATT callbacks .
take a look at the Android BLE sample something similar

BLE GattServices

I'm developing an application that tries to connect my smartphone to a BLE device with Gatt (it can be either a smartphone or a BLE device) and read just a piece of data. This data will dynamically change and I want to keep this data as the value of a characteristic of a service. My main purpose is to read this data from the client side. Since I am connecting with Gatt, I think the only way is to store that data as the value of characteristic.
How can I add a service and a characteristic to the BLE device from the app which I created for the BLE device? I did a research and found some useful information from here. But this helps you create a service for a device you have connected to. Please help me or give me an idea. Thank you very much
Seems to me that what you are trying to do is to connect to a device and add a service on that remote device. That is not have it works and you will not be able to create an app that does this.
In BLE, both sides have a Gatt server. The addService API call in BluetoothGattService is only for adding services to your local Gatt server, not the remote server.
This is how it needs to work:
Your phone is called the central (or master), the device you are connecting to are the peripheral (or slave).
Both sides can define their local Gatt server (typically before they connect). The local Gatt server is mandatory for both sides.
If you can write the firmware running on the peripheral you can certainly create the services and characteristics you need to make your application work (again this is usually as the devices is starting, not after a connection has been made)
Your phone will connect to the peripheral and can read from or write to the Gatt server running on the remote device using the Android Gatt client. The structure however remains unchanged.
To be able to answer more detailed a lot more information on the setup is necessary.
Android (prior to 5.0) doesn't support acting as a peripheral mode you can write applications in order to act as central(master) or Observer mode.
Since Android 5.0, you can use android.bluetooth.le API along with BluetoothGattServer to adopt peripheral role on your Android 5.0+ devices.

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).

Android: Access to an already paired connection

I have a question on accessing already existing paired bluetooth connections...
How can a remote service detect an already paired connection and await a file transfer from it?
In all comments on stackoverflow, on tutorials, ebooks or on google there is always shown how to create a new connection using sockets and so on, but no word about accessing to an existing one.
Is it possible at all? If yes, can you also tell/show me how?
At the moment I have no clue how to get started with it.
Would be great if someone can help me get started :)
Pairing just means that the two devices have authenticated eachother. It does not imply an open socket connection.
After pairing, comes connecting: One side creates a server socket and the other device can then connect to it with a client socket.
I think the confusion is coming from the blending of two concepts: "already paired" with "already connected". Pairing just means they are authenticated. The devices cannot connect without first pairing, but pairing does not connect the devices.
To make an app wait for a file transfer it would go something like this:
Make device A pairable.
From device B, search for nearby devices and when you find A, pair with it (this is where you enter a secret PIN code that has to match on the two devices)
Now that the devices are paired, start your file server on device A or B (It then creates a server socket and awaits incoming connections)
Then initiate a connection to the file server from the other device.
With the sockets open, data can then flow.

Categories

Resources