From 461f5dd9f44f911e90a2355b725f6da2b376c14f Mon Sep 17 00:00:00 2001 From: Wency16098574d <16098574d@connect.polyu.hk> Date: Wed, 8 Aug 2018 22:35:30 +0800 Subject: [PATCH] Add GetScannedInfo event to BLE extension --- .../src/edu/mit/appinventor/ble/BluetoothLE.java | 12 ++++++++++++ .../src/edu/mit/appinventor/ble/BluetoothLEint.java | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/appinventor/components/src/edu/mit/appinventor/ble/BluetoothLE.java b/appinventor/components/src/edu/mit/appinventor/ble/BluetoothLE.java index f725c91d9..42df4487d 100644 --- a/appinventor/components/src/edu/mit/appinventor/ble/BluetoothLE.java +++ b/appinventor/components/src/edu/mit/appinventor/ble/BluetoothLE.java @@ -43,6 +43,8 @@ import java.util.List; import java.util.Set; +import android.os.Handler; + /** * @author Andrew McKinney (mckinney@mit.edu) @@ -1214,11 +1216,21 @@ public void Disconnected() { * * @param rssi the strength of the received signal, from -100 to 0. */ + + + //New Event which returns the new scanned deviceID and rssi + @SimpleEvent + public void GetScannedInfo(final String deviceID, final int rssi) { + } + + @SimpleEvent(description = "Trigger event when RSSI (Received Signal Strength Indicator) of found" + " BluetoothLE device changes") public void RssiChanged(final int rssi) { } + + /** * The DeviceFound event is run when a new Bluetooth low energy device is found. */ diff --git a/appinventor/components/src/edu/mit/appinventor/ble/BluetoothLEint.java b/appinventor/components/src/edu/mit/appinventor/ble/BluetoothLEint.java index 45d2a3fed..3a00fe220 100644 --- a/appinventor/components/src/edu/mit/appinventor/ble/BluetoothLEint.java +++ b/appinventor/components/src/edu/mit/appinventor/ble/BluetoothLEint.java @@ -1312,6 +1312,8 @@ public void onScanResult(final int callbackType, final ScanResult scanResult) { public void run() { isScanning = true; addDevice(scanResult.getDevice(), scanResult.getRssi()); + //Call GetScannedInfo() function to trigger an event + GetScannedInfo(scanResult.getDevice().getAddress(), scanResult.getRssi()); } }); } @@ -2509,6 +2511,16 @@ public void run() { }); } + //New method for positioning, which triggers GetScannedInfo event and return the deviceID and Rssi + private void GetScannedInfo(final String deviceID, final int device_rssi) { + uiThread.postDelayed(new Runnable() { + @Override + public void run() { + EventDispatcher.dispatchEvent(outer, "GetScannedInfo", deviceID, device_rssi); + } + }, 1000); + } + private void RssiChanged(final int device_rssi) { uiThread.postDelayed(new Runnable() { @Override