Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Autoconnect fix transparent #14

Open
wants to merge 3 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ public class BluetoothSPP {

private String keyword = "";
private boolean isAndroid = BluetoothState.DEVICE_ANDROID;

private BluetoothConnectionListener bcl;

// This is where we store the callback if AutoConnection is enabled
private BluetoothConnectionListener mBluetoothConnectionListenerSecondary = null;
private int c = 0;

public BluetoothSPP(Context context) {
Expand Down Expand Up @@ -221,7 +222,11 @@ public void handleMessage(Message msg) {
};

public void stopAutoConnect() {
isAutoConnectionEnabled = false;
if (isAutoConnectionEnabled) {
isAutoConnectionEnabled = false;
// Restore the previous callback
mBluetoothConnectionListener = mBluetoothConnectionListenerSecondary;
}
}

public void connect(Intent data) {
Expand Down Expand Up @@ -255,7 +260,12 @@ public void setOnDataReceivedListener (OnDataReceivedListener listener) {
}

public void setBluetoothConnectionListener (BluetoothConnectionListener listener) {
mBluetoothConnectionListener = listener;
// We can't replace the primary callback if AutoConnection is enabled
if (isAutoConnectionEnabled) {
mBluetoothConnectionListenerSecondary = listener;
} else {
mBluetoothConnectionListener = listener;
}
}

public void setAutoConnectionListener(AutoConnectionListener listener) {
Expand Down Expand Up @@ -324,7 +334,6 @@ public void autoConnect(String keywordName) {
if(!isAutoConnectionEnabled) {
keyword = keywordName;
isAutoConnectionEnabled = true;
isAutoConnecting = true;
if(mAutoConnectionListener != null)
mAutoConnectionListener.onAutoConnectionStarted();
final ArrayList<String> arr_filter_address = new ArrayList<String>();
Expand All @@ -337,42 +346,64 @@ public void autoConnect(String keywordName) {
arr_filter_name.add(arr_name[i]);
}
}

bcl = new BluetoothConnectionListener() {

// Save the previously existing callback
mBluetoothConnectionListenerSecondary = mBluetoothConnectionListener;

mBluetoothConnectionListener = new BluetoothConnectionListener() {
public void onDeviceConnected(String name, String address) {
bcl = null;
isAutoConnecting = false;
// Run the secondary callback
if(mBluetoothConnectionListenerSecondary != null) {
mBluetoothConnectionListenerSecondary.onDeviceConnected(name,address);
}
}

public void onDeviceDisconnected() { }
public void onDeviceDisconnected() {
// Run the secondary callback
if(mBluetoothConnectionListenerSecondary != null) {
mBluetoothConnectionListenerSecondary.onDeviceDisconnected();
}

}
public void onDeviceConnectionFailed() {
Log.e("CHeck", "Failed");
if(isServiceRunning) {
if(isAutoConnectionEnabled) {
c++;
if(c >= arr_filter_address.size())
c = 0;
isAutoConnecting = true;
connect(arr_filter_address.get(c));
Log.e("CHeck", "Connect");
Log.e("CHeck", "Connect");
if(mAutoConnectionListener != null)
mAutoConnectionListener.onNewConnection(arr_filter_name.get(c)
, arr_filter_address.get(c));
} else {
bcl = null;
isAutoConnecting = false;
}

// Run the secondary callback (can't fail is AutoConnection is disabled)
if(mBluetoothConnectionListenerSecondary != null) {
mBluetoothConnectionListenerSecondary.onDeviceConnectionFailed();
}

}
}
};

setBluetoothConnectionListener(bcl);
c = 0;
if(mAutoConnectionListener != null)
mAutoConnectionListener.onNewConnection(arr_name[c], arr_address[c]);
if(arr_filter_address.size() > 0)
connect(arr_filter_address.get(c));
else
if (arr_filter_address.size() > 0) {
if (!isConnected) {
// Connect() breaks when it's already connected
if(mAutoConnectionListener != null)
mAutoConnectionListener.onNewConnection(arr_name[c], arr_address[c]);
isAutoConnecting = true;
connect(arr_filter_address.get(c));
}
} else {
Toast.makeText(mContext, "Device name mismatch", Toast.LENGTH_SHORT).show();
}
}
}
}