Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pair/unpair #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
77 changes: 76 additions & 1 deletion src/android/com/megster/cordova/BluetoothSerial.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.json.JSONObject;

import java.util.Set;
import java.lang.reflect.Method;

/**
* PhoneGap Plugin for Serial Communication over Bluetooth
Expand All @@ -48,6 +49,8 @@ public class BluetoothSerial extends CordovaPlugin {
private static final String SETTINGS = "showBluetoothSettings";
private static final String ENABLE = "enable";
private static final String DISCOVER_UNPAIRED = "discoverUnpaired";
private static final String PAIR = "pair";
private static final String UNPAIR = "unpair";

// callbacks
private CallbackContext connectCallback;
Expand Down Expand Up @@ -77,6 +80,7 @@ public class BluetoothSerial extends CordovaPlugin {
StringBuffer buffer = new StringBuffer();
private String delimiter;
private static final int REQUEST_ENABLE_BLUETOOTH = 1;
private String PIN; // If needed for pairing

@Override
public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
Expand Down Expand Up @@ -200,7 +204,14 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback

discoverUnpairedDevices(callbackContext);

} else {
}
else if (action.equals(PAIR)) {
pairRequest(args, callbackContext);
}
else if (action.equals(UNPAIR)) {
unpairRequest(args, callbackContext);
}
else {
validAction = false;

}
Expand Down Expand Up @@ -276,6 +287,70 @@ public void onReceive(Context context, Intent intent) {
bluetoothAdapter.startDiscovery();
}

protected byte[] getPin() {
return PIN.getBytes();
}

private void unpairRequest(CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
String macAddress = args.getString(0);
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(macAddress);
Log.d(TAG,"unpairRequest function");
try {
Method removeBond = BluetoothDevice.class.getMethod("removeBond");
if (!(Boolean)removeBond.invoke(device)) {
Log.d(TAG,"Must already be unpaired!");
}
callbackContext.success("Unpaired");
} catch (Exception e) {
Log.e("unpairRequest", "Exception", e);
callbackContext.error("Unpair exception:"+e);
}
}



private void pairRequest(CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
String macAddress = args.getString(0);
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(macAddress);
try {
PIN=args.getString(1);
} catch (JSONException e) {
PIN="";
}
Log.d(TAG,"pairRequest; mac:"+macAddress+" PIN:"+PIN);
final BroadcastReceiver pairingReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, 0);
Log.d(TAG,"action:"+action +" bondState:"+bondState+" 12=Bonded 11=Bonding 10=None");
if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) {
Log.d(TAG,"Setting PIN");
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
byte[] pinBytes = getPin();
device.setPin(pinBytes);
device.setPairingConfirmation(true);
}
if (bondState == BluetoothDevice.BOND_BONDED)
callbackContext.success("PAIRED correctly");
else
if (bondState == BluetoothDevice.BOND_NONE)
callbackContext.error("PAIR failed because of state:"+bondState);
if (bondState == BluetoothDevice.BOND_BONDED || bondState == BluetoothDevice.BOND_NONE)
cordova.getActivity().unregisterReceiver(this);
}
};
Activity activity = cordova.getActivity();
activity.registerReceiver(pairingReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
activity.registerReceiver(pairingReceiver, new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST));
activity.registerReceiver(pairingReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));

Log.d(TAG,"About to createBond()");
if(device.createBond() != true) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If device is not bonded, Dont we need to creatBond here? with device.createBond()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass and stow

Log.d(TAG,"Must already be bonded/paired!");
callbackContext.success("PAIRED already");
}
}

private JSONObject deviceToJSON(BluetoothDevice device) throws JSONException {
JSONObject json = new JSONObject();
json.put("name", device.getName());
Expand Down
10 changes: 10 additions & 0 deletions www/bluetoothSerial.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,18 @@ module.exports = {

discoverUnpaired: function (success, failure) {
cordova.exec(success, failure, "BluetoothSerial", "discoverUnpaired", []);
},

pair: function (macAddress, pin, success, failure) {
cordova.exec(success, failure, "BluetoothSerial", "pair", [macAddress, pin]);
},

unpair: function (macAddress, success, failure) {
cordova.exec(success, failure, "BluetoothSerial", "unpair", [macAddress]);
}



};

var stringToArrayBuffer = function(str) {
Expand Down