I have an Android tablet that acts as a Bluetooth server and allows up to 6 clients to connect (I set this limit, knowing that the theoretical maximum is 7, as per the Bluetooth piconet specification). I tried with two different tablet models from the same manufacturer: Huawei Mediapad T3 and Huawei MediaPad T5. On the T3, I can get up to 6 connections that remain stable and allow communicating almost seamlessly with the clients. On the T5, however, the tablet will not accept more than 4 simultaneous connections. If I disconnect one device, one of the two remaining ones which keep requesting to connect gets accepted in its place. I do not get any error messages from the calls to the accept() method, or any other errors regarding Bluetooth in the Android Logcat.
Both tablets run under EMUI 8.0.0 (the Huawei brew of Android 8.0.0).
Here is the code I run for the thread that accepts the connections :
private class AcceptThread extends Thread {
private BluetoothServerSocket serverSocket;
AcceptThread(String name, String uuidStr) {
btAdapter.cancelDiscovery();
try{
serverSocket = btAdapter.listenUsingInsecureRfcommWithServiceRecord(name, UUID.fromString(uuidStr));
} catch (IOException ex){
onStatusListener.OnStatus("server.error.COULD_NOT_LISTEN");
Log.e(TAG, "Could not open socket", ex);
}
}
public void run() {
onStatusListener.OnStatus("server.listening");
while (connections.size() < maxConnections) {
setState(STATE_LISTENING);
try {
BluetoothSocket socket = serverSocket.accept();
if (socket != null) {
ConnectedThread conn = new ConnectedThread(socket);
setState(STATE_CONNECTING);
conn.start();
connections.add(conn);
conn.setOnDisconnectEvent(() -> {
onStatusListener.OnStatus("server.disconnected." + socket.getRemoteDevice().getAddress());
connections.remove(conn);
});
onStatusListener.OnStatus("server.connected." + socket.getRemoteDevice().getAddress());
}
} catch (IOException ex) {
onStatusListener.OnStatus("server.error.COULD_NOT_ACCEPT");
Log.e(TAG, "Socket's accept() method failed", ex);
break;
}
}
onStatusListener.OnStatus("server.not_listening");
cancel();
}
void cancel(){
try {
setState(STATE_NONE);
serverSocket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close server socket", e);
}
}
}
What can cause my code to work on the T3 tablet and not on the T5?
Is there anything I could do differently to get my 6 simultaneous connections working with both tablets?