Dual SPP bluetooth connexion on android - android

I have been asked to connect 2 concurrent bluetooth SPP devices (Serial port over bluetooth) on an Android tablet.
I used the bluetooth chat as a base to connect one, but I'm lost when it comes to connecting to 2 devices at the same time.
The goal is to collect data from two remote devices and compare the data.
The tablet can not act as a server, it must be client to those devices.
I looked around but did not found any source examples.
If someone could help...
Thanks
Cedric

Finally I cloned the class containing the connection threads and doubled the handler in the main activity.
I also doubled the menu in order to connect to the 2 devices and after a few tweakings, works like a charm !

it really is simple. just do everything 2 times.
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothDevice = bluetoothAdapter.getRemoteDevice(btAddress1);
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
bluetoothSocket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(uuid);
Log.d(TAG, "start connect.");
bluetoothSocket.connect();
Log.d(TAG, "finished connect.");
Log.d(TAG, "getting second adapter");
bluetoothAdapter2 = BluetoothAdapter.getDefaultAdapter();
Log.d(TAG, "getting second adapter success, getting device 2");
bluetoothDevice2 = bluetoothAdapter2.getRemoteDevice(btAddress2);
Log.d(TAG, "getting second device success");
UUID uuid2 = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
bluetoothSocket2 = bluetoothDevice2.createInsecureRfcommSocketToServiceRecord(uuid2);
Log.d(TAG, "start connect 2.");
bluetoothSocket2.connect();
Log.d(TAG, "finished connect 2.");

Related

How to make Android connect after the Bluetooth Adapter has been disabled & reenabled?

I have written an app that connects to a BLE device. The app works OK on most devices; but some devices (most noticeably Huawei P8 Lite and Nexus 6P) refuse to connect after the Bluetooth adapter has been disabled.
This is the test sequence:
Make sure the app is NOT running.
Slide down from the top, disable BT for a couple of seconds, then re-enable bluetooth.
Start the app. The app automatically connects to a bluetooth address stored in the preferences.
Wait for connect. This is where nothing happens on Huawei phones, but other phones, such as Samsung, works like a charm.
Verify from another phone the device is advertising and you can
connect to it.
This is the code I use to connect:
private final Runnable mBeginConnectRunnable = new Runnable() {
#Override
public void run() {
synchronized (GattConnection.this) {
if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
try {
mBluetoothAdapter.cancelDiscovery();
mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(mAddress);
mGatt = mBluetoothDevice.connectGatt(mContext, mBackgroundConnect, mGattCallback);
final boolean connectSuccess = mGatt.connect();
Log.d(TAG, String.format(Locale.ENGLISH, "mGatt.connect(%s, %s) %s",
mAddress,
mBackgroundConnect ? "background[slow]" : "foreground[fast]",
connectSuccess ? "success" : "failed"));
refreshDeviceCache(mGatt);
} catch (Exception ex) {
Log.e(TAG, "Create connection failed: " + ex.getMessage());
setState(State.Closed);
}
} else {
Log.d(TAG, "Can't create connection. Adapter is disabled");
setState(State.Closed);
}
}
}
};
All calls are posted via a Handler to the main thread. I can see it waits for a connect, gives up after 30 seconds at which I call BluetoothGatt.close() on the object and nulls it. It's like nothing is out there.
After some time, later in the day, it works again.
Help is highly appreciated :-)
Update September 14, 2018: After great explanation from Emil I've updated our app and as such don't have this problem on the Nexus. I've noticed the Huawei P8 Lite continues to scan in the background and it seems there is nothing you can do to stop it.
To demonstrate the problems I've made a very simple and clean app that exercise the Bluetooth LE functionality on a phone and used it to demonstrate this problem and also the P8 is broken. The app is available here: https://play.google.com/store/apps/details?id=eu.millibit.bluetootherror
Source is available here: https://bitbucket.org/millibit/eu.millibit.bluetootherror/src/master/
I hope I over time can extend this app to make it a test vehicle for Android documenting all the stange behavior from Android and collect it in a database. In case you are interested in contributing, don't hesitate to drop me a mail on bt.error#millibit.dk
The Android Bluetooth stack has a design flaw in its API. When you connect to a specific device by Bluetooth Device Address, there is no way to tell if you mean a Public address or Random address.
If you start to connect to a device with autoConnect=false which is not bonded and has not recently been seen in a Scan, it will assume you mean a Public address. So if you try to connect to a device having a static random address, it will fail.
To be sure you connect with the correct address type if the device is not bonded, you MUST perform a scan first, find the device and THEN start the connection attempt.

