完善设备网络上传情况
This commit is contained in:
@@ -87,7 +87,7 @@ public class InfoActivity extends BaseActivity<ActivityInfoBinding, InfoActivity
|
||||
private void initData() {
|
||||
//初始化共育状态
|
||||
fillDataInText(binding.tvHead, RoomSetting.getRoomName());
|
||||
fillDataInText(binding.tvSim, HardwareSetting.getImei());
|
||||
fillDataInText(binding.tvSim, HardwareSetting.getIccid());
|
||||
if (SilkwormDBManager.isStarting()) {
|
||||
//正在共育中
|
||||
SilkwormNew silkworm = SilkwormDBManager.getLatestSilkworm();
|
||||
|
||||
+1
-12
@@ -2,9 +2,6 @@ package com.example.iot_controlhost.ui.activity;
|
||||
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
@@ -62,15 +59,7 @@ public class MainActivityPresenter extends BasePresenter {
|
||||
|
||||
public MainActivityPresenter(@NonNull Application application) {
|
||||
super(application);
|
||||
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1 || HardwareSetting.isYSAndroid()) {
|
||||
// 旧版Android7.1 || 新版Android11
|
||||
TelephonyManager telephonyManager = (TelephonyManager) getApplication().getSystemService(Context.TELEPHONY_SERVICE);
|
||||
MMKVUtil.put(HardwareSetting.IMEI, telephonyManager.getSimSerialNumber());
|
||||
MMKVUtil.put(HardwareSetting.IMSI, telephonyManager.getSubscriberId());
|
||||
} else {
|
||||
// 旧版Android11
|
||||
}
|
||||
MyLog.test("SIM:" + HardwareSetting.getImei());
|
||||
HardwareSetting.refreshMobileIdentifiers(getApplication());
|
||||
//更新数据
|
||||
RxJavaUtils.doInIOThread(new RxIOTask<>(null) {
|
||||
@Override
|
||||
|
||||
@@ -4,6 +4,9 @@ import android.app.Application;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.Network;
|
||||
import android.net.NetworkCapabilities;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.telephony.PhoneStateListener;
|
||||
import android.telephony.SignalStrength;
|
||||
@@ -64,6 +67,8 @@ public class AppBarViewModel extends BasePresenter {
|
||||
private Boolean pingBaiduAccess = true;
|
||||
private NetworkStateListener networkStateListener;
|
||||
private TelephonyManager mTelephonyManager;
|
||||
private ConnectivityManager connectivityManager;
|
||||
private ConnectivityManager.NetworkCallback networkCallback;
|
||||
|
||||
public MutableLiveData<Integer> getIvServer() {
|
||||
return ivServer;
|
||||
@@ -110,6 +115,7 @@ public class AppBarViewModel extends BasePresenter {
|
||||
networkStateListener = new NetworkStateListener();
|
||||
mTelephonyManager = (TelephonyManager) getApplication().getSystemService(Context.TELEPHONY_SERVICE);
|
||||
mTelephonyManager.listen(networkStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
|
||||
registerNetworkCallback();
|
||||
isLight.setValue(!RoomSetting.isDarkTheme());
|
||||
RxBusUtils.get().onMainThread(RxTag.MAIN_WARNING, DeviceErrorInfo.class, info -> {
|
||||
//弹窗
|
||||
@@ -292,14 +298,55 @@ public class AppBarViewModel extends BasePresenter {
|
||||
}
|
||||
ivServer.setValue(ivNetServerId);
|
||||
ivNetwork.setValue(pingBaiduAccess ? resId : R.mipmap.network_0);
|
||||
if (lastNetState != netType) {
|
||||
//网络状态发生变化
|
||||
UploadHostInfo();
|
||||
lastNetState = netType;
|
||||
}
|
||||
uploadHostInfoOnNetworkTypeChanged(netType);
|
||||
}
|
||||
|
||||
int lastNetState = -1;
|
||||
private int lastNetState = -1;
|
||||
|
||||
/**
|
||||
* 监听系统默认网络变化,确保WIFI、网线和移动网络之间切换时主动上传主机信息。
|
||||
*/
|
||||
private void registerNetworkCallback() {
|
||||
connectivityManager = (ConnectivityManager) getApplication()
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
if (connectivityManager == null) {
|
||||
MyLog.networkError("网络类型监听注册失败:ConnectivityManager不可用");
|
||||
return;
|
||||
}
|
||||
networkCallback = new ConnectivityManager.NetworkCallback() {
|
||||
@Override
|
||||
public void onAvailable(@NonNull Network network) {
|
||||
checkNetworkTypeChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCapabilitiesChanged(@NonNull Network network,
|
||||
@NonNull NetworkCapabilities networkCapabilities) {
|
||||
checkNetworkTypeChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLost(@NonNull Network network) {
|
||||
checkNetworkTypeChanged();
|
||||
}
|
||||
};
|
||||
connectivityManager.registerDefaultNetworkCallback(networkCallback);
|
||||
checkNetworkTypeChanged();
|
||||
}
|
||||
|
||||
private void checkNetworkTypeChanged() {
|
||||
uploadHostInfoOnNetworkTypeChanged(NetState.getNetworkType(getApplication()));
|
||||
}
|
||||
|
||||
private synchronized void uploadHostInfoOnNetworkTypeChanged(int netType) {
|
||||
if (lastNetState == netType) {
|
||||
return;
|
||||
}
|
||||
int previousNetState = lastNetState;
|
||||
lastNetState = netType;
|
||||
MyLog.network("网络类型发生变化:" + previousNetState + " → " + netType + ",主动上传主机信息");
|
||||
UploadHostInfo();
|
||||
}
|
||||
|
||||
Weather weather;
|
||||
|
||||
@@ -348,7 +395,7 @@ public class AppBarViewModel extends BasePresenter {
|
||||
case 3 -> type = 1;
|
||||
}
|
||||
params.addParameter("Network", type);
|
||||
params.addParameter("IotCardCode", HardwareSetting.getImsi());
|
||||
params.addParameter("IotCardCode", HardwareSetting.getIccid());
|
||||
params.addParameter("MAC", HardwareSetting.getImei());
|
||||
x.http().post(params, new XHttpShorthand<CommonResponse>() {
|
||||
@Override
|
||||
@@ -362,4 +409,15 @@ public class AppBarViewModel extends BasePresenter {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCleared() {
|
||||
if (mTelephonyManager != null && networkStateListener != null) {
|
||||
mTelephonyManager.listen(networkStateListener, PhoneStateListener.LISTEN_NONE);
|
||||
}
|
||||
if (connectivityManager != null && networkCallback != null) {
|
||||
connectivityManager.unregisterNetworkCallback(networkCallback);
|
||||
}
|
||||
super.onCleared();
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,10 @@ package com.example.iot_controlhost.utils.global;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.example.iot_controlhost.MyApp;
|
||||
import com.example.iot_controlhost.utils.MMKVUtil;
|
||||
import com.example.iot_controlhost.utils.log.MyLog;
|
||||
|
||||
/**
|
||||
* 硬件相关配置
|
||||
@@ -20,17 +21,66 @@ public class HardwareSetting {
|
||||
*/
|
||||
public static final String IMEI = "IMEI";
|
||||
/**
|
||||
* 物联网卡号
|
||||
* ICCID(物联网卡号)
|
||||
*/
|
||||
public static String IMSI = "IMSI";
|
||||
public static final String ICCID = "ICCID";
|
||||
/**
|
||||
* IMSI(国际移动用户识别码)
|
||||
*/
|
||||
public static final String IMSI = "IMSI";
|
||||
|
||||
/**
|
||||
* 获取物联网卡号-IMSI国际移动用户识别码
|
||||
* 获取IMSI国际移动用户识别码
|
||||
*/
|
||||
public static String getImsi() {
|
||||
return MMKVUtil.get(IMSI, "-");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ICCID物联网卡号
|
||||
*/
|
||||
public static String getIccid() {
|
||||
return MMKVUtil.get(ICCID, "-");
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新蜂窝设备及SIM卡标识。
|
||||
* Android 8.0及以上使用getImei,Android 7.1使用getDeviceId兼容获取IMEI。
|
||||
*/
|
||||
public static void refreshMobileIdentifiers(Context context) {
|
||||
TelephonyManager telephonyManager =
|
||||
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
if (telephonyManager == null) {
|
||||
MMKVUtil.put(ICCID, "-");
|
||||
MMKVUtil.put(IMEI, "-");
|
||||
MyLog.appError("蜂窝设备标识获取失败:TelephonyManager不可用");
|
||||
return;
|
||||
}
|
||||
|
||||
String iccid = null;
|
||||
String imei = null;
|
||||
try {
|
||||
iccid = telephonyManager.getSimSerialNumber();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
imei = telephonyManager.getImei();
|
||||
} else {
|
||||
imei = telephonyManager.getDeviceId();
|
||||
}
|
||||
} catch (SecurityException e) {
|
||||
MyLog.appError("蜂窝设备标识获取失败:缺少读取设备标识权限");
|
||||
} catch (Exception e) {
|
||||
MyLog.appError("蜂窝设备标识获取失败:" + e.getMessage());
|
||||
}
|
||||
|
||||
// 始终覆盖旧值,避免历史版本将ICCID存入IMEI后继续错误上传。
|
||||
MMKVUtil.put(ICCID, normalizeIdentifier(iccid));
|
||||
MMKVUtil.put(IMEI, normalizeIdentifier(imei));
|
||||
}
|
||||
|
||||
private static String normalizeIdentifier(String identifier) {
|
||||
return TextUtils.isEmpty(identifier) ? "-" : identifier.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 控制器串口
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user