Ble multiple connection - android

is it possible to connect multiple sensor tags via Bluetooth low energy? In the app provided by TI, the sensor tag is the server, and app is client. So, it is possible for a single client to connect multiple servers?

YES, it is possible to connect from one application to multiple BLE devices
On the other side, at at the moment, Android BLE stack (BlueDroid) seems to have synchronous nature (may be it is inspired by Samsung stack from 4.2 devices , where this behaviour was documented as discuseed here Has native Android BLE GATT implementation synchronous nature?).
This mean, that if one operation is in progress, othes are ignored, so you have to implement e.g., queue of requests to be sure that just one characteristic read/write is in progress.
More over, Andorid BLE stack is at the moment quite unstable, as discussed in many threads, e.g, here Android 4.3 Bluetooth Low Energy unstable
Hope this helps

You're terminology doesn't seem to be quite correct, but if you're trying to ask whether it's possible to connect multiple senrors or similar to one phone, the answer to that should be yes.
I'd recommend you to take a look at this and this, to better understand the roles in BLE.

Related

Transfer Data Using BLE with same application with in different android device

I need to transfer data from android phone to each other through my application using BLE. When a device comes near to me and my application is already installed in it, So my phone will establish a connection and send some data in strings and also receive from that device without notify user. Is it possible to do with BLE and I have studied BLE from
https://developer.android.com/guide/topics/connectivity/bluetooth-le
But I am new so I dont know from where to start and what should i have to do? Kindly let me know about BLE more easy for my better understanding and refer me some tutorials where I can learn that how will fulfill my application functionalities.
Thanks
I suggest starting with the Central-Peripheral example. Here you have an simple application:
https://github.com/itanbp/android-ble-peripheral-central
And tutorial with basis:
https://www.bignerdranch.com/blog/bluetooth-low-energy-on-android-part-1/
In the case you described, if there are not defined roles for the android phones (master, slave), the program will have to work in alternately in two modes: central and peripheral.
This is possible form the BLE side. However, there may be a problem with the application wroking as a background service. Since Android 8 there are new restrictions and limitations in background sericves:
https://developer.android.com/about/versions/oreo/background
Bluetooth Low Energy on Adnroid is very troublesome technology. A lot depends on the version of Android, processor and antenna system of the phone.

How to execute the BluetoothGatt read and write requests same time for all connected devices?

