Skip to content

Commit 39de97b

Browse files
committed
added setPin method to android
1 parent 40ceb62 commit 39de97b

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor.formatOnSave": false
3+
}

readme.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ This plugin allows you to interact with Bluetooth LE devices on Android, iOS, an
6262
- [requestPermission](#requestpermission)
6363
- [isLocationEnabled](#islocationenabled)
6464
- [requestLocation](#requestlocation)
65+
- [setPin](#setPin)
6566
- [Peripheral Life Cycle](#peripheral-life-cycle)
6667
- [Initilization](#initilization)
6768
- [Notifications](#notifications)
@@ -245,6 +246,7 @@ Neither Android nor iOS support Bluetooth on emulators, so you'll need to test o
245246
* [bluetoothle.stopAdvertising](#stopadvertising)
246247
* [bluetoothle.respond](#respond)
247248
* [bluetoothle.notify](#notify)
249+
* [bluetoothle.setPin](#setPin) (Android)
248250
* [bluetoothle.encodedStringToBytes](#encodedstringtobytes)
249251
* [bluetoothle.bytesToEncodedString](#bytestoencodedstring)
250252
* [bluetoothle.stringToBytes](#stringtobytes)
@@ -286,6 +288,7 @@ Whenever the error callback is executed, the return object will contain the erro
286288
* isNotConnected - Device isn't connected (Don't call discover or any read/write operations)
287289
* isDisconnected - Device is disconnected (Don't call disconnect)
288290
* isBonded - Operation is unsupported. (Is the device Android?)
291+
* setPin - Operation is unsupported. (Is the device Android?)
289292

290293
For example:
291294
```javascript
@@ -1559,7 +1562,32 @@ bluetoothle.isBonded(isBondedSuccess, isBondedError, params);
15591562
}
15601563
```
15611564

1565+
### setPin ###
1566+
Set PIN if required by the pairing process. Android support only.
15621567

1568+
```javascript
1569+
bluetoothle.setPin(success, error, params);
1570+
```
1571+
1572+
#### Params ####
1573+
* address = The address/identifier provided by the scan's return object
1574+
* pin = Pairing PIN code
1575+
1576+
```javascript
1577+
{
1578+
"address": "5A:94:4B:38:B3:FD",
1579+
"pin": "1234"
1580+
}
1581+
```
1582+
1583+
##### Success #####
1584+
* status => string
1585+
1586+
```javascript
1587+
{
1588+
"status": "pinSet",
1589+
}
1590+
```
15631591

15641592
### wasConnected ###
15651593
Determine whether the device was connected, or error if not initialized.

src/android/BluetoothLePlugin.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ public class BluetoothLePlugin extends CordovaPlugin {
129129
private final String keyDiscoveredState = "discoveredState";
130130
private final String keyConnectionPriority = "connectionPriority";
131131
private final String keyMtu = "mtu";
132+
private final String keyPin = "pin";
132133

133134
//Write Types
134135
private final String writeTypeNoResponse = "noResponse";
@@ -421,6 +422,8 @@ public boolean execute(String action, final JSONArray args, final CallbackContex
421422
respondAction(args, callbackContext);
422423
} else if ("notify".equals(action)) {
423424
notifyAction(args, callbackContext);
425+
} else if ("setPin".equals(action)) {
426+
setPinAction(args, callbackContext);
424427
} else {
425428
return false;
426429
}
@@ -2767,6 +2770,66 @@ private void requestConnectionPriorityAction(JSONArray args, CallbackContext cal
27672770
}
27682771
}
27692772

2773+
private void setPinAction(JSONArray args, CallbackContext callbackContext) {
2774+
Log.d("BLE","set pin");
2775+
if (mPairingRequestReceiver!=null) {
2776+
cordova.getActivity().unregisterReceiver(mPairingRequestReceiver);
2777+
}
2778+
2779+
if (isNotInitialized(callbackContext, true)) {
2780+
return;
2781+
}
2782+
2783+
JSONObject obj = getArgsObject(args);
2784+
if (isNotArgsObject(obj, callbackContext)) {
2785+
return;
2786+
}
2787+
2788+
String address = getAddress(obj);
2789+
if (isNotAddress(address, callbackContext)) {
2790+
return;
2791+
}
2792+
2793+
String pin = getPin(obj);
2794+
if (pin==null) {
2795+
return;
2796+
}
2797+
2798+
Log.d("BLE","set pin " + address + " " + pin);
2799+
JSONObject returnObj = new JSONObject();
2800+
try {
2801+
mPairingRequestReceiver = new BroadcastReceiver() {
2802+
@Override
2803+
public void onReceive(Context context, Intent intent) {
2804+
Log.d("BLE", "on receive");
2805+
String action = intent.getAction();
2806+
if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) {
2807+
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
2808+
if(bluetoothDevice.getAddress().equalsIgnoreCase(address)){
2809+
int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
2810+
if (type == BluetoothDevice.PAIRING_VARIANT_PIN) {
2811+
bluetoothDevice.setPin(pin.getBytes());
2812+
abortBroadcast();
2813+
}
2814+
}
2815+
}
2816+
}
2817+
};
2818+
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
2819+
intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
2820+
cordova.getActivity().registerReceiver(mPairingRequestReceiver, intentFilter);
2821+
addProperty(returnObj, keyStatus, "pinSet");
2822+
callbackContext.success(returnObj);
2823+
} catch (Exception e) {
2824+
Log.d("BLE","exception " + e.getMessage());
2825+
addProperty(returnObj, keyError, "setPin");
2826+
addProperty(returnObj, keyMessage, "Failed to set pin");
2827+
callbackContext.error(returnObj);
2828+
}
2829+
return;
2830+
2831+
}
2832+
27702833
@Override
27712834
public void onDestroy() {
27722835
super.onDestroy();
@@ -2777,6 +2840,9 @@ public void onDestroy() {
27772840
if (isBondReceiverRegistered) {
27782841
cordova.getActivity().unregisterReceiver(mBondReceiver);
27792842
}
2843+
if(mPairingRequestReceiver!=null){
2844+
cordova.getActivity().unregisterReceiver(mPairingRequestReceiver);
2845+
}
27802846
}
27812847

27822848
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@@ -2888,6 +2954,8 @@ public void onReceive(Context context, Intent intent) {
28882954
}
28892955
}
28902956
};
2957+
2958+
private BroadcastReceiver mPairingRequestReceiver;
28912959

28922960
@Override
28932961
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
@@ -3903,6 +3971,12 @@ private JSONObject getPermissions(BluetoothGattDescriptor descriptor) {
39033971
return permissionsObject;
39043972
}
39053973

3974+
private String getPin(JSONObject obj) {
3975+
//Get the pin string from arguments
3976+
String pin = obj.optString(keyPin, null);
3977+
return pin;
3978+
}
3979+
39063980
//Bluetooth callback for connecting, discovering, reading and writing
39073981
private BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {
39083982
@Override

types/index.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,18 @@ declare namespace BluetoothlePlugin {
540540
error: (error: Error) => void,
541541
params: NotifyParams): void;
542542

543+
/**
544+
* Set pin if required to pair with a device. Android support only.
545+
* @param success The success callback that is passed with device's status and sent value
546+
* @param error The callback that will be triggered when the bond operation fails
547+
* @param params The set pin params
548+
*
549+
*/
550+
setPin(
551+
success: (result: { status:Status }) => void,
552+
error: (error: Error) => void,
553+
params: { address: string, pin:string }): void;
554+
543555
/**
544556
* Helper function to convert a base64 encoded string from a characteristic or descriptor value into a uint8Array object
545557
* @param value Encoded string which need t be encoded

www/bluetoothle.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ var bluetoothle = {
141141
notify: function(successCallback, errorCallback, params) {
142142
cordova.exec(successCallback, errorCallback, bluetoothleName, "notify", [params]);
143143
},
144+
setPin: function(successCallback, errorCallback, params) {
145+
cordova.exec(successCallback, errorCallback, bluetoothleName, "setPin", [params]);
146+
},
144147
encodedStringToBytes: function(string) {
145148
var data = atob(string);
146149
var bytes = new Uint8Array(data.length);

0 commit comments

Comments
 (0)