Skip to content

Commit

Permalink
add start_scan settings and filters arguments #25
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b committed Dec 11, 2021
1 parent b45539d commit 416ce60
Show file tree
Hide file tree
Showing 13 changed files with 445 additions and 5 deletions.
15 changes: 13 additions & 2 deletions able/android/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@
from able.dispatcher import BluetoothDispatcherBase


ArrayList = autoclass('java.util.ArrayList')

Activity = autoclass('android.app.Activity')
BLE = autoclass('org.able.BLE')

BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
BluetoothDevice = autoclass('android.bluetooth.BluetoothDevice')
BluetoothGattDescriptor = autoclass('android.bluetooth.BluetoothGattDescriptor')

ScanFilter = autoclass('android.bluetooth.le.ScanFilter')
ScanFilterBuilder = autoclass('android.bluetooth.le.ScanFilter$Builder')
ScanSettings = autoclass('android.bluetooth.le.ScanSettings')
ScanSettingsBuilder = autoclass('android.bluetooth.le.ScanSettings$Builder')

ENABLE_NOTIFICATION_VALUE = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
ENABLE_INDICATION_VALUE = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
DISABLE_NOTIFICATION_VALUE = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE
Expand Down Expand Up @@ -89,8 +96,12 @@ def _request_runtime_permissions(self):

@require_bluetooth_enabled
@require_runtime_permissions
def start_scan(self):
self._ble.startScan(self.enable_ble_code)
def start_scan(self, filters=None, settings=None):
if not filters:
filters = ArrayList()
if not settings:
settings = ScanSettingsBuilder().build()
self._ble.startScan(self.enable_ble_code, filters, settings)

def stop_scan(self):
self._ble.stopScan()
Expand Down
2 changes: 1 addition & 1 deletion able/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def set_queue_timeout(self, timeout):
self.queue_timeout = timeout
self.queue.set_timeout(timeout)

