started to add ble functions
added ble as sensor to java and c++ added sensorlistener for ble added ble to observation and onDataSensor in filter started to work on ble fingerprints for optimization
This commit is contained in:
111
_android/src/BLE.java
Normal file
111
_android/src/BLE.java
Normal file
@@ -0,0 +1,111 @@
|
||||
package indoor.java;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothManager;
|
||||
import android.bluetooth.le.BluetoothLeScanner;
|
||||
import android.bluetooth.le.ScanCallback;
|
||||
import android.bluetooth.le.ScanFilter;
|
||||
import android.bluetooth.le.ScanSettings;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
public class BLE {
|
||||
|
||||
private static Activity act;
|
||||
private static BluetoothAdapter bt = null;
|
||||
private static BluetoothLeScanner scanner = null;
|
||||
private static ScanCallback mLeScanCallback;
|
||||
private static ScanSettings mLeSettings;
|
||||
private static final List<ScanFilter> mLeFilter = new ArrayList<>();
|
||||
|
||||
/** called when a scan is completed successfully */
|
||||
public static native void onScanComplete(final byte[] result);
|
||||
|
||||
/**
|
||||
* start bluetooth scanning in the background.
|
||||
* will call onScanComplete() once a scan is available
|
||||
*/
|
||||
public static int start() {
|
||||
|
||||
Log.d("ble", "start()");
|
||||
|
||||
MyActivity act = MyActivity.act;
|
||||
|
||||
// sanity check
|
||||
if (!act.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
|
||||
Toast.makeText(act, "Bluetooth-LE not supported!", Toast.LENGTH_SHORT).show();
|
||||
return 1338;
|
||||
}
|
||||
|
||||
Log.d("ble", "Sanity Checks");
|
||||
|
||||
// Initializes a Bluetooth adapter. For API level 18 and above, get a reference to
|
||||
// BluetoothAdapter through BluetoothManager.
|
||||
final BluetoothManager mgr = (BluetoothManager) act.getSystemService(Context.BLUETOOTH_SERVICE);
|
||||
bt = mgr.getAdapter();
|
||||
|
||||
Log.d("ble", "BLE Mangaer Start");
|
||||
|
||||
// create the scanner
|
||||
scanner = bt.getBluetoothLeScanner();
|
||||
|
||||
// set the ble settings
|
||||
mLeSettings = new ScanSettings.Builder()
|
||||
.setReportDelay(0)
|
||||
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
|
||||
.setNumOfMatches(ScanSettings.MATCH_NUM_MAX_ADVERTISEMENT)
|
||||
.build();
|
||||
|
||||
//mLeFilter.add(new ScanFilter.Builder().setServiceUuid(new ParcelUuid(UUID.fromString("fda50693-a4e2-4fb1-afcf-c6eb07647825"))).build());
|
||||
|
||||
// and attach the callback
|
||||
mLeScanCallback = new ScanCallback() {
|
||||
@Override public void onScanResult(int callbackType, android.bluetooth.le.ScanResult result) {
|
||||
//Log.d("ble", result.getDevice() + " " + result.getRssi());
|
||||
|
||||
final byte[] rst = serialize(result);
|
||||
BLE.onScanComplete(rst);
|
||||
}
|
||||
|
||||
// @Override public void onBatchScanResults(List<android.bluetooth.le.ScanResult> results){
|
||||
|
||||
// for(android.bluetooth.le.ScanResult result : results){
|
||||
// Log.d("ble2", result.getDevice() + " " + result.getRssi());
|
||||
|
||||
// final byte[] rst = serialize(result);
|
||||
// BLE.onScanComplete(rst);
|
||||
// }
|
||||
// }
|
||||
};
|
||||
|
||||
if(bt == null){
|
||||
Toast.makeText(act, "Bluetooth-LE not supported!", Toast.LENGTH_SHORT).show();
|
||||
return 1338;
|
||||
}
|
||||
scanner.startScan(mLeFilter, mLeSettings, mLeScanCallback);
|
||||
|
||||
return 1337;
|
||||
}
|
||||
|
||||
/** convert the given scan-result to a binary byte[] representation */
|
||||
private static byte[] serialize(final android.bluetooth.le.ScanResult res) {
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try {
|
||||
baos.write(res.getDevice().getAddress().getBytes());
|
||||
baos.write((byte)res.getRssi());
|
||||
baos.write((byte)0);
|
||||
baos.write((byte)0);
|
||||
} catch (final Exception e) {;}
|
||||
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,8 +19,8 @@ public class WiFi {
|
||||
private static WifiManager manager;
|
||||
private static BroadcastReceiver receiver;
|
||||
|
||||
private static Thread tHeartbeat = null;
|
||||
private static long lastScanStartTS = 0;
|
||||
private static Thread tHeartbeat = null;
|
||||
private static long lastScanStartTS = 0;
|
||||
|
||||
/** called when a scan is completed successfully */
|
||||
public static native void onScanComplete(final byte[] result);
|
||||
@@ -36,12 +36,12 @@ public class WiFi {
|
||||
MyActivity act = MyActivity.act;
|
||||
manager = (WifiManager) act.getSystemService(Context.WIFI_SERVICE);
|
||||
|
||||
// reset();
|
||||
// reset();
|
||||
|
||||
WiFi.receiver = new BroadcastReceiver() {
|
||||
public final void onReceive(final Context context, final Intent intent) {
|
||||
final byte[] result = serialize(manager.getScanResults());
|
||||
triggerOneScan();
|
||||
triggerOneScan();
|
||||
WiFi.onScanComplete(result);
|
||||
}
|
||||
};
|
||||
@@ -50,42 +50,42 @@ public class WiFi {
|
||||
act.registerReceiver(WiFi.receiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
|
||||
|
||||
// start the first scan
|
||||
triggerOneScan();
|
||||
triggerOneScan();
|
||||
|
||||
//this is a very nice hack. do not try this at home.
|
||||
Method m = null;
|
||||
try {
|
||||
m = manager.getClass().getDeclaredMethod("setFrequencyBand", int.class, boolean.class);
|
||||
m.setAccessible(true);
|
||||
m.invoke(manager, 2, true);
|
||||
m.invoke(manager, 2, true);
|
||||
m.invoke(manager, 2, true);
|
||||
Log.d("wifi", "HACK IS RUNNING, BIAAAATCH");
|
||||
} catch (Exception e) {
|
||||
Log.d("wifi", "HACK HAS FAILED >.<");
|
||||
e.printStackTrace();
|
||||
}
|
||||
//this is a very nice hack. do not try this at home.
|
||||
Method m = null;
|
||||
try {
|
||||
m = manager.getClass().getDeclaredMethod("setFrequencyBand", int.class, boolean.class);
|
||||
m.setAccessible(true);
|
||||
m.invoke(manager, 2, true);
|
||||
m.invoke(manager, 2, true);
|
||||
m.invoke(manager, 2, true);
|
||||
Log.d("wifi", "HACK IS RUNNING, BIAAAATCH");
|
||||
} catch (Exception e) {
|
||||
Log.d("wifi", "HACK HAS FAILED >.<");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// if the scan result takes longer than X milliseconds,
|
||||
// trigger another-scan to ensure nothing is stuck
|
||||
final Runnable r = new Runnable() {
|
||||
public void run() {
|
||||
while(true) {
|
||||
final long ts = System.currentTimeMillis();
|
||||
final long diff = ts - lastScanStartTS;
|
||||
if (diff > 1000) { triggerOneScan(); }
|
||||
try {Thread.sleep(200);} catch (final Exception e) {;}
|
||||
}
|
||||
}
|
||||
};
|
||||
// if the scan result takes longer than X milliseconds,
|
||||
// trigger another-scan to ensure nothing is stuck
|
||||
final Runnable r = new Runnable() {
|
||||
public void run() {
|
||||
while(true) {
|
||||
final long ts = System.currentTimeMillis();
|
||||
final long diff = ts - lastScanStartTS;
|
||||
if (diff > 1000) { triggerOneScan(); }
|
||||
try {Thread.sleep(200);} catch (final Exception e) {;}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// start the heartbeat once
|
||||
if (tHeartbeat == null) {
|
||||
tHeartbeat = new Thread(r);
|
||||
tHeartbeat.start();
|
||||
}
|
||||
// start the heartbeat once
|
||||
if (tHeartbeat == null) {
|
||||
tHeartbeat = new Thread(r);
|
||||
tHeartbeat.start();
|
||||
}
|
||||
|
||||
return 1337;
|
||||
return 1337;
|
||||
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ public class WiFi {
|
||||
|
||||
try {
|
||||
if(!manager.startScan()) {throw new RuntimeException("Cant start WiFi!");}
|
||||
lastScanStartTS = System.currentTimeMillis();
|
||||
lastScanStartTS = System.currentTimeMillis();
|
||||
}catch (final Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user