From 51623e5a4f0c92184bf5bbd44c62c3bfe8a18138 Mon Sep 17 00:00:00 2001 From: weiwentan23 Date: Mon, 21 Mar 2022 13:14:05 +0800 Subject: [PATCH 1/4] initiate pairing before connect in connectShimmerThroughBTAddress --- .../ShimmerBluetoothManagerAndroid.java | 99 ++++++++++++++----- 1 file changed, 76 insertions(+), 23 deletions(-) diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/manager/ShimmerBluetoothManagerAndroid.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/manager/ShimmerBluetoothManagerAndroid.java index 3016d182..ca33603c 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/manager/ShimmerBluetoothManagerAndroid.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/manager/ShimmerBluetoothManagerAndroid.java @@ -5,6 +5,9 @@ import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.Context; +import android.content.BroadcastReceiver; +import android.content.IntentFilter; +import android.content.Intent; import android.os.Handler; import android.util.Log; import android.widget.Toast; @@ -91,11 +94,14 @@ public void connectBluetoothDevice(BluetoothDevice device){ /** * @param bluetoothAddress * @param context if the context is set, a progress dialog will show, otherwise a toast msg will show + * @exception DeviceNotPairedException if device is not paired and AllowAutoPairing is disabled */ public void connectShimmerThroughBTAddress(final String bluetoothAddress,Context context) { if(isDevicePaired(bluetoothAddress) || AllowAutoPairing) { if (!isDevicePaired(bluetoothAddress)){ + final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(bluetoothAddress); + device.createBond(); if (context!=null) { //Toast.makeText(mContext, "Attempting to pair device, please wait...", Toast.LENGTH_LONG).show(); final ProgressDialog progress = new ProgressDialog(context); @@ -107,42 +113,89 @@ public void connectShimmerThroughBTAddress(final String bluetoothAddress,Context } else { Toast.makeText(mContext, "Attempting to pair device, please wait...", Toast.LENGTH_LONG).show(); } - } - addDiscoveredDevice(bluetoothAddress); - super.connectShimmerThroughBTAddress(bluetoothAddress); - super.setConnectionExceptionListener(new ConnectionExceptionListener() { - @Override - public void onConnectionStart(String connectionHandle) { - } + BroadcastReceiver receiver; + receiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){ + if (device.getBondState() == BluetoothDevice.BOND_BONDED) { + addDiscoveredDevice(bluetoothAddress); + ShimmerBluetoothManagerAndroid.super.connectShimmerThroughBTAddress(bluetoothAddress); + ShimmerBluetoothManagerAndroid.super.setConnectionExceptionListener(new ConnectionExceptionListener() { + @Override + public void onConnectionStart(String connectionHandle) { + + } + + @Override + public void onConnectionException(Exception exception) { + if (mProgressDialog!=null) { + mProgressDialog.dismiss(); + } + mHandler.obtainMessage(ShimmerBluetooth.MSG_IDENTIFIER_STATE_CHANGE, -1, -1, + new ObjectCluster("", bluetoothAddress, ShimmerBluetooth.BT_STATE.DISCONNECTED)).sendToTarget(); + + } + + @Override + public void onConnectStartException(String connectionHandle) { + if (mProgressDialog!=null) { + mProgressDialog.dismiss(); + } + mHandler.obtainMessage(ShimmerBluetooth.MSG_IDENTIFIER_STATE_CHANGE, -1, -1, + new ObjectCluster("", bluetoothAddress, ShimmerBluetooth.BT_STATE.DISCONNECTED)).sendToTarget(); + + } + }); + } + else if (device.getBondState() == BluetoothDevice.BOND_NONE){ + Toast.makeText(mContext, "Failed to pair device, please try again...", Toast.LENGTH_LONG).show(); + } + } + } + }; + IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED); + filter.addAction(BluetoothDevice.ACTION_FOUND); + mContext.registerReceiver(receiver, filter); + } + else{ + addDiscoveredDevice(bluetoothAddress); + ShimmerBluetoothManagerAndroid.super.connectShimmerThroughBTAddress(bluetoothAddress); + ShimmerBluetoothManagerAndroid.super.setConnectionExceptionListener(new ConnectionExceptionListener() { + @Override + public void onConnectionStart(String connectionHandle) { - @Override - public void onConnectionException(Exception exception) { - if (mProgressDialog!=null) { - mProgressDialog.dismiss(); } - mHandler.obtainMessage(ShimmerBluetooth.MSG_IDENTIFIER_STATE_CHANGE, -1, -1, - new ObjectCluster("", bluetoothAddress, ShimmerBluetooth.BT_STATE.DISCONNECTED)).sendToTarget(); - } + @Override + public void onConnectionException(Exception exception) { + if (mProgressDialog!=null) { + mProgressDialog.dismiss(); + } + mHandler.obtainMessage(ShimmerBluetooth.MSG_IDENTIFIER_STATE_CHANGE, -1, -1, + new ObjectCluster("", bluetoothAddress, ShimmerBluetooth.BT_STATE.DISCONNECTED)).sendToTarget(); - @Override - public void onConnectStartException(String connectionHandle) { - if (mProgressDialog!=null) { - mProgressDialog.dismiss(); } - mHandler.obtainMessage(ShimmerBluetooth.MSG_IDENTIFIER_STATE_CHANGE, -1, -1, - new ObjectCluster("", bluetoothAddress, ShimmerBluetooth.BT_STATE.DISCONNECTED)).sendToTarget(); - } - }); + @Override + public void onConnectStartException(String connectionHandle) { + if (mProgressDialog!=null) { + mProgressDialog.dismiss(); + } + mHandler.obtainMessage(ShimmerBluetooth.MSG_IDENTIFIER_STATE_CHANGE, -1, -1, + new ObjectCluster("", bluetoothAddress, ShimmerBluetooth.BT_STATE.DISCONNECTED)).sendToTarget(); + + } + }); + } } else{ String msg = "Device " + bluetoothAddress + " not paired"; throw new DeviceNotPairedException(bluetoothAddress, msg); } } - @Override public void connectShimmerThroughBTAddress(final String bluetoothAddress) { connectShimmerThroughBTAddress(bluetoothAddress,null); From 57b8ec4caf6ea917a93c5f07c0d974ee402f49ba Mon Sep 17 00:00:00 2001 From: weiwentan23 Date: Mon, 21 Mar 2022 15:16:29 +0800 Subject: [PATCH 2/4] show dialog if pairing fail --- .../manager/ShimmerBluetoothManagerAndroid.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/manager/ShimmerBluetoothManagerAndroid.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/manager/ShimmerBluetoothManagerAndroid.java index ca33603c..26bb169b 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/manager/ShimmerBluetoothManagerAndroid.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/manager/ShimmerBluetoothManagerAndroid.java @@ -2,6 +2,7 @@ import android.app.ProgressDialog; +import android.app.AlertDialog; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.Context; @@ -57,6 +58,7 @@ public class ShimmerBluetoothManagerAndroid extends ShimmerBluetoothManager { ProgressDialog mProgressDialog; + AlertDialog mAlertDialog; private static final String TAG = ShimmerBluetoothManagerAndroid.class.getSimpleName(); private static final String DEFAULT_SHIMMER_NAME = "ShimmerDevice"; @@ -121,6 +123,7 @@ public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){ if (device.getBondState() == BluetoothDevice.BOND_BONDED) { + mProgressDialog.dismiss(); addDiscoveredDevice(bluetoothAddress); ShimmerBluetoothManagerAndroid.super.connectShimmerThroughBTAddress(bluetoothAddress); ShimmerBluetoothManagerAndroid.super.setConnectionExceptionListener(new ConnectionExceptionListener() { @@ -151,7 +154,18 @@ public void onConnectStartException(String connectionHandle) { }); } else if (device.getBondState() == BluetoothDevice.BOND_NONE){ - Toast.makeText(mContext, "Failed to pair device, please try again...", Toast.LENGTH_LONG).show(); + if (context!=null) { + mProgressDialog.dismiss(); + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setMessage("Failed to pair device " + bluetoothAddress + " , please try again...") + .setTitle("Pairing Failed"); + if(mAlertDialog == null){ + mAlertDialog = builder.create(); + } + mAlertDialog.show(); + } else { + Toast.makeText(mContext, "Failed to pair device, please try again...", Toast.LENGTH_LONG).show(); + } } } } From f9f1d5ba19aa05ec4ad04579b49f70ece5302521 Mon Sep 17 00:00:00 2001 From: weiwentan23 Date: Mon, 21 Mar 2022 16:33:53 +0800 Subject: [PATCH 3/4] minor update --- .../android/manager/ShimmerBluetoothManagerAndroid.java | 1 - 1 file changed, 1 deletion(-) diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/manager/ShimmerBluetoothManagerAndroid.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/manager/ShimmerBluetoothManagerAndroid.java index 26bb169b..cf81b5aa 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/manager/ShimmerBluetoothManagerAndroid.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/manager/ShimmerBluetoothManagerAndroid.java @@ -96,7 +96,6 @@ public void connectBluetoothDevice(BluetoothDevice device){ /** * @param bluetoothAddress * @param context if the context is set, a progress dialog will show, otherwise a toast msg will show - * @exception DeviceNotPairedException if device is not paired and AllowAutoPairing is disabled */ public void connectShimmerThroughBTAddress(final String bluetoothAddress,Context context) { From 39563c32cba66fa8ed694fc5f922edc1b6f6f3fe Mon Sep 17 00:00:00 2001 From: weiwentan23 Date: Mon, 21 Mar 2022 16:47:41 +0800 Subject: [PATCH 4/4] minor fix --- .../android/manager/ShimmerBluetoothManagerAndroid.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/manager/ShimmerBluetoothManagerAndroid.java b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/manager/ShimmerBluetoothManagerAndroid.java index cf81b5aa..66a5e897 100644 --- a/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/manager/ShimmerBluetoothManagerAndroid.java +++ b/ShimmerAndroidInstrumentDriver/ShimmerAndroidInstrumentDriver/src/main/java/com/shimmerresearch/android/manager/ShimmerBluetoothManagerAndroid.java @@ -122,7 +122,9 @@ public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){ if (device.getBondState() == BluetoothDevice.BOND_BONDED) { - mProgressDialog.dismiss(); + if(mProgressDialog != null){ + mProgressDialog.dismiss(); + } addDiscoveredDevice(bluetoothAddress); ShimmerBluetoothManagerAndroid.super.connectShimmerThroughBTAddress(bluetoothAddress); ShimmerBluetoothManagerAndroid.super.setConnectionExceptionListener(new ConnectionExceptionListener() { @@ -154,7 +156,9 @@ public void onConnectStartException(String connectionHandle) { } else if (device.getBondState() == BluetoothDevice.BOND_NONE){ if (context!=null) { - mProgressDialog.dismiss(); + if(mProgressDialog != null){ + mProgressDialog.dismiss(); + } AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage("Failed to pair device " + bluetoothAddress + " , please try again...") .setTitle("Pairing Failed");