Skip to content
Closed
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 @@ -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;
Expand Down Expand Up @@ -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";

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down