-
Notifications
You must be signed in to change notification settings - Fork 11
/
SubPos.java
156 lines (133 loc) · 5.53 KB
/
SubPos.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package iocomms.subpos;
/* Android API for SubPos (http://www.subpos.org)
* Copyright (C) 2015 Blair Wyatt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import java.util.List;
public class SubPos {
private WifiManager mainWifiObj;
private WifiScanReceiver wifiReceiver;
private NodeLocations SPSNodes = new NodeLocations();
private Context mContext;
private boolean three_d_map;
private boolean offset_mapping;
private long application_id;
private int rolling_average_length;
private int age; //Default age of 30 seconds; remove anything over 30 seconds old
private class WifiScanReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
if (wifiScanList.size() != 0) {
SPSNodes.killAgedNodes(age);
for (int i = 0; i < wifiScanList.size(); i++) {
if (wifiScanList.get(i).SSID.startsWith("SPS")) {
SPSNodes.addLocation(wifiScanList.get(i).SSID.toCharArray(),
wifiScanList.get(i).level,
wifiScanList.get(i).BSSID,
wifiScanList.get(i).frequency,
application_id, offset_mapping, three_d_map);
}
}
if (SPSNodes.size() > 0) {
SPSNodes.calculatePosition();
}
}
}
}
//Create a SubPos positioning object that will obtain position from nodes that belong to the
//appropriate application ID and positioning type (3d,offset). If you don't have a specific
//application ID for the positioning project, just use 0.
public SubPos(Context mContext, int nodeAge, int rolling_average_length, boolean offset_mapping,
boolean three_d_mapping, long application_id) {
this.three_d_map = three_d_mapping;
this.offset_mapping = offset_mapping;
this.application_id = application_id;
this.rolling_average_length = rolling_average_length;
this.age = nodeAge;
SPSNodes.setRollingAverage(this.rolling_average_length);
this.mContext = mContext;
mainWifiObj = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
wifiReceiver = new WifiScanReceiver();
mainWifiObj.startScan();
}
public SubPos(Context mContext, boolean offset_mapping,
boolean three_d_mapping, long application_id) {
this.three_d_map = three_d_mapping;
this.offset_mapping = offset_mapping;
this.application_id = application_id;
this.rolling_average_length = 10;
this.age = 30;
SPSNodes.setRollingAverage(this.rolling_average_length);
this.mContext = mContext;
mainWifiObj = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
wifiReceiver = new WifiScanReceiver();
mainWifiObj.startScan();
}
public SubPos(Context mContext) {
this.three_d_map = false;
this.offset_mapping = false;
this.application_id = 0;
this.rolling_average_length = 10;
this.age = 30;
SPSNodes.setRollingAverage(this.rolling_average_length);
this.mContext = mContext;
mainWifiObj = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
wifiReceiver = new WifiScanReceiver();
mainWifiObj.startScan();
}
//Call this on the object when the application is paused.
public void pause() {
mContext.unregisterReceiver(wifiReceiver);
}
//Call this on the object when the application resumes.
public void resume() {
mContext.registerReceiver(wifiReceiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
//Get the current position as calculated from the visible nodes.
//Return null if nothing is calculated.
public SubPosPosition getPosition()
{
if (SPSNodes.isCalculated()) {
return new SubPosPosition(SPSNodes.currentPosition.lat,
SPSNodes.currentPosition.lng, SPSNodes.currentPosition.altitude);
} else {
return null;
}
}
//Get the current position error as calculated from the visible nodes.
//Return 0 if nothing is calculated.
public Double getPositionError()
{
if (SPSNodes.isCalculated()) {
if (SPSNodes.currentPosition.errorCalced) {
return SPSNodes.currentPosition.errorRadius;
}
}
return 0;
}
//Get the current visible Nodes
public ArrayList<SPSData> getNodes()
{
return SPSNodes.getLocations();
}
}