Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for multiple bluetooth devices #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 50 additions & 14 deletions bluetooth/src/main/java/me/aflak/bluetooth/Bluetooth.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class Bluetooth {
private DiscoveryCallback discoveryCallback;
private BluetoothCallback bluetoothCallback;

private ReceiveThread receiveThread;
private ArrayList<ReceiveThread> receiveThreads = new ArrayList<>();
Copy link
Owner

@omaraflak omaraflak Aug 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer the use of List here :

List<ReceiveThread> receiveThreads;

and initialize it with the other variables in the initialize() method :

receiveThreads = new ArrayList<>();

private boolean connected, runOnUi;

/**
Expand Down Expand Up @@ -137,10 +137,22 @@ public void disable(){
}

/**
* Get BluetoothSocket used for connection.
* Get BluetoothSockets used for connection.
* @return ArrayList<BluetoothSocket>.
*/
public ArrayList<BluetoothSocket> getSockets() {
ArrayList<BluetoothSocket> 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();
}

Expand Down Expand Up @@ -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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to able to control each socket separately. We should not disconnect every socket.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you should rename the method disconnectAll()

disconnectThread(receiveThread);
}
}

/**
* Disconnect from bluetooth device.
*/
public void disconnectThread(ReceiveThread receiveThread) {
try {
receiveThread.getSocket().close();
} catch (final IOException e) {
Expand All @@ -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){
Copy link
Owner

@omaraflak omaraflak Aug 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, can you rename it to sendToAll() ?

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){
Expand All @@ -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.
Expand Down Expand Up @@ -478,7 +513,8 @@ public void run() {
try {
socket.connect();
connected = true;
receiveThread = new ReceiveThread(socket, device);
ReceiveThread receiveThread = new ReceiveThread(socket, device);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we should be able to differentiate messages from callbacks. We should know which socket triggered which callback.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need it. Do you have update for this?

bluetooth.setDeviceCallback(new DeviceCallback() {
    @Override
    public void onMessage(BluetoothDevice device, byte[] message) {
    }
}

receiveThreads.add(receiveThread);
if(deviceCallback !=null) {
ThreadHelper.run(runOnUi, activity, new Runnable() {
@Override
Expand Down