diff --git a/library/src/main/java/app/akexorcist/bluetotohspp/library/BluetoothSPP.java b/library/src/main/java/app/akexorcist/bluetotohspp/library/BluetoothSPP.java index 869a98b..15a908e 100644 --- a/library/src/main/java/app/akexorcist/bluetotohspp/library/BluetoothSPP.java +++ b/library/src/main/java/app/akexorcist/bluetotohspp/library/BluetoothSPP.java @@ -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) { @@ -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) { @@ -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) { @@ -324,7 +334,6 @@ public void autoConnect(String keywordName) { if(!isAutoConnectionEnabled) { keyword = keywordName; isAutoConnectionEnabled = true; - isAutoConnecting = true; if(mAutoConnectionListener != null) mAutoConnectionListener.onAutoConnectionStarted(); final ArrayList arr_filter_address = new ArrayList(); @@ -337,14 +346,26 @@ 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) { @@ -352,27 +373,37 @@ public void onDeviceConnectionFailed() { 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(); + } } } }