i try to create an Android App that connects to two Bluetooth HC-05 Modules (which are connected to an Arduino).
I am able to connect to both of them and if i only send Data to one of them everything works fine, if im sending Data to both however it might work for some Time but than it stops sending to one Device.
On one Phone this leads to the App crashing, on another Phone nothing (no Exception or notification that the connection was aborted) happens.
So i checked the Internet and in the most Part it says you can connect to two Bluetooth Devices at once (e.g. here).
I cannot figure out what the reason for this error is. But i have some ideas. Maybe someone can confirm then, or knows the real Problem or better yet a solution:)
Since i use two HC-05 Bluetooth Modules they have the same UUID, which i belive may be problematic because that this UUID is not uniqe to one device. But im not sure if thats the issue. Maybe someone knows how to change the UUID of the HC-05?
Note that im am sending data at random intervalls to both machines. So i think that could cause the issue. That at some point the App tries to send to two Devices at once and that causes trouble. So i am thinking about making some kined of workaround for this. But im not sure how. Maybe some Stack in which i put the Data and the device it needs to send to and then i send them Item by item with maybe some delay in between. So the sending of Data is somewhat 'synchronised'. However i dont know if this is a good solution and im thankful for any advice i could get.
So right now i connect to the Devices like that:
public void connect(String mac){
Set<BluetoothDevice> bondedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
for (BluetoothDevice devices : bondedDevices)
if (devices.getAddress().equals(mac)) {
this.device = devices;
break;
}
if (this.device == null) {
//some error handling
} else {
UUID id= UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
try {
this.socket = this.device.createRfcommSocketToServiceRecord(id);
this.socket.connect();
this.outputStream = this.socket.getOutputStream();
final InputStream st = this.socket.getInputStream();
//some more init stuff
} catch (Exception e) {
e.printStackTrace();
this.disconnect();
}
}
}
And i am sending data like this (without excess code):
byte[] send = new byte[9];
//fill Data
try {
this.outputStream.write(send);
this.outputStream.flush();
} catch (Exception e) {
//Displaying Toast with Error message
Toast.makeText(DirtyCode.appcont, e.getMessage(), Toast.LENGTH_LONG).show();
Log.e("SEND EXCEPTION:" + device.getName(), e.getMessage());
e.printStackTrace();
return false;
}
So i was also thinking wheater i might need to use BluetoothServerSocket somehow. But im not sure about that.
The connection works perfectely with one Device connected only or two Devices connected but only sending to one Device (Sending very ocasionaly Data to the second one works usually very long before causing trouble).
I tried solving this for a long time but i cant get it working, so any help is creatly apretiated.