I want to connect multiple ble device and do read write operation parallely?
Our experience with BLE on Android was clearly indicating the multi threaded operations on the Android BLE stack can cause many issues.. ;)
I would suggest you to do one thing at a time and try to avoid parallel work...
Our 2 cents...
To be able to connect to multiple devices at the same time, you have to use the auto connect gatt feature, rather than direct connect (http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#connectGatt(android.content.Context,%20boolean,%20android.bluetooth.BluetoothGattCallback)).
If you use auto connect, the phone can initiate multiple connections at the same time. If you use direct connect, only one connection can be initiated at the same time.
Once you are connected however, you can execute all gatt operations in parallel, although you can only have one outstanding gatt operation at a time per peripheral.
There are several libs, that give you a possibility to do this, without too much hassle.
RXAndroidBLE is a great piece of work for example. But if you're using Kotlin - there is something easier: BleGattCoroutines.
Basically it uses the Kotlin's coroutines to wrap async (and ugly) Android BLE API into the plain sync-style calls.
Disclaimer: I don't use this lib itself, but use the same approach in my recent commercial embedded project. And it works like a charm.
This is one of the better repos I've found for working with the BLE stack in Android. But it doesn't give parallel processing of the operations. I think if you're working at the SDK level, there really is no way to do parallel operations because you don't have any low level control.
There is a new RXAndroidBLE library, I haven't used it, but it may be helpful. There was some discussion about it here. They apparently were able to connect 5 sensors simultaneously and process data...Although it would have still been sequential.

How to use the Bluetooth Low Energy Services?

Want to build a chat app that can only communicate with BLE supported devices.
How to initialize the BLE service and how to pair with a device so that it can receive messages.
Great idea!
The first step is to "discover" nearby devices so that you can decide to which device you want to connect. One important issue on Android is that many Android phones don't support the BLE Peripheral mode which means you can not connect and exchange with them over BLE. In this case, you would need to use another technology and this complicates things a lot.
A possible solution would be to have a look at some of the SDK's available that can do discovery for you and reduce the complexity of dealing with the BLE stack: such as google nearby, p2pkit.io or newaer.
Hope this helps...
Disclaimer: I'm one of the engineers working on p2pkit.io

Android: wifi & bluetooth searching together

Currently I have a question about using Android Wifi, Bluetooth APIs together.
(Please be noted that it's a problem on app development, not for a normal user guide)
I have a trouble when search device' services via Wifi at the same time with searching device via Bluetooth.
For more info:
On wifi search, I use Bonjour protocol (an Apple's implement of Zeroconf which can help locating services that local network devices offer). The Java implement that I use is jmdns. During the search session, I create a MulticastLock in order to receive multicast packages of Bonjour.
On bluetooth search, I just use BluetoothAdapter together with BroadcastReceiver to get bluetooth device info.
Problem happens to the search result, whenever two search sessions above are executed at the same time, mysearch result on Bonjour (Wifi) search has never get enough like it does when only it is executed.
Ex: Bonjour search just gets 1 instead of 2 device' services when search together with Bluetooth. Bluetooth search result is always enough, however.
This is tried many times and I surely confirm that there is nothing related to network' strength problems.
Has anyone met this problem yet?
Even if your situation is about using Wifi together with Bluetooth, any experiences or guests are very helpful to me!
Khoi.
I am doing pretty much the same stuff you are doing.
My experience differs in that:
I do not use Bluetooth discovery, as I know what device I want to connect to. I just initiate a client connection.
I use Asynctasks for background operations. What are you using?
Two possible issues:
Multiple Asynctasks not executing in parallel due to Android faulty implementation. I have had a similar bug after setting my Android API target to the latest SDK (as it is best practice). Now, after API13, Android Asynctask implementation changed and tasks are NO LONGER executed in parallel (so my wifi thread never started for example, if bluetooth thread was running!!!). The fix is to put a couple of lines of code to make the default executor parallel again, or to just use API11 or lower as target version. See this thread for details.
OverTheAir interference between the 2. Bluetooth discovery is, to my knowledge, the most expensive operation you could do on Bluetooth. So that 2.4GHz multi-purpose chip may be swamped over the air. Internally it has coexistence between WiFi and BT. Remember the WiFi and Bluetooth channels over the 2.4GHz overlap. So what I am saying doing discovery over the same main frequency at the same time might not work that great. You should investigate that...

Android Bluetooth Chat sample app - multiple connections

Is it possible to set up the Android Bluetooth Chat sample app to connect more than one person at a time, and have a mini chat room? What would that entail?
tl;dr version: Bluetooth sucks for this, don't use it, use wifi instead, probably backed by a web backend.
I have investigated this issue thoroughly throughout the years in the interests of a social wireless network research project. My general advice is: it doesn't work with more than two / three people. Bluetooth just isn't designed with wireless peer to peer networks in mind.
In general, it seems that the cheap Bluetooth controllers included on Android devices (especially HTC's devices, iirc) don't really handle any more than two or three connections at a time. I'm unsure if this is a hardware or firmware problem, but I can recount some basic anecdotes. I was working to implement this idea at the SDK level (i.e., without firmware modifications) around the beginning of 2011, and was able to get a peer to get two additional connections (i.e., three devices, each connecting to the other two) to work for a period of a few minutes to an hour before the connections would suddenly die and the socket would become unusable, requiring reconnection. Unfortunately, 20 minutes was an upper bound, and generally it was impossible to get connections to more than one other device at all reliably.
The goal of the project was to support multiple people interacting with each other silently in the background, but this never materialized, instead we ditched Bluetooth and went with wifi instead, which worked much much better. In the abstract, I think people view Bluetooth as a possible medium for reliable peer to peer communication, but it wasn't really designed that way: it's more of a medium used for short range communication between small devices (think headsets).
Be aware that if you want to do this, the maximum number of devices to which you can connect is fixed, because as per the Bluetooth spec, a piconet supports a maximum of seven devices. (See the wikipedia article.)
The required change is simple: you use a different UUID for each device. This can be implemented a number of ways, using an out of band exchange mechanism, or simple scheme where you assign UUIDs in an increasing fashion and when connecting to the network, try each in succession.
Here are some relevant Google groups threads:
Bluetooth peer to peer networks
Multiple connections on Android Bluetooth
I remember posting a more elaborate one detailing how to do this (with code) that I might dig up as well.., if I can find it. It should be from late 2010 or early 2011.
So the answer is, in the abstract, yes, you can try to do this, by using multiple UUIDs (after you use one, that's it, and you have to try another using some assignment protocol). However, in practice, after a lot of trial and error, this doesn't really work for what you probably want to use it for, and it's a lot better to go with an internet backend instead. By the way, this is also good for another reason, most users don't really like to turn on their Bluetooth for fear of their battery being drained..
Leaving this here, in case it helps someone else.
I was able to make my custom chat room following official bluetooth tutorial and modifying it a little.
Unfortunately, I cannot provide most of my code, but main idea is:
Every device is acts both as server and as a client. When Chat is started, device starts its server thread. Server thread is the same as official but doesn't ends when accept connection. It just keep listening.
Client thread is identical as in tutorial.
Both server and client thread manages connection same. I created separated threads for accepting messages following this tutorial and one for sending them.
private void manageConnectedSocket(BluetoothSocket socket) {
//create thread responsible for sending messages.
SendingThread w = new SendingThread(socket);
MainActivity.addSendingThread(w);
//Creates listener for messages to accept.
MainActivity.addListener(socket);
}
Now in main activity always when user click send button, for each worker (sending thread) send message to remote device. Listening is running asynchronously.
IMPORTANT:
You need to handle exceptions when message send fails and remove sending and recieving thread for device when you detect it is disconected. In my case I used well known UUID "00001101-0000-1000-8000-00805f9b34fb". For every device.
You need to wait 3 second between atempts to connect as client because some devices has weak bluetooth hardware and it is refusing connect as client.
Bt connection is supporting up to 7 -10 connections. So you will be limited in that range. I think it is designed for extensions of main device and not for random comunication
Source: search "bluetooth programming" on google

Categories

Resources