This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
YASMIN/sensors/android/BLESensorAndroid.h
mail@toni-fetzer.de 8a1f8430fb added the files
2019-06-05 18:05:01 +02:00

83 lines
1.8 KiB
C++

#ifndef BLESENSORANDROID_H
#define BLESENSORANDROID_H
#ifdef ANDROID
#include <QAndroidJniObject>
#include "../../misc/Debug.h"
#include "../BLESensor.h"
#include <../misc/fixc11.h>
#include <Indoor/misc/Debug.h>
class BLESensorAndroid : public BLESensor {
private:
/** hidden ctor. use singleton! */
BLESensorAndroid() {;}
bool started = false;
public:
/** singleton access */
static BLESensorAndroid& get() {
static BLESensorAndroid beacon;
return beacon;
}
void start() override {
// do NOT start twice!
if (started) {return;}
started = true;
// start scanning
int res = QAndroidJniObject::callStaticMethod<int>("indoor/java/BLE", "start", "()I");
if (res != 1337) {throw Exception("error while starting BLE");}
}
void stop() override {
throw "todo";
}
bool isRunning() const override {
return started;
}
/**
* called from WiFi.java
* handle the given incoming scan result
* just a bit curious, thus std::string instead of std::string&
*/
void handle(const std::string data) {
// tag all scan-entries with the current app runtime
const Timestamp curTS = Timestamp::fromRunningTime();
// parse each mac->rssi entry
const std::string bssid = data.substr(0,17);
const int8_t rssi = data[17];
const int8_t pad1 = data[18];
const int8_t pad2 = data[19];
if (pad1 != 0 || pad2 != 0) {Debug::error("padding error within BLE scan result");}
Log::add("BLE", bssid + "; " + std::to_string(static_cast<int>(rssi)));
// call listeners
informListeners(BeaconMeasurement(curTS, Beacon(bssid), rssi));
}
};
#endif
#endif // BLESENSORANDROID_H