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

manual services filtering #9

Open
wants to merge 5 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
10 changes: 8 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
10 changes: 6 additions & 4 deletions app/src/main/java/com/fidesmo/ble/client/BleCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -38,14 +37,17 @@ public class BleCard implements IsoCard, Closeable {
private int transceiveLength = 512;
private boolean connected = false;

private List<OnCardErrorListener> errorListeners = new CopyOnWriteArrayList();
private List<OnCardErrorListener> errorListeners = new CopyOnWriteArrayList<OnCardErrorListener>();

@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
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 19 additions & 5 deletions app/src/main/java/com/fidesmo/ble/client/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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();
}

Expand Down
56 changes: 39 additions & 17 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,48 @@
android:layout_alignParentStart="true" android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" android:indeterminate="true"
android:visibility="invisible"/>

<ScrollView
android:id="@+id/scrollView"
android:layout_width="368dp"
android:layout_height="426dp"
android:layout_above="@+id/btnScan"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:fillViewport="false">

<TextView
android:id="@+id/outputView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView"
android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" android:fillViewport="false"
android:layout_above="@+id/btnScan">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press scan or Advertise button"
android:id="@+id/outputView"
android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Press scan or Advertise button" />
</ScrollView>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan"
android:id="@+id/btnScan"
android:onClick="onDiscoveryClicked"
android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"/>
android:id="@+id/btnScan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/progressBar"
android:layout_alignParentBottom="true"
android:onClick="onDiscoveryClicked"
android:text="Scan"
android:layout_alignRight="@+id/progressBar" />

<EditText
android:id="@+id/macFilter"
android:layout_width="255dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:hint="@string/mac_filter_hint"
android:layout_alignParentLeft="true" />

</RelativeLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">APDU over BLE demo application</string>
<string name="mac_filter_hint">MAC filter</string>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0'
classpath 'com.android.tools.build:gradle:3.1.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
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 @@
#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