-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignalStrengthListener.java
263 lines (228 loc) · 12 KB
/
SignalStrengthListener.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package edu.nctu.wirelab.testsignalv1;
import android.annotation.TargetApi;
import android.os.Build;
import android.telephony.CellInfo;
import android.telephony.CellInfoLte;
import android.telephony.CellInfoWcdma;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class SignalStrengthListener extends PhoneStateListener {
private final String TagName = "SignalStrengthListener";
private long curTestTime=0, preTestTime=0;
private List<CellInfo> cellInfoList;
private String Msg;
//CellInfoType (LTE or Wcdma)
public static String CellInfoType;
//LTE info
public static int AtCellRSRP, AtCellID, AtCellMCC, AtCellMNC, AtCellPCI, AtCellTAC, AtCellRSSI, AtCellRSRQ;
public static long AtCellTimeInterval;
//Wcdma info
public static int WcdmaAtCellPsc, WcdmaAtCellLac, WcdmaAtCellID, WcdmaAtCellMCC, WcdmaAtCellMNC, WcdmaAtCellSignalStrength;
public static int PassCellNum = 0;
public static int NowCellID = 0;
public static long StayCellAt = 0, CellHoldTime = 0;
public static double AvgCellResidenceTime = 0, AvgCellHoldTime, TtlCellResidenceTime = 0;
private Date LogTime;
private SimpleDateFormat LogTimesdf = new SimpleDateFormat("HH:mm:ss:SSS");
public SignalStrengthListener(){
}
public static void CalculateAvg(){
if( PassCellNum!=0 )
AvgCellResidenceTime = TtlCellResidenceTime/PassCellNum;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onSignalStrengthsChanged(android.telephony.SignalStrength signalstrength) {
try {
curTestTime = System.currentTimeMillis();
LogTime = null;
LogTime = new Date();
Log.d(TagName, "curTestTime:"+curTestTime);
String ltestr = signalstrength.toString();
String[] parts = ltestr.split(" ");
Log.d(TagName, "mGsmSignalStrength:" + parts[1] +
" mGsmBitErrorRate:" + parts[2] +
" mCdmaDbm:" + parts[3] +
" mCdmaEcio:" + parts[4] +
" mEvdoDbm:" + parts[5] +
" mEvdoEcio:" + parts[6] +
" mEvdoSnr:" + parts[7] +
" mLteSignalStrength:" + parts[8] +
" mLteRsrp:" + parts[9] +
" mLteRsrq:" + parts[10] +
" mLteRssnr:" + parts[11] +
" mLteCqi:" + parts[12] +
" mTdScdmaRscp:" + parts[13] +
" (gsm|lte : cdma)?:" + parts[14]
);
AtCellRSRQ = Integer.valueOf(parts[10]);
if (Integer.valueOf(parts[8]) >= 0 && Integer.valueOf(parts[8]) <= 99)
AtCellRSSI = Integer.valueOf(parts[8]) * 2 - 113;
else if (Integer.valueOf(parts[8]) >= 100 && Integer.valueOf(parts[8]) <= 199)
AtCellRSSI = Integer.valueOf(parts[8]) - 216;
else
AtCellRSSI = Integer.MAX_VALUE;
//String ltersrp = parts[9];
cellInfoList = MainActivity.getTelephonyManager().getAllCellInfo();
for (CellInfo cellInfo : cellInfoList) {
if ( cellInfo.isRegistered()) {
//Log.d(TagName, "isRegistered");
if( cellInfo instanceof CellInfoLte ){
CellInfoType = "LTE";
// cast to CellInfoLte and call all the CellInfoLte methods you need
// gets RSRP cell signal strength:
AtCellRSRP = ((CellInfoLte) cellInfo).getCellSignalStrength().getDbm();
// Gets the LTE cell identity: (returns 28-bit Cell Identity, Integer.MAX_VALUE if unknown)
AtCellID = ((CellInfoLte) cellInfo).getCellIdentity().getCi();
// Gets the LTE MCC: (returns 3-digit Mobile Country Code, 0..999, Integer.MAX_VALUE if unknown)
AtCellMCC = ((CellInfoLte) cellInfo).getCellIdentity().getMcc();
// Gets theLTE MNC: (returns 2 or 3-digit Mobile Network Code, 0..999, Integer.MAX_VALUE if unknown)
AtCellMNC = ((CellInfoLte) cellInfo).getCellIdentity().getMnc();
// Gets the LTE PCI: (returns Physical Cell Id 0..503, Integer.MAX_VALUE if unknown)
AtCellPCI = ((CellInfoLte) cellInfo).getCellIdentity().getPci();
// Gets the LTE TAC: (returns 16-bit Tracking Area Code, Integer.MAX_VALUE if unknown)
AtCellTAC = ((CellInfoLte) cellInfo).getCellIdentity().getTac();
Log.d(TagName, "RSRP:" + AtCellRSRP +
" cellID:" + AtCellID +
" cellMcc:" + AtCellMCC +
" cellMnc:" + AtCellMNC +
" cellPci:" + AtCellPCI +
" cellTac:" + AtCellTAC);
}
else if (cellInfo instanceof CellInfoWcdma) {
CellInfoType = "Wcdma";
WcdmaAtCellSignalStrength = ((CellInfoWcdma) cellInfo).getCellSignalStrength().getDbm();
WcdmaAtCellID = ((CellInfoWcdma) cellInfo).getCellIdentity().getCid();
WcdmaAtCellMCC = ((CellInfoWcdma) cellInfo).getCellIdentity().getMcc();
WcdmaAtCellMNC = ((CellInfoWcdma) cellInfo).getCellIdentity().getMnc();
WcdmaAtCellPsc = ((CellInfoWcdma) cellInfo).getCellIdentity().getPsc();
WcdmaAtCellLac = ((CellInfoWcdma) cellInfo).getCellIdentity().getLac();
Log.d(TagName, "WcdmaAtCellSignalStrength:" + WcdmaAtCellSignalStrength +
" WcdmaAtCellID:" + WcdmaAtCellID +
" WcdmaAtCellMCC:" + WcdmaAtCellMCC +
" WcdmaAtCellMNC:" + WcdmaAtCellMNC +
" WcdmaAtCellPsc:" + WcdmaAtCellPsc +
" WcdmaAtCellLac:" + WcdmaAtCellLac);
}
AtCellTimeInterval = curTestTime - preTestTime;
RunIntentService2.recordwriter.write(JsonParser.AtCellInfoToJson() + "\n,\n");
if( CellInfoType.equals("LTE") && AtCellID!=NowCellID ){
HandoverOccur();
}
else if( CellInfoType.equals("Wcdma") && WcdmaAtCellID!=NowCellID ){
HandoverOccur();
}
//ask ShowMsg to change the ViewText
Msg = "AppicationType: Android" +
"\nApplicationVersion: " + Build.VERSION.RELEASE +
"\nAccount: " + JsonParser.Account +
"\nPassword: " + JsonParser.Password +
"\nequipmentId: " + MainActivity.tm.getDeviceId() +
"\nEvent: Measurement" +
"\nBatteryLevel:" + BatteryInfoReceiver.level;
if( LocationUpdater.userlocation!=null ){
Msg = Msg.concat("Lat: " + String.valueOf(LocationUpdater.userlocation.getLatitude()) +
"\nLng: " + String.valueOf(LocationUpdater.userlocation.getLongitude()) );
}
else{
Msg = Msg.concat("Lat: Unknown" +
"\nLng: Unknown");
}
if( CellInfoType.equals("LTE") ){
Msg = Msg.concat("\nTime: " + curTestTime +
"\nCellInfoType: " + String.valueOf(SignalStrengthListener.CellInfoType) +
"\nCellID: " + String.valueOf(SignalStrengthListener.AtCellID) +
"\nCellMCC: " + String.valueOf(SignalStrengthListener.AtCellMCC) +
"\nCellMNC: " + String.valueOf(SignalStrengthListener.AtCellMNC) +
"\nCellPCI: " + String.valueOf(SignalStrengthListener.AtCellPCI) +
"\nCellTAC: " + String.valueOf(SignalStrengthListener.AtCellTAC) +
"\nRSSI(dbm): " + String.valueOf(SignalStrengthListener.AtCellRSSI) +
"\nSINR: null" +
"\nRSRQ: " + String.valueOf(SignalStrengthListener.AtCellRSRQ) +
"\nRSRP(dbm): " + String.valueOf(SignalStrengthListener.AtCellRSRP) +
"\nTimeInterval: " + String.valueOf(SignalStrengthListener.AtCellTimeInterval));
}
else if( CellInfoType.equals("Wcdma") ){
Msg = Msg.concat("\nTime: " + curTestTime +
"\nCellInfoType: " + String.valueOf(SignalStrengthListener.CellInfoType) +
"\nCellID: " + String.valueOf(SignalStrengthListener.WcdmaAtCellID) +
"\nCellMCC: " + String.valueOf(SignalStrengthListener.WcdmaAtCellMCC) +
"\nCellMNC: " + String.valueOf(SignalStrengthListener.WcdmaAtCellMNC) +
"\nCellPsc: " + String.valueOf(SignalStrengthListener.WcdmaAtCellPsc) +
"\nCellLAC: " + String.valueOf(SignalStrengthListener.WcdmaAtCellLac) +
"\nSignalStrength(dbm): " + String.valueOf(SignalStrengthListener.WcdmaAtCellSignalStrength) +
"\nTimeInterval: " + String.valueOf(SignalStrengthListener.AtCellTimeInterval));
}
ShowMsg.NoticeChange(ShowMsg.AtCellInfo, Msg);
preTestTime = curTestTime;
break;
}
}
}
catch (Exception e){
Log.d(TagName, "Exception: " + e.toString());
}
}
private void HandoverOccur(){
Log.d(TagName, "Handover");
if( CellInfoType.equals("LTE") && AtCellID!=NowCellID ){
NowCellID = AtCellID;
}
else if( CellInfoType.equals("Wcdma") && WcdmaAtCellID!=NowCellID ){
NowCellID = WcdmaAtCellID;
}
PassCellNum = PassCellNum+1;
if( StayCellAt==0 ){
StayCellAt = curTestTime;
}
else{
CellHoldTime = curTestTime - StayCellAt;
TtlCellResidenceTime = TtlCellResidenceTime + CellHoldTime;
StayCellAt = curTestTime;
}
if( MainSetting.PhoneStateSwitch )
PSListener.HandoverOccur();
try {
RunIntentService2.recordwriter.write(JsonParser.HandoverInfoToJson() + "\n,\n");
} catch (IOException e) {
e.printStackTrace();
}
}
private void initBfRun() {
preTestTime = System.currentTimeMillis();
AtCellRSRP = 0;
AtCellID = 0;
AtCellMCC = 0;
AtCellMNC = 0;
AtCellPCI = 0;
AtCellTAC = 0;
AtCellRSSI = 0;
AtCellTimeInterval = 0;
CellInfoType = "LTE";
WcdmaAtCellPsc = 0;
WcdmaAtCellLac = 0;
WcdmaAtCellID = 0;
WcdmaAtCellMCC = 0;
WcdmaAtCellMNC = 0;
WcdmaAtCellSignalStrength = 0;
PassCellNum = 0;
NowCellID = 0;
StayCellAt = 0;
CellHoldTime = 0;
AvgCellResidenceTime = 0;
AvgCellHoldTime = 0;
TtlCellResidenceTime = 0;
}
public void startService(TelephonyManager tm){
initBfRun();
tm.listen(this, SignalStrengthListener.LISTEN_SIGNAL_STRENGTHS);
}
public void stopService(TelephonyManager tm){
tm.listen(this, SignalStrengthListener.LISTEN_NONE);
}
}