Skip to content

Commit

Permalink
Adding new functionality to issue PIN codes for peripherals with a fi…
Browse files Browse the repository at this point in the history
…xed PIN code
  • Loading branch information
martijnvanwelie committed May 8, 2020
1 parent 2b4f2d3 commit 747f308
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 13 additions & 0 deletions blessed/src/main/java/com/welie/blessed/BluetoothCentral.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public class BluetoothCentral {
private final Map<String, Integer> connectionRetries = new ConcurrentHashMap<>();
private boolean expectingBluetoothOffDisconnects = false;
private Runnable disconnectRunnable;
private Map<String, String> pinCodes = new ConcurrentHashMap<>();

//region Callbacks

Expand Down Expand Up @@ -289,6 +290,11 @@ public void run() {
}
});
}

@Override
public String getPincode(BluetoothPeripheral device) {
return pinCodes.get(device.getAddress());
}
};

//endregion
Expand Down Expand Up @@ -902,6 +908,13 @@ private void cancelAutoConnectTimer() {
}
}

/**
* Add a fixed PIN code for a peripheral
*/
public void addPinForPeripheral(String peripheralAddress, String pin) {
pinCodes.put(peripheralAddress, pin);
}

/**
* Remove bond for a peripheral.
*
Expand Down
12 changes: 11 additions & 1 deletion blessed/src/main/java/com/welie/blessed/BluetoothPeripheral.java
Original file line number Diff line number Diff line change
Expand Up @@ -717,8 +717,16 @@ public void onReceive(final Context context, final Intent intent) {
return;

// String values are used as the constants are not available for Android 4.3.
final int variant = intent.getIntExtra("android.bluetooth.device.extra.PAIRING_VARIANT"/*BluetoothDevice.EXTRA_PAIRING_VARIANT*/, 0);
final int variant = intent.getIntExtra("android.bluetooth.device.extra.PAIRING_VARIANT", 0);
Timber.d("pairing request received " + ", pairing variant: " + pairingVariantToString(variant) + " (" + variant + ")");

if (variant == PAIRING_VARIANT_PIN) {
String pin = listener.getPincode(BluetoothPeripheral.this);
if (pin != null) {
Timber.d("Setting PIN code for this peripheral using '%s'", pin);
device.setPin(pin.getBytes());
}
}
}
};

Expand Down Expand Up @@ -1742,6 +1750,8 @@ interface InternalCallback {
*/
void disconnected(BluetoothPeripheral device, final int status);

String getPincode(BluetoothPeripheral device);

}

/////////////////
Expand Down

0 comments on commit 747f308

Please sign in to comment.