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..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 @@ -2,9 +2,13 @@ import android.app.ProgressDialog; +import android.app.AlertDialog; 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; @@ -54,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"; @@ -96,6 +101,8 @@ public void connectShimmerThroughBTAddress(final String bluetoothAddress,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 +114,105 @@ 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) { + if(mProgressDialog != null){ + mProgressDialog.dismiss(); + } + 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){ + if (context!=null) { + 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"); + if(mAlertDialog == null){ + mAlertDialog = builder.create(); + } + mAlertDialog.show(); + } else { + 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);