From 5057cfae5e04b176e611ac6667200930ff65a22a Mon Sep 17 00:00:00 2001 From: stakutis Date: Fri, 13 Mar 2015 14:07:38 -0400 Subject: [PATCH] Added pair/unpair Added support to Pair/Unpair and can specify a PIN --- .../com/megster/cordova/BluetoothSerial.java | 77 ++++++++++++++++++- www/bluetoothSerial.js | 10 +++ 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/src/android/com/megster/cordova/BluetoothSerial.java b/src/android/com/megster/cordova/BluetoothSerial.java index bc0f537d..3a4766a6 100644 --- a/src/android/com/megster/cordova/BluetoothSerial.java +++ b/src/android/com/megster/cordova/BluetoothSerial.java @@ -23,6 +23,7 @@ import org.json.JSONObject; import java.util.Set; +import java.lang.reflect.Method; /** * PhoneGap Plugin for Serial Communication over Bluetooth @@ -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; @@ -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 { @@ -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; } @@ -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) { + 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()); diff --git a/www/bluetoothSerial.js b/www/bluetoothSerial.js index 3f1342bc..6978ca62 100644 --- a/www/bluetoothSerial.js +++ b/www/bluetoothSerial.js @@ -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) {