diff --git a/app/build.gradle b/app/build.gradle index a80f346..6658122 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.application' android { compileSdkVersion 25 - buildToolsVersion "25.0.2" + buildToolsVersion '26.0.2' defaultConfig { applicationId "com.fidesmo.ble.client" diff --git a/app/src/main/java/com/fidesmo/ble/client/MainActivity.java b/app/src/main/java/com/fidesmo/ble/client/MainActivity.java index 4f7ce4c..24433e9 100644 --- a/app/src/main/java/com/fidesmo/ble/client/MainActivity.java +++ b/app/src/main/java/com/fidesmo/ble/client/MainActivity.java @@ -4,6 +4,7 @@ import android.annotation.TargetApi; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothHealthCallback; import android.bluetooth.BluetoothManager; import android.content.BroadcastReceiver; import android.content.Context; @@ -99,6 +100,9 @@ public void onReceive(Context context, Intent intent) { } nfcTagDispatcher = TagDispatcher.get(this, this, false, false, false, true, false, true); + + registerReceiver(mBondStateReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED)); + registerReceiver(mPairRequestReceiver, new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST)); } @@ -123,6 +127,9 @@ public void tagDiscovered(Tag tag) { protected void onDestroy() { super.onDestroy(); deviceScanner.stopScan(); + + unregisterReceiver(mBondStateReceiver); + unregisterReceiver(mPairRequestReceiver); } @Override @@ -175,9 +182,56 @@ private void askForBtDevicePermissionsAndFireAction(int requestCode) { enableBluetoothAndAction(requestCode); } + private final BroadcastReceiver mBondStateReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1); + int prevState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1); + String msg = "Bond state change: state " + printBondState(state) + + ", previous state " + printBondState(prevState); + Log.w("Bond state receiver", msg); + log(msg); + } + + private String printBondState(int state) { + switch (state) { + case BluetoothDevice.BOND_NONE: + return "BOND_NONE"; + case BluetoothDevice.BOND_BONDING: + return "BOND_BONDING"; + case BluetoothDevice.BOND_BONDED: + return "BOND_BONDED"; + default: + return String.valueOf(state); + } + } + }; + + private final BroadcastReceiver mPairRequestReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + int variant = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, -1); + String msg = "Pairing request, variant: " + printVariant(variant); + Log.w("Pair request receiver", msg); + log(msg); + } + + private String printVariant(int variant) { + switch (variant) { + case BluetoothDevice.PAIRING_VARIANT_PIN: + return "PAIRING_VARIANT_PIN"; + default: + return String.valueOf(variant); + } + } + }; + @Override public void deviceDiscovered(BluetoothDevice bluetoothDevice) { log("BLE device discovered. Obtaining card information"); + if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_NONE) { + bluetoothDevice.createBond(); + } CardInfoClient client = new CardInfoClient(new BleCard(this, bluetoothDevice)); diff --git a/build.gradle b/build.gradle index b78a0b8..7e01a0a 100644 --- a/build.gradle +++ b/build.gradle @@ -3,9 +3,10 @@ buildscript { repositories { jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.1' + classpath 'com.android.tools.build:gradle:3.0.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -15,6 +16,7 @@ buildscript { allprojects { repositories { jcenter() + google() } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a86f7e6..3c623ee 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Mon May 22 11:51:22 CEST 2017 +#Tue Feb 27 16:39:29 CET 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip