diff --git a/app/build.gradle b/app/build.gradle index 7d61c86..3117bb6 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']) + 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.14' + implementation 'com.fidesmo:ble-client-android:0.1.17' implementation '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..31caf3f 100644 --- a/app/src/main/java/com/fidesmo/ble/client/BleCard.java +++ b/app/src/main/java/com/fidesmo/ble/client/BleCard.java @@ -19,7 +19,6 @@ 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..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; @@ -43,8 +44,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 +68,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 +182,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 +195,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(); } } @@ -246,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" /> +