def start_scan(self):
def start_scan(self, filters=None, settings=None):
"""Start a scan for devices.
Ask for runtime permission to access location.
Start a system activity that allows the user to turn on Bluetooth,
Expand Down
9 changes: 7 additions & 2 deletions able/src/org/able/BLE.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;

import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanSettings;

import android.os.Handler;
import android.util.Log;
import java.util.List;
Expand Down Expand Up @@ -87,7 +90,9 @@ public BluetoothGatt getGatt() {
return mBluetoothGatt;
}

public void startScan(int EnableBtCode) {
public void startScan(int EnableBtCode,
List<ScanFilter> filters,
ScanSettings settings) {
Log.d(TAG, "startScan");
BluetoothAdapter adapter = getAdapter(EnableBtCode);
if (adapter != null) {
Expand All @@ -97,7 +102,7 @@ public void startScan(int EnableBtCode) {
}
if (mBluetoothLeScanner != null) {
mScanning = false;
mBluetoothLeScanner.startScan(mScanCallback);
mBluetoothLeScanner.startScan(filters, settings, mScanCallback);
} else {
showError("Could not get BLE Scanner object.");
mPython.on_scan_started(false);
Expand Down
3 changes: 3 additions & 0 deletions tests/notebooks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
there.env
*.asciidoc
*.ipynb
43 changes: 43 additions & 0 deletions tests/notebooks/init.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
jupyter:
jupytext:
formats: ipynb,md
text_representation:
extension: .md
format_name: markdown
format_version: '1.3'
jupytext_version: 1.11.2
kernelspec:
display_name: Python 3
language: python
name: python3
---

```python
from time import sleep

from dataclasses import dataclass, field
from typing import List

%load_ext pythonhere
%connect-there
```

```python
%%there
from able.android.dispatcher import (
ArrayList,
BluetoothDispatcher,
ScanFilterBuilder,
ScanSettingsBuilder
)
```

```python
%%there
@dataclass
class Results:
started: bool = None
completed: bool = None
devices: List = field(default_factory=lambda: [])
```
22 changes: 22 additions & 0 deletions tests/notebooks/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
set -ex

name="$1"
command="${2}"
command="${command:=test}"


cat "${name}.md" |
jupytext --execute --to ipynb |
jupyter nbconvert --stdin --no-input --to asciidoc --output "${name}"

cat "${name}".asciidoc

if [ "${command}" = "test" ]; then
diff "${name}.asciidoc" "${name}.expected"
elif [ "${command}" = "record" ]; then
cp "${name}".asciidoc "${name}".expected
else
echo "Unknown command: ${command}"
exit 1
fi
4 changes: 4 additions & 0 deletions tests/notebooks/run_all_tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -e

for name in test_*.md; do ./run "${name%%.*}"; done
26 changes: 26 additions & 0 deletions tests/notebooks/test_basic.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[[setup]]
= Setup

[[run-ble-devices-scan]]
= Run BLE devices scan


----
Started: None Completed: None
----

[[check-that-scan-started-and-completed]]
= Check that scan started and completed


----
Started: 1 Completed: 1
----

[[check-that-testing-device-was-discovered]]
= Check that testing device was discovered


----
True
----
74 changes: 74 additions & 0 deletions tests/notebooks/test_basic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
jupyter:
jupytext:
formats: ipynb,md
text_representation:
extension: .md
format_name: markdown
format_version: '1.3'
jupytext_version: 1.11.2
kernelspec:
display_name: Python 3
language: python
name: python3
---

# Setup

```python
%run init.ipynb
```

```python
%%there
class BLE(BluetoothDispatcher):

def on_scan_started(self, success):
results.started = success

def on_scan_completed(self):
results.completed = 1

def on_device(self, device, rssi, advertisement):
results.devices.append(device)

ble = BLE()
```

# Run BLE devices scan

```python
%%there
results = Results()
print(f"Started: {results.started} Completed: {results.completed}")
ble.start_scan()
```

```python
sleep(10)
```

```python
%%there
ble.stop_scan()
```

```python
sleep(2)
```

# Check that scan started and completed

```python
%%there
print(f"Started: {results.started} Completed: {results.completed}")
```

# Check that testing device was discovered

```python
%%there
print(
"KivyBLETest" in [dev.getName() for dev in results.devices]
)
```
18 changes: 18 additions & 0 deletions tests/notebooks/test_scan_filters.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[[setup]]
= Setup

[[test-device-is-found-with-filter-by-name]]
= Test device is found with filter by name


----
{'KivyBLETest'}
----

[[test-device-is-not-found-filtered-out-by-name]]
= Test device is not found: filtered out by name


----
[]
----
102 changes: 102 additions & 0 deletions tests/notebooks/test_scan_filters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
jupyter:
jupytext:
formats: ipynb,md
text_representation:
extension: .md
format_name: markdown
format_version: '1.3'
jupytext_version: 1.11.2
kernelspec:
display_name: Python 3
language: python
name: python3
---

# Setup

```python
%run init.ipynb
```

```python
%%there
class BLE(BluetoothDispatcher):

def on_scan_started(self, success):
results.started = success

def on_scan_completed(self):
results.completed = 1

def on_device(self, device, rssi, advertisement):
results.devices.append(device)

ble = BLE()
```

# Test device is found with filter by name

```python
%%there
results = Results()

filters = ArrayList()
filters.add(
ScanFilterBuilder().setDeviceName("KivyBLETest").build()
)

ble.start_scan(filters=filters)
```

```python
sleep(10)
```

```python
%%there
ble.stop_scan()
```

```python
sleep(2)
```

```python
%%there
print(set([dev.getName() for dev in results.devices]))
```

# Test device is not found: filtered out by name

```python
%%there
results = Results()

filters = ArrayList()
filters.add(
ScanFilterBuilder().setDeviceName("No-such-device-8458e2e35158").build()
)

ble.start_scan(filters=filters)
```

```python
sleep(10)
```

```python
%%there
ble.stop_scan()
```

```python
sleep(2)
```

```python
%%there
ble.stop_scan()

print(results.devices)
```
Loading

0 comments on commit 416ce60

Please sign in to comment.