accept() and connect() methods in bluetooth socket

I am getting bluetooth socket connection problem and so tried various alternatives to connect.
It is like if first fails then second and if second also fails then third and so the main UI blocks during the process.
The exceptions are like -
"Service discovery failed" OR "Host is down"
My all three alternatives -
1) Connect with a connect() function
m = bdDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(bdDevice, Integer.valueOf(i));
mBluetoothAdapter.cancelDiscovery();
socket.connect();
2) Connect with a accept() function
m = bdDevice.getClass().getMethod ("listenUsingRfcommOn", new Class [] {int.class});
BluetoothServerSocket returnValue =
(BluetoothServerSocket) m.invoke(bdDevice, new Object [] {29});
socket = returnValue.accept();
3) Connect with a well know SPP UUID
private final UUID my_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
socket = bdDevice.createInsecureRfcommSocketToServiceRecord(my_UUID);
mBluetoothAdapter.cancelDiscovery();
socket.connect();
The second alternative is recently added and so I am not sure that how is it different from first and third one. All will block the UI until connect, all can through IOException.
1) You are connecting as a client to your SPP device. Therefore your device must be expecting a connection before you can actually connect.
2) You are accepting the connection as the server. Your bluetooth device will be responsible for firing the connection requests.
3) You are connecting to a device that uses a particular UUID as it's Serial Port Profile (SPP). Think of this as similar to ports in http protocol. The 00001101-0000-1000-8000-00805F9B34FB, is one of the most common ones. More info : https://developer.bluetooth.org/TechnologyOverview/Pages/SPP.aspx
Additionally, if you are thinking on supporting multiple OS / handset versions. You might see those error messages after an initial broken comm. Android's bluetooth stack (bluez) was totally replaced on 4.2, but you might need to still handle previous known bugs.
Reflection in this case is a good thing (some parts of the bluetooth API wasn't public in older versions). But, in my experience, API level < 17; using createInsecureRfcommSocket instead of createRfcommSocket is much more reliable.
Hope this helps. But if you are building an app for a custom bluetooth spp device of yours, explaining how that devices handles comms could help pointing out the exact root of your problem. Although, bluetooth on Android is never straight forward.

Can't accept an incoming bluetooth connection unless device is discoverable

I've hit my wits end with this one.
I have a typical BluetoothServerSocket that I'm trying to get the A&D Weight scale (UC-321PBT) to connect to. Their company has an app (myFitnessCompanion) which doesn't seem to have many connection issues, yet the scale will only connect to my app if I have the phone 'discoverable'. Mind you this is after I pair with the scale.
The only key code that is really needed here is how the listener is open, beyond that it's typical Bluetooth
serverSocket = adapter.listenUsingRfcommWithServiceRecord("PWAccessP", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
(I have also tried listenUsingInsecureRfcommWithServiceRecord and even reflection to open a port)
I interrogated my phones Bluetooth from another phone to list get the 'PWAccessP' value used by the myFitnessCompanion application, and even used the app to get the scale to pair with my phone.
The scale WILL enter into my socket listener when I make the phone discoverable, but other than that I see the BluetoothDevice begin making the low level connection monitoring the intent 'BluetoothDevice.ACTION_ACL_CONNECTED', but unless I have the phone in a discoverable mode it will not trigger my server socket's 'accept()' method. The pairing screen does not appear on new connections.
Any help would be appreciated. Considering I have to have the phone 'discoverable' for the connection to occur in my app vs the myFitnessCompanion allowing the connection at any time after pairing I can't see how the myFitnessCompanion is getting around this.
Thanks in advance.
A and D device work as Master and the android device as the slave. So the android device must be in discoverable mode. Me tested with both listenUsingRfcommWithServiceRecord and listenUsingInsecureRfcommWithServiceRecord method.
try {
tmp = mBluetoothAdapter
.listenUsingInsecureRfcommWithServiceRecord(NAME,
MY_UUID); // #2
// tmp.accept();
// tmp =
// mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME,
// MY_UUID);
// tmp =
// mAdapter.getRemoteDevice("").createRfcommSocketToServiceRecord(MY_UUID);
mServerSocket = tmp;
mPortNumber = getPortNr();
} catch (IOException e) {
//Log.e("DROID", "listen() failed", e);
flag=false;
return flag;
}

How to programmatically connect 2 android devices with bluetooth?

I am developing an application which should connect 2 Android devices through Bluetooth automatically. Let's say they are already paired. Is it possible to achieve that?
Of course it is possible. I'll make a short tutorial out of the documentation:
Start with the BluetoothAdapter - it is your Bluetooth manager.
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
If bluetoothAdapter is null, it means that this Android device does not support Bluetooth (It has no Bluetooth radio. Though I think it's rare to encounter these devices...)
Next, make sure Bluetooth is on:
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, request_code_for_enabling_bt);
}
If it's not on, we start the activity which asks the user to enable it.
Let's say the user did enable (I guess you should check if he did, do it in your onActivityResult method). We can query for the paired devices:
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
Then loop over them: for(BluetoothDevice device : pairedDevices) and find the one you want to connect to.
Once you have found a device, create a socket to connect it:
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(YOUR_UUID);
YOUR_UUID is a UUID object containing a special ID of your app. Read about it here.
Now, attempt to connect (The device you are trying to connect to must have a socket created with the same UUID on listening mode):
socket.connect();
connect() blocks your thread until a connection is established, or an error occurs - an exception will be thrown in this case. So you should call connect on a separate thread.
And there! You are connected to another device. Now get the input and output streams:
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
and you can begin sending/receiving data. Keep in mind that both actions (sending and receiving) are blocking so you should call these from separate threads.
Read more about this, and find out how to create the server (Here we've created a client) in the Bluetooth documentation.

Android BluetoothSocket.connect() throws IOExceptions "Connection Refused" and "Service discovery failed"

My goal is to write an app that allows me to control my Motorola Xoom with a Playstation 3 Bluetooth Remote Control.
The device is able to be discovered by the native bluetooth app & classified as being a joystick. However, I cannot pair via the native bluetooth app because the app requires a PIN & the device does not have a pin that I am aware of.
So far I am able to programmatically discover the device & create a socket, however all attempts to connect to the device fail.
In both cases:
UUID u = UUID.fromString("00001124-0000-1000-8000-00805f9b34fb");
This is supposed to be the UUID used by HID devices. I also used the method described on another site to verify the UUID is available on the device.
Method1 (many people seem to have issues with this):
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(u);
socket.connect();
Result: IOException "Service discovery failed"
Method2 (the accepted workaround to Method1. I also tried ports 1-100):
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
BluetoothSocket socket = (BluetoothSocket) m.invoke(device, 1);
socket.connect();
Result: IOException "Connection refused"
I have tried this code using 3 devices (plus their UUIDs):
Playstation Remote
00001124-0000-1000-8000-00805f9b34fb
00001200-0000-1000-8000-00805f9b34fb
Nintento Wiimote
00001000-0000-1000-8000-00805f9b34fb
00001124-0000-1000-8000-00805f9b34fb
00001200-0000-1000-8000-00805f9b34fb
Microsoft Bluetooth Number Pad
(which sucessfully pairs, with a pin, via the natvie bluetooth app)
00001000-0000-1000-8000-00805f9b34fb
00001124-0000-1000-8000-00805f9b34fb
00001200-0000-1000-8000-00805f9b34fb
To be able to connect to the devices you have to connect over the HID profile, what you are trying is connecting over SPP (Serial Port Profile) to the UUID for HID etc, this will not work.
In addition these devices have some "custom" HID protocol descriptors that allow it to work with this pre-paired gaming consoles, you will need to get access to those to be able to control the Xoom with these controllers

Categories

Resources