77 lines
1.6 KiB
C++
77 lines
1.6 KiB
C++
#ifndef WIFISENSORANDROID_H
|
|
#define WIFISENSORANDROID_H
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
#include <QAndroidJniObject>
|
|
|
|
#include "Debug.h"
|
|
#include "WiFiSensor.h"
|
|
|
|
class WiFiSensorAndroid : public WiFiSensor {
|
|
|
|
private:
|
|
|
|
/** hidden ctor. use singleton! */
|
|
WiFiSensorAndroid() {;}
|
|
|
|
public:
|
|
|
|
/** singleton access */
|
|
static WiFiSensorAndroid& get() {
|
|
static WiFiSensorAndroid wifi;
|
|
return wifi;
|
|
}
|
|
|
|
void start() override {
|
|
|
|
// start scanning
|
|
int res = QAndroidJniObject::callStaticMethod<int>("java/indoor/WiFi", "start", "()I");
|
|
(void) res;
|
|
|
|
}
|
|
|
|
/** called from java. handle the given incoming scan result */
|
|
void handle(const std::string& data) {
|
|
|
|
// to-be-constructed sensor data
|
|
WiFiSensorData sensorData;
|
|
|
|
// parse each mac->rssi entry
|
|
for (int i = 0; i < (int)data.length(); i += 17+1+2) {
|
|
const std::string bssid = data.substr(i, 17);
|
|
const int8_t rssi = data[i+17];
|
|
const int8_t pad1 = data[i+18];
|
|
const int8_t pad2 = data[i+19];
|
|
if (pad1 != 0 || pad2 != 0) {Debug::error("padding error within WiFi scan result");}
|
|
sensorData.entries.push_back(WiFiSensorDataEntry(bssid, rssi));
|
|
}
|
|
|
|
// call listeners
|
|
informListeners(sensorData);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
extern "C" {
|
|
|
|
/** called after each successful WiFi scan */
|
|
JNIEXPORT void JNICALL Java_java_indoor_WiFi_onScanComplete(JNIEnv* env, jobject jobj, jbyteArray arrayID) {
|
|
(void) env; (void) jobj;
|
|
jsize length = env->GetArrayLength(arrayID);
|
|
jboolean isCopy;
|
|
jbyte* data = env->GetByteArrayElements(arrayID, &isCopy);
|
|
std::string str((char*)data, length);
|
|
env->ReleaseByteArrayElements(arrayID, data, JNI_ABORT);
|
|
WiFiSensorAndroid::get().handle(str);
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif // WIFISENSORANDROID_H
|