From 4b41bf0c77d0e448368e573aab5eb643ed6483eb Mon Sep 17 00:00:00 2001 From: Lunarelements Date: Sun, 11 Aug 2019 01:11:54 -0400 Subject: [PATCH] Added support for multiple bluetooth devices --- .../java/me/aflak/bluetooth/Bluetooth.java | 64 +++++++++++++++---- 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/bluetooth/src/main/java/me/aflak/bluetooth/Bluetooth.java b/bluetooth/src/main/java/me/aflak/bluetooth/Bluetooth.java index 9af8a32..7ee4fee 100644 --- a/bluetooth/src/main/java/me/aflak/bluetooth/Bluetooth.java +++ b/bluetooth/src/main/java/me/aflak/bluetooth/Bluetooth.java @@ -47,7 +47,7 @@ public class Bluetooth { private DiscoveryCallback discoveryCallback; private BluetoothCallback bluetoothCallback; - private ReceiveThread receiveThread; + private ArrayList receiveThreads = new ArrayList<>(); private boolean connected, runOnUi; /** @@ -137,10 +137,22 @@ public void disable(){ } /** - * Get BluetoothSocket used for connection. + * Get BluetoothSockets used for connection. + * @return ArrayList. + */ + public ArrayList getSockets() { + ArrayList sockets = new ArrayList<>(); + for (ReceiveThread receiveThread : receiveThreads) { + sockets.add(getThreadSocket(receiveThread)); + } + return sockets; + } + + /** + * Get BluetoothSocket used for connection from thread. * @return BluetoothSocket. */ - public BluetoothSocket getSocket(){ + public BluetoothSocket getThreadSocket(ReceiveThread receiveThread){ return receiveThread.getSocket(); } @@ -288,9 +300,18 @@ public void connectToDeviceWithPortTrick(BluetoothDevice device){ } /** - * Disconnect from bluetooth device. + * Disconnect from all bluetooth devices. */ public void disconnect() { + for (ReceiveThread receiveThread: receiveThreads) { + disconnectThread(receiveThread); + } + } + + /** + * Disconnect from bluetooth device. + */ + public void disconnectThread(ReceiveThread receiveThread) { try { receiveThread.getSocket().close(); } catch (final IOException e) { @@ -315,11 +336,33 @@ public boolean isConnected(){ } /** - * Send string message to the connected device. + * Send string message to all the connected devices. * @param msg String message. * @param charset Charset used to encode the String. Default charset is UTF-8. */ public void send(String msg, String charset){ + for (ReceiveThread receiveThread: receiveThreads) { + sendToThread(msg, charset, receiveThread); + } + } + + /** + * Send string message to all the connected devices. + * @param msg String message. + */ + public void send(String msg){ + for (ReceiveThread receiveThread: receiveThreads) { + sendToThread(msg, null, receiveThread); + } + } + + /** + * Send string message to the connected device. + * @param msg String message. + * @param charset Charset used to encode the String. Default charset is UTF-8. + * @param receiveThread Thread for the connected device. + */ + public void sendToThread(String msg, String charset, final ReceiveThread receiveThread){ OutputStream out = receiveThread.getOutputStream(); try { if(charset==null){ @@ -340,14 +383,6 @@ public void run() { } } - /** - * Send string message to the connected device. - * @param msg String message. - */ - public void send(String msg){ - send(msg, null); - } - /** * Get list of paired devices. * @return List of BluetoothDevice. @@ -478,7 +513,8 @@ public void run() { try { socket.connect(); connected = true; - receiveThread = new ReceiveThread(socket, device); + ReceiveThread receiveThread = new ReceiveThread(socket, device); + receiveThreads.add(receiveThread); if(deviceCallback !=null) { ThreadHelper.run(runOnUi, activity, new Runnable() { @Override