From 921db1bcdc288fe602cd3ca370a0f65c2240dd33 Mon Sep 17 00:00:00 2001 From: Sergey Khruschak Date: Wed, 4 Apr 2018 16:16:58 +0300 Subject: [PATCH 1/5] manual services filtering --- app/build.gradle | 18 ++++++++++++------ .../java/com/fidesmo/ble/client/BleCard.java | 12 +++++++----- .../com/fidesmo/ble/client/MainActivity.java | 12 +++++++----- gradle/wrapper/gradle-wrapper.properties | 4 ++-- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 7d61c86..6460f0e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,14 +37,20 @@ repositories { maven { url "http://dl.bintray.com/fidesmo/maven" } + ivy { + url "${System.properties['user.home']}/.ivy2/local" + } + flatDir{ + dirs '../libs' + } jcenter() } dependencies { - implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar']) - testImplementation 'junit:junit:4.12' - implementation 'com.android.support:appcompat-v7:25.3.1' - implementation 'io.reactivex:rxjava:1.0.13' - implementation 'com.fidesmo:ble-client-android:0.1.14' - implementation 'com.fidesmo:nordpol-android:0.1.18' + compile fileTree(include: ['*.jar', '*.aar'], dir: 'libs') + testCompile 'junit:junit:4.12' + compile 'com.android.support:appcompat-v7:25.3.1' + compile 'io.reactivex:rxjava:1.0.13' + compile 'com.fidesmo:ble-client-android:0.1.14' + compile 'com.fidesmo:nordpol-android:0.1.18' } diff --git a/app/src/main/java/com/fidesmo/ble/client/BleCard.java b/app/src/main/java/com/fidesmo/ble/client/BleCard.java index 47f564a..fee60b9 100644 --- a/app/src/main/java/com/fidesmo/ble/client/BleCard.java +++ b/app/src/main/java/com/fidesmo/ble/client/BleCard.java @@ -12,14 +12,13 @@ import java.io.Closeable; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.UUID; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import static com.fidesmo.ble.client.Utils.*; -import static com.fidesmo.ble.client.Utils.fromApduSequence; @TargetApi(Build.VERSION_CODES.LOLLIPOP) public class BleCard implements IsoCard, Closeable { @@ -38,14 +37,17 @@ public class BleCard implements IsoCard, Closeable { private int transceiveLength = 512; private boolean connected = false; - private List errorListeners = new CopyOnWriteArrayList(); + private List errorListeners = new CopyOnWriteArrayList(); @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) - public BleCard(Context context, BluetoothDevice device) { + public BleCard(Context context, BluetoothDevice device, LogsConsumer logsConsumer) { gattClient = new BleGattServiceClient(context, device, SimplePacketFragmenter.factory(), transceiveLength); + + gattClient.setLogsConsumer(logsConsumer); + } @Override @@ -79,7 +81,7 @@ public void connect() throws IOException { public void connectionEstablished() { connectionLatch.countDown(); } - }); + }, Collections.singletonList(APDU_SERVICE_UUID)); connectionLatch.await(timeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { 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 4d9f481..791d6d9 100644 --- a/app/src/main/java/com/fidesmo/ble/client/MainActivity.java +++ b/app/src/main/java/com/fidesmo/ble/client/MainActivity.java @@ -43,8 +43,7 @@ public class MainActivity extends AppCompatActivity implements OnDiscoveredTagLi final private int REQUEST_CODE_SCAN = 123; final private int REQUEST_CODE_ADVERT = 124; - private BleDeviceScanner deviceScanner = - BleDeviceScanner.singleServiceScanner(this, BleCard.APDU_SERVICE_UUID, this, this); + private BleDeviceScanner deviceScanner = new BleDeviceScanner(this); private TagDispatcher nfcTagDispatcher; @@ -68,6 +67,9 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + deviceScanner.addBleDeviceListener(this); + deviceScanner.setLogsConsumer(this); + // Use this check to determine whether BLE is supported on the device. Then you can selectively disable BLE-related features. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, "BLE is not supported on this device", Toast.LENGTH_SHORT).show(); @@ -179,9 +181,9 @@ private void askForBtDevicePermissionsAndFireAction(int requestCode) { @Override public void deviceDiscovered(BluetoothDevice bluetoothDevice) { - log("BLE device discovered. Obtaining card information"); + // log("BLE device discovered. Obtaining card information"); - CardInfoClient client = new CardInfoClient(new BleCard(this, bluetoothDevice)); + CardInfoClient client = new CardInfoClient(new BleCard(this, bluetoothDevice, this)); try { CardInfo cardInfo = client.getCardInfo(); @@ -192,7 +194,7 @@ public void deviceDiscovered(BluetoothDevice bluetoothDevice) { ); } catch (Exception e) { - log("Failed to get remote card information"); + log("Failed to get remote card information: " + e.getMessage()); e.printStackTrace(); } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 5e08ab1..096af2e 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Apr 04 16:19:01 EEST 2018 +#Thu Mar 29 11:19:18 EEST 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip From 0d6d7388ade60f580d43281020f86a34b7b190c6 Mon Sep 17 00:00:00 2001 From: Sergey Khruschak Date: Fri, 13 Apr 2018 16:41:43 +0300 Subject: [PATCH 2/5] device mac filtering --- app/build.gradle | 12 ++-- .../com/fidesmo/ble/client/MainActivity.java | 12 ++++ app/src/main/res/layout/activity_main.xml | 56 +++++++++++++------ app/src/main/res/values/strings.xml | 1 + 4 files changed, 58 insertions(+), 23 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 6460f0e..948772e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -47,10 +47,10 @@ repositories { } dependencies { - compile fileTree(include: ['*.jar', '*.aar'], dir: 'libs') - testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:25.3.1' - compile 'io.reactivex:rxjava:1.0.13' - compile 'com.fidesmo:ble-client-android:0.1.14' - compile 'com.fidesmo:nordpol-android:0.1.18' + implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs') + testImplementation 'junit:junit:4.12' + implementation 'com.android.support:appcompat-v7:25.3.1' + implementation 'io.reactivex:rxjava:1.0.13' + implementation 'com.fidesmo:ble-client-android:0.1.15' + implementation 'com.fidesmo:nordpol-android:0.1.18' } 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 791d6d9..53aac1e 100644 --- a/app/src/main/java/com/fidesmo/ble/client/MainActivity.java +++ b/app/src/main/java/com/fidesmo/ble/client/MainActivity.java @@ -32,6 +32,7 @@ import java.io.IOException; import java.util.LinkedList; +import java.util.regex.Pattern; import java.util.List; import static com.fidesmo.ble.client.BleUtils.byteArrayToString; @@ -248,6 +249,17 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { } private void startScan() { + final Pattern macPattern = Pattern.compile("[A-F0-9]{2}(:[A-F0-9]{2}){5}"); + TextView macFilter = (TextView) findViewById(R.id.macFilter); + String filterString = macFilter.getText().toString(); + if (!filterString.isEmpty()) { + if(macPattern.matcher(filterString).matches()) { + log("Applying Mac filter '" + filterString + "'"); + deviceScanner.setDeviceAddressFilter(filterString); + } else { + log("Mac filter '" + filterString + "' is invalid, ignoring it"); + } + } deviceScanner.startDiscovery(); } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f0f8e5e..29844ea 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -20,26 +20,48 @@ android:layout_alignParentStart="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:indeterminate="true" android:visibility="invisible"/> + + + - + android:layout_alignParentLeft="true" + android:layout_alignParentStart="true" + android:layout_alignParentTop="true" + android:text="Press scan or Advertise button" /> +