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

Testing pairing #11

Open
wants to merge 2 commits 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
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
54 changes: 54 additions & 0 deletions app/src/main/java/com/fidesmo/ble/client/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}


Expand All @@ -123,6 +127,9 @@ public void tagDiscovered(Tag tag) {
protected void onDestroy() {
super.onDestroy();
deviceScanner.stopScan();

unregisterReceiver(mBondStateReceiver);
unregisterReceiver(mPairRequestReceiver);
}

@Override
Expand Down Expand Up @@ -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));

Expand Down
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,6 +16,7 @@ buildscript {
allprojects {
repositories {
jcenter()
google()
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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