bh add mqtt

master
hwf452 10 months ago
parent 38bc5ce5e9
commit 9b2779f0c3

@ -107,6 +107,12 @@ dependencies {
implementation 'io.github.azhon:appupdate:4.2.6'
implementation 'com.tencent.bugly:crashreport:3.2.1'
implementation 'com.github.xuexiangjys:XUI:1.1.4'
//mqtt
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.4'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
implementation 'org.bouncycastle:bcpkix-jdk15on:1.56'
implementation 'commons-codec:commons-codec:1.15'
// testImplementation 'junit:junit:4.13.1'
// androidTestImplementation 'androidx.test.ext:junit:1.1.2'
// androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

@ -19,6 +19,7 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!--Android 12 中的新蓝牙权限结束-->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

@ -14,7 +14,6 @@ import android.view.View
import com.azhon.appupdate.listener.OnButtonClickListener
import com.azhon.appupdate.listener.OnDownloadListener
import com.azhon.appupdate.manager.DownloadManager
import com.google.gson.Gson
import com.rehome.bhdxj.R
import com.rehome.bhdxj.base.BaseActivityAutoToolbarViewBinding
import com.rehome.bhdxj.bean.ApkUpdateBean
@ -27,6 +26,7 @@ import com.rehome.bhdxj.utils.GsonUtils
import com.rehome.bhdxj.utils.HttpListener
import com.rehome.bhdxj.utils.NetworkUtil
import com.rehome.bhdxj.utils.NohttpUtils
import com.rehome.bhdxj.utils.RSAUtils
import com.rehome.bhdxj.utils.SPUtils
import com.rehome.bhdxj.utils.StatusBarUtil
import com.rehome.bhdxj.utils.UiUtlis
@ -60,17 +60,29 @@ class LoginActivity : BaseActivityAutoToolbarViewBinding<ActivityLoginBinding>()
override fun initView() {
binding.tvVersion.text = "版本V" + getVersionName()
StatusBarUtil.transparencyBar(this) //设置透明状态栏
UserID = SPUtils.get(this, Contans.USERID, "") as String
UserPwd = SPUtils.get(this, Contans.USERPWD, "") as String
val UserIDTemp = SPUtils.get(this, Contans.USERID, "") as String
val UserPwdTemp = SPUtils.get(this, Contans.USERPWD, "") as String
val save = SPUtils.get(this, "save", true) as Boolean
binding.savePwUser.isChecked = save
if (!TextUtils.isEmpty(UserIDTemp)) {
UserID = RSAUtils.decryptBASE64StrLocal(UserIDTemp);
binding.etUse.setText(UserID)
}
//binding.etUse.setText(UserID)
if (save) {
binding.etPwd.setText(UserPwd)
//binding.etPwd.setText(UserPwd)
if (!TextUtils.isEmpty(UserPwdTemp)) {
UserPwd = RSAUtils.decryptBASE64StrLocal(UserPwdTemp);
binding.etPwd.setText(UserPwd);
}
}
binding.etUse.setText("870064")
binding.etPwd.setText("Xdj@2025")
// binding.etUse.setText("870064")
// binding.etPwd.setText("Xdj@2025")
}
@ -84,7 +96,7 @@ class LoginActivity : BaseActivityAutoToolbarViewBinding<ActivityLoginBinding>()
private fun onclick() {
binding.btnLogin.setOnClickListener(View.OnClickListener {
if (isCanLogin()) {
if (NetworkUtil.hasNetWorkStatus(context)) {
if (NetworkUtil.canNetWorkConnect(context)) {
Log.i("app", "有网络")
onlineLogin()
} else {
@ -305,12 +317,12 @@ class LoginActivity : BaseActivityAutoToolbarViewBinding<ActivityLoginBinding>()
SPUtils.put(
context,
Contans.USERID,
binding.etUse.text.toString()
RSAUtils.encryptBASE64StrLocal(binding.etUse.getText().toString())
)
SPUtils.put(
context,
Contans.USERPWD,
binding.etPwd.text.toString()
RSAUtils.encryptBASE64StrLocal(binding.etPwd.getText().toString())
)
SPUtils.put(context, Contans.USERNAME, username)
SPUtils.put(

@ -34,6 +34,8 @@ import com.rehome.bhdxj.contans.Contans;
import com.rehome.bhdxj.utils.AppManager;
import com.rehome.bhdxj.utils.GsonUtils;
import com.rehome.bhdxj.utils.HttpListener;
import com.rehome.bhdxj.utils.MqttSSLPublishServer;
import com.rehome.bhdxj.utils.NetworkUtil;
import com.rehome.bhdxj.utils.NohttpUtils;
import com.rehome.bhdxj.utils.OAToolbar;
import com.rehome.bhdxj.utils.SPUtils;
@ -52,6 +54,8 @@ import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends BaseActivity3 {
@ -67,6 +71,10 @@ public class MainActivity extends BaseActivity3 {
private long exitTime = 0;
private BluetoothLeService mBluetoothLeService;
MqttSSLPublishServer mqttPublishServer;
//定时器
private Timer timer;
@Override
public int getLayoutId() {
@ -81,6 +89,19 @@ public class MainActivity extends BaseActivity3 {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//设置透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);//设置透明导航栏
StatusBarUtil.transparencyBar(MainActivity.this);//设置透明状态栏
if (NetworkUtil.canNetWorkConnect(context)) {
Log.i("app", "有网络");
} else {
Log.i("app", "无网络");
}
if(NetworkUtil.canNetWorkConnect(context)&& NetworkUtil.isISPNetWorkConnect(context)){
Log.i("app", "运营商网络");
initMqtt();
}
if(NetworkUtil.canNetWorkConnect(context)&& NetworkUtil.isWifiNetWorkConnect(context)){
Log.i("app", "wifi网络");
}
}
@Override
@ -212,14 +233,44 @@ public class MainActivity extends BaseActivity3 {
}
};
@Override
protected void onResume() {
super.onResume();
if(NetworkUtil.canNetWorkConnect(context)&&NetworkUtil.isISPNetWorkConnect(context)){
if(mqttPublishServer==null){
mqttPublishServer = new MqttSSLPublishServer(this);
}
if(timer==null){
try {
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
mqttPublishServer.start();
}
}, 1000, 60000);
// 设定指定的时间time,此处为10000毫秒
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
mBluetoothLeService.disconnect();
mBluetoothLeService.close();
unbindService(mServiceConnection);
Intent intent = new Intent(this, BluetoothLeService.class);
stopService(intent);
super.onDestroy();
if(mqttPublishServer!=null){
mqttPublishServer.destroyAll();
mqttPublishServer=null;
}
if(timer!=null){
timer.cancel();
}
}
//获取运行专业数据
@ -318,4 +369,20 @@ public class MainActivity extends BaseActivity3 {
}
}).build().download();
}
public void initMqtt() {
//开启MQTT一键招回
mqttPublishServer = new MqttSSLPublishServer(this);
try {
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
mqttPublishServer.start();
}
}, 1000, 60000);
// 设定指定的时间time,此处为10000毫秒
} catch (Exception e) {
e.printStackTrace();
}
}
}

@ -12,6 +12,7 @@ import com.rehome.bhdxj.bean.QXGDResultBean;
import com.rehome.bhdxj.contans.Contans;
import com.rehome.bhdxj.utils.GsonUtils;
import com.rehome.bhdxj.utils.HttpUtils;
import com.rehome.bhdxj.utils.RSAUtils;
import com.rehome.bhdxj.utils.SPUtils;
import com.rehome.bhdxj.weight.DateTimePickDialog;
import com.rehome.bhdxj.weight.InputLayout;
@ -310,6 +311,9 @@ public class QxgdActivity extends BaseActivity3 {
}
private String GetInfo() {
String usernameTemp = (String) SPUtils.get(context, Contans.USERID,String.valueOf(""));
String username = RSAUtils.decryptBASE64StrLocal(usernameTemp);
String json = "";
Map<String, String> map = new HashMap<String, String>();
map.put("description", etQxms.getContentText());
@ -317,7 +321,7 @@ public class QxgdActivity extends BaseActivity3 {
map.put("profession", ZYValue);
map.put("bugtype", LXvalue);
map.put("buglevel", QXDJValue);
map.put("reportedby", (String) SPUtils.get(context, Contans.USERID, ""));
map.put("reportedby", username);
map.put("bugdept", GDAPValue);
json = GsonUtils.GsonString(map);
return json;

@ -22,6 +22,7 @@ import com.rehome.bhdxj.contans.Contans;
import com.rehome.bhdxj.utils.GsonUtils;
import com.rehome.bhdxj.utils.HttpListener;
import com.rehome.bhdxj.utils.HttpResponseListener;
import com.rehome.bhdxj.utils.RSAUtils;
import com.rehome.bhdxj.utils.SPUtils;
import com.rehome.bhdxj.utils.UiUtlis;
import com.rehome.bhdxj.weight.toastviewbymyself;
@ -198,7 +199,10 @@ public class DjdscFragment extends BaseFragment {
private void uploadData() {
uploadCecked = 0;
uploadSuccess = 0;
String USERNAME = (String) SPUtils.get(context, Contans.USERID, "");
String usernameTemp = (String) SPUtils.get(context, Contans.USERID,String.valueOf(""));
String USERNAME = RSAUtils.decryptBASE64StrLocal(usernameTemp);
for (int i = 0; i < xdjjhxzDataList.size(); i++) {
if (xdjjhxzDataList.get(i).isChecked()) {
uploadCecked++;

@ -19,6 +19,7 @@ import com.rehome.bhdxj.contans.Contans;
import com.rehome.bhdxj.utils.GsonUtils;
import com.rehome.bhdxj.utils.HttpListener;
import com.rehome.bhdxj.utils.NohttpUtils;
import com.rehome.bhdxj.utils.RSAUtils;
import com.rehome.bhdxj.utils.SPUtils;
import com.rehome.bhdxj.utils.StatusBarUtil;
import com.yolanda.nohttp.NoHttp;
@ -141,12 +142,14 @@ public class MainSbxdjglActivity extends BaseActivity {
};
private String createZyJson() {
String usernameTemp = (String) SPUtils.get(context, Contans.USERID,String.valueOf(""));
String username = RSAUtils.decryptBASE64StrLocal(usernameTemp);
String GWID = (String) SPUtils.get(getApplicationContext(), Contans.BZBH, "");
String YHID = (String) SPUtils.get(getApplicationContext(), Contans.USERID, "");
XscbRequestBean info = new XscbRequestBean();
info.setAction("DJ_GWLIST_GET");
info.setGWID(GWID);
info.setYHID(YHID);
info.setYHID(username);
String json = GsonUtils.GsonString(info);
return json;
}

@ -22,6 +22,7 @@ import com.rehome.bhdxj.contans.Contans;
import com.rehome.bhdxj.utils.GsonUtils;
import com.rehome.bhdxj.utils.HttpListener;
import com.rehome.bhdxj.utils.HttpResponseListener;
import com.rehome.bhdxj.utils.RSAUtils;
import com.rehome.bhdxj.utils.SPUtils;
import com.rehome.bhdxj.utils.UiUtlis;
import com.rehome.bhdxj.weight.WaitDialog;
@ -244,7 +245,8 @@ public class XzjhFragment extends BaseFragment {
}
private String createZyJson(String gwid) {
String YHID = (String) SPUtils.get(context, Contans.USERID, "");
String usernameTemp = (String) SPUtils.get(context, Contans.USERID,String.valueOf(""));
String YHID = RSAUtils.decryptBASE64StrLocal(usernameTemp);
XscbRequestBean info = new XscbRequestBean();
info.setAction("DJ_RWXZ_GET");
info.setGWID(gwid);

@ -24,6 +24,7 @@ import com.rehome.bhdxj.contans.Contans;
import com.rehome.bhdxj.utils.GsonUtils;
import com.rehome.bhdxj.utils.HttpListener;
import com.rehome.bhdxj.utils.HttpResponseListener;
import com.rehome.bhdxj.utils.RSAUtils;
import com.rehome.bhdxj.utils.SPUtils;
import com.rehome.bhdxj.utils.UiUtlis;
import com.rehome.bhdxj.weight.WaitDialog;
@ -255,7 +256,11 @@ public class ScxsjhFragment extends BaseFragment {
datas.setSBZT("1");
}
datas.setZcmc((String) SPUtils.get(context, Contans.BZBH, ""));
datas.setCbr((String) SPUtils.get(context, Contans.USERID, ""));
String usernameTemp = (String) SPUtils.get(context, Contans.USERID,String.valueOf(""));
String username = RSAUtils.decryptBASE64StrLocal(usernameTemp);
datas.setCbr(username);
datas.setFxnr(xsjjhdatabeanList.get(j).getFxnr());
datas.setSmfx("");
datalist.add(datas);

@ -13,6 +13,7 @@ import com.rehome.bhdxj.base.BaseActivity;
import com.rehome.bhdxj.utils.GsonUtils;
import com.rehome.bhdxj.utils.HttpListener;
import com.rehome.bhdxj.utils.NohttpUtils;
import com.rehome.bhdxj.utils.RSAUtils;
import com.rehome.bhdxj.utils.SPUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
@ -131,7 +132,11 @@ public class XsHistoryActivity extends BaseActivity {
info.setZc((String) SPUtils.get(context, Contans.BZBH, ""));
info.setRqts("4");
info.setZymc(Contans.YXCB_ZY_ID);
info.setYhid((String) SPUtils.get(context, Contans.USERID, ""));//抄表人
String usernameTemp = (String) SPUtils.get(context, Contans.USERID,String.valueOf(""));
String username = RSAUtils.decryptBASE64StrLocal(usernameTemp);
info.setYhid(username);//抄表人
String json = GsonUtils.GsonString(info);
return json;

@ -16,6 +16,7 @@ import com.rehome.bhdxj.base.BaseActivity;
import com.rehome.bhdxj.utils.GsonUtils;
import com.rehome.bhdxj.utils.HttpListener;
import com.rehome.bhdxj.utils.NohttpUtils;
import com.rehome.bhdxj.utils.RSAUtils;
import com.rehome.bhdxj.utils.SPUtils;
import com.rehome.bhdxj.weight.AutoListView;
import com.yolanda.nohttp.NoHttp;
@ -208,7 +209,10 @@ public class XscbqyWorkActivity extends BaseActivity {
bean.setZcmc((String) SPUtils.get(context, Contans.BZBH, ""));//班组
bean.setCbr((String) SPUtils.get(context, Contans.USERID, ""));//抄表人
String usernameTemp = (String) SPUtils.get(context, Contans.USERID,String.valueOf(""));
String username = RSAUtils.decryptBASE64StrLocal(usernameTemp);
bean.setCbr(username);//抄表人
bean.setFxnr("");//分析内容
bean.setSmfx(smfx);//扫描方式

@ -16,6 +16,7 @@ import com.rehome.bhdxj.base.BaseActivity;
import com.rehome.bhdxj.utils.GsonUtils;
import com.rehome.bhdxj.utils.HttpListener;
import com.rehome.bhdxj.utils.NohttpUtils;
import com.rehome.bhdxj.utils.RSAUtils;
import com.rehome.bhdxj.utils.SPUtils;
import com.rehome.bhdxj.weight.AutoListView;
import com.yolanda.nohttp.NoHttp;
@ -187,7 +188,10 @@ public class XscbqyWorkXsActivity extends BaseActivity {
bean.setZcmc((String) SPUtils.get(context, Contans.BZBH, ""));//班组
bean.setCbr((String) SPUtils.get(context, Contans.USERID, ""));//抄表人
String usernameTemp = (String) SPUtils.get(context, Contans.USERID,String.valueOf(""));
String username = RSAUtils.decryptBASE64StrLocal(usernameTemp);
bean.setCbr(username);//抄表人
bean.setFxnr("");//分析内容
bean.setSmfx(smfx);//扫描方式
dataBeanList.add(bean);

@ -18,6 +18,7 @@ import com.rehome.bhdxj.bean.YhpcResultBean;
import com.rehome.bhdxj.contans.Contans;
import com.rehome.bhdxj.utils.Api;
import com.rehome.bhdxj.utils.GsonUtils;
import com.rehome.bhdxj.utils.RSAUtils;
import com.rehome.bhdxj.utils.RetrofitHttpUtils;
import com.rehome.bhdxj.utils.SPUtils;
import org.litepal.crud.DataSupport;
@ -64,7 +65,9 @@ public class YhpcDataListActivity extends BaseActivity3 {
startActivity(YhpcTakePhotoActivity.class);
}
});
SCR = (String) SPUtils.get(context, Contans.USERID, "");
String usernameTemp = (String) SPUtils.get(context, Contans.USERID,String.valueOf(""));
SCR = RSAUtils.decryptBASE64StrLocal(usernameTemp);
lv = findViewById(R.id.lv);
tvNodata = findViewById(R.id.tv_nodata);

@ -11,6 +11,7 @@ import android.widget.TextView;
import com.rehome.bhdxj.DBModel.Xjzjrw;
import com.rehome.bhdxj.R;
import com.rehome.bhdxj.contans.Contans;
import com.rehome.bhdxj.utils.RSAUtils;
import com.rehome.bhdxj.utils.SPUtils;
import com.zhy.autolayout.utils.AutoUtils;
@ -76,7 +77,8 @@ public class ZjrwxjAdapter extends BaseAdapter {
holder.tv_zyfzr.setText(list.get(i).getProusername());
holder.tv_zjsj.setText(list.get(i).getZjtime());
String YHID = (String) SPUtils.get(context, Contans.USERID, "");
String usernameTemp = (String) SPUtils.get(context, Contans.USERID,String.valueOf(""));
String YHID = RSAUtils.decryptBASE64StrLocal(usernameTemp);
if (list.get(i).getProuserid().equals(YHID)) {//当前用户是主要负责人则可以下载
holder.cb.setEnabled(true);

@ -83,6 +83,7 @@ public class Contans {
public final static String PERMISSIONSRESULT = "PermissionsResult";
public final static String USERID = "USERID";
public final static String USERPWD = "USERPWD";
public final static String NFCBM = "NFCBM";
public final static String BZBH = "bzbh";
public final static String BZMC = "bzmc";
public final static String USERNAME = "USERNAME";

@ -16,6 +16,7 @@ import com.rehome.bhdxj.contans.Contans;
import com.rehome.bhdxj.utils.GsonUtils;
import com.rehome.bhdxj.utils.HttpListener;
import com.rehome.bhdxj.utils.NohttpUtils;
import com.rehome.bhdxj.utils.RSAUtils;
import com.rehome.bhdxj.utils.SPUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
@ -156,7 +157,10 @@ public class PushService extends IntentService {
private String createZyJson() {
String GWID = (String) SPUtils.get(getApplicationContext(), Contans.BZBH, "");
String YHID = (String) SPUtils.get(getApplicationContext(), Contans.USERID, "");
String usernameTemp = (String) SPUtils.get(this, Contans.USERID,String.valueOf(""));
String YHID = RSAUtils.decryptBASE64StrLocal(usernameTemp);
XscbRequestBean info = new XscbRequestBean();
info.setAction("DJ_GWLIST_GET");
info.setGWID(GWID);

@ -0,0 +1,211 @@
package com.rehome.bhdxj.utils;
/**
* @author huangwenfei
* @version v1.0.0.0
* Created DateTime 2021-03-08 14:52
* @description: mqtt 线
*/
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;
import com.rehome.bhdxj.contans.Contans;
import com.google.gson.Gson;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
public class MqttSSLPublishServer {
/**
* ip
*/
//private final String HOST = "tcp://39.101.173.20:1883";
//private final String HOST = "ssl://39.101.173.20:8883";
private final String HOST = "tcp://47.242.184.139:1883";
//private final String HOST = "ssl://47.242.184.139:8883";
//private final String HOST = "tcp://113.107.214.27:1883";
/**
*
*/
private final String topicPush = "app_push_bh";
/**
*
*/
private final String topicReceive = "member_recall";
/**
* 线
*/
private String clientid = "v1_server_ssl_android";
private MqttClient client;
private MqttConnectOptions options;
/**
* MQTT
*/
private final String userName = "admin";
/**
* MQTT
*/
//private final String passWord = "public452131wW452131wW$";
private final String passWord = "publish452131wW452131wW$";
/**
*
* 0<=1
* 1>=1
* 2=1
*/
private int qos = 2;
// 推送消息
private MqttMessage message;
private Context context;
public MqttSSLPublishServer(Context context) {
this.context=context;
// host为主机名clientid即连接MQTT的客户端ID一般以唯一标识符表示MemoryPersistence设置clientid的保存形式默认为以内存保存
try {
clientid=(String) SPUtils.get(context,"clientid",String.valueOf(""));
if(TextUtils.isEmpty(clientid)){
clientid=UUID.randomUUID().toString();
SPUtils.put(context,"clientid",clientid);
}
client = new MqttClient(HOST, clientid, new MemoryPersistence());
// MQTT的连接设置
options = new MqttConnectOptions();
// 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录这里设置为true表示每次连接到服务器都以新的身份连接
options.setCleanSession(false);
// 设置连接的用户名
options.setUserName(userName);
// 设置连接的密码
options.setPassword(passWord.toCharArray());
// 设置超时时间 单位为秒
options.setConnectionTimeout(10);
// 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线但这个方法并没有重连的机制
options.setKeepAliveInterval(20);
//断线重连
options.setAutomaticReconnect(true);
// 发布目的消息对象
message = new MqttMessage();
//mqtt服务器端单向加密
// SSLSocketFactory socketFactory = MqttSSLSocketFactory.getSingleSocketFactory(context);
// options.setSocketFactory(socketFactory);
//mqtt服务器端单双向加密
//SSLSocketFactory socketFactory = MqttSSLSocketFactory.getTwoDirSocketFactory(" ",context);
//options.setSocketFactory(socketFactory);
// 设置回调
client.setCallback(new MqttCallbackExtended() {
@Override
public void connectComplete(boolean reconnect, String serverURI) {
//连接成功后调用
try {
client.subscribe(topicReceive,qos);//具体订阅代码
} catch (MqttException e) {
e.printStackTrace();
}
}
public void connectionLost(Throwable cause) {
//断线后,重新连接
try {
client.reconnect();
} catch (MqttException e) {
e.printStackTrace();
}
}
public void messageArrived(String topic, MqttMessage message) throws Exception {
//收到招回指令
String callbackStr = new String(message.getPayload());
if(callbackStr.length()>0){
Log.i("app",callbackStr);
}
}
public void deliveryComplete(IMqttDeliveryToken token) {
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public void start() {
try {
message.setQos(qos);
message.setRetained(true);
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String usernameTemp = (String) SPUtils.get(context, Contans.USERID,String.valueOf(""));
String passwordTemp = (String) SPUtils.get(context,Contans.USERPWD,String.valueOf(""));
String username =RSAUtils.decryptBASE64StrLocal(usernameTemp);
String password = RSAUtils.decryptBASE64StrLocal(passwordTemp);
String nfc = (String) SPUtils.get(context,Contans.NFCBM,String.valueOf(""));
String name = (String) SPUtils.get(context, Contans.USERNAME, String.valueOf(""));
HashMap<String, String> mapPush = new HashMap<>();
mapPush.put("username",username);
mapPush.put("password",password);
mapPush.put("date",sd.format(new Date()));
mapPush.put("appversion", getVersionName());
mapPush.put("nfc",nfc);
mapPush.put("name", name);
Gson gson = new Gson();
try {
//判断拦截状态,这里注意一下,如果没有这个判断,是非常坑的
if (!client.isConnected()) {
// 重新连接
client.connect(options);
// 订阅
client.subscribe(topicReceive,qos);
}
if (client.isConnected()) {//连接成功,跳出连接
// 发布消息
String messageEn = RSAAndroid.encryptByPublicKeyForSpiltStr(gson.toJson(mapPush),RSAAndroid.publicRsaKey);
message.setPayload(messageEn.getBytes());
client.publish(topicPush, message);
}
} catch (Exception e1) {
e1.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private String getVersionName() {
//1,包管理者对象packageManager
PackageManager pm = this.context.getPackageManager();
//2,从包的管理者对象中,获取指定包名的基本信息(版本名称,版本号),传0代表获取基本信息
try {
PackageInfo packageInfo = pm.getPackageInfo(this.context.getPackageName(), 0);
//3,获取版本名称
return packageInfo.versionName;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void destroyAll() {
try {
if(client.isConnected()){
client.disconnect();
}
// 关闭客户端
client.close();
} catch (MqttException e) {
e.printStackTrace();
}
}
}

@ -1,63 +1,84 @@
package com.rehome.bhdxj.utils;
/**
* Create By HuangWenFei
* 2023-05-19 15:35
*
*/
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
/**
* Create By HuangWenFei
* 2023-05-18 16:06
*
*/
public class NetworkUtil {
private NetworkUtil() {}
/**
* Wifi
*
*
* @param context
* @param needWifi wifi
*/
public static boolean hasNetWorkWifiStatus(Context context) {
public static boolean hasNetWorkStatus(Context context, boolean needWifi) {
NetworkInfo info = getActiveNetwork(context);
if (info == null) {
//网络服务不可用
Log.i("app", "网络服务不可用");
return false;
}
if (info.getType() == ConnectivityManager.TYPE_WIFI) {
//有WIFI网络
Log.i("app", "有WIFI网络");
if (!needWifi) {
return info.isAvailable();
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
return info.isAvailable();
}
//无网络
Log.i("app", "无网络");
return false;
}
/**
*
* wifi
*
* @param context
*/
public static boolean hasNetWorkStatus(Context context) {
public static boolean isWifiNetWorkConnect(Context context) {
NetworkInfo info = getActiveNetwork(context);
if (info == null) {
//网络服务不可用
Log.i("app", "网络服务不可用");
return false;
}
if(info.isAvailable()){
//有运营商网络
Log.i("app", "网络可用");
if (info.getType() == ConnectivityManager.TYPE_WIFI) {
return info.isAvailable();
}
//无运营商网络
Log.i("app", "无网络可用");
return false;
}
/**
*
*
* @param context
*/
public static boolean isISPNetWorkConnect(Context context) {
NetworkInfo info = getActiveNetwork(context);
if (info == null) {
return false;
}
if (info.getType() == ConnectivityManager.TYPE_WIFI) {
}else{
return true;
}
return false;
}
/**
*
* @param context
*/
public static boolean canNetWorkConnect(Context context) {
NetworkInfo info = getActiveNetwork(context);
if (info == null) {
return false;
}
return true;
}
/**
*
*

@ -0,0 +1,387 @@
package com.rehome.bhdxj.utils;
/**
* @ Author : huangwenfei
* @ Date : Created in 2022/3/8 14:44
* @ Version : $1.0.0.0
* @ Description:
*/
import android.util.Base64;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.List;
import javax.crypto.Cipher;
public class RSAAndroid {
private static String TAG = "RSAAndroid";
public static final String RSA = "RSA";// 非对称加密密钥算法
/**加密方式android的*/
// public static final String ECB_PKCS1_PADDING = "RSA/None/NoPadding";
/**加密方式标准jdk的*/
//public static final String ECB_PKCS1_PADDING = "RSA/None/PKCS1Padding";
public static final String ECB_PKCS1_PADDING = "RSA/ECB/PKCS1Padding";
public static final int DEFAULT_KEY_SIZE = 2048;//秘钥默认长度
public static final byte[] DEFAULT_SPLIT = "#PART#".getBytes(); // 当要加密的内容超过bufferSize则采用partSplit进行分块加密
public static final int DEFAULT_BUFFERSIZE = (DEFAULT_KEY_SIZE / 8) - 11;// 当前秘钥支持加密的最大字节数
public static String publicRsaKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmMLyJw1CAl25lnDgEeYZvOps+1pSi93Q39djEniGNo5uUKVEkqDIayTli2zreX10HqT2jTtDN9APtwuEhWazP/VgOXoWsztbtZtSwJGM6Eg0R9zDCbKyQt5Qhg3jkTrXrvrGn7j/ZP56VNWELv/i5dsRCTccr1MeIyxjOC2pojCOsrTN4HZzgBj+GEUKPRLcKOiPfOsoP7HgkAua82vTOIgWpqIp+1PIfcjjCqzOsSv5PQnGP75+flIXtz75OKo/9hX9zl5JHNcH3SC6nS8Czii9E292XIsBtKdQijvNMn+YcmKFo6mZOUXHdO506NoKkxRny5fbKiPf/oqTA7Zx5QIDAQAB";
public static byte[] decryptBASE64(String key) throws Exception {
return Base64.decode(key, Base64.DEFAULT);
}
public static String encryptBASE64(byte[] key) throws Exception {
return Base64.encodeToString(key, Base64.DEFAULT);
}
/**
* RSA
*
* @param keyLength 5122048
* 1024
* @return
*/
public static KeyPair generateRSAKeyPair(int keyLength) {
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA);
kpg.initialize(keyLength);
return kpg.genKeyPair();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
/**
*
*
* @param data
*/
public static byte[] encryptByPublicKey(byte[] data, byte[] publicKey) throws Exception {
// 得到公钥
byte[] decoded = Base64.decode(publicKey, Base64.DEFAULT);
RSAPublicKey keyPublic = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
// 加密数据
Cipher cp = Cipher.getInstance(ECB_PKCS1_PADDING);
cp.init(Cipher.ENCRYPT_MODE, keyPublic);
return cp.doFinal(data);
}
/**
*
*
* @param data
* @param privateKey
* @return byte[]
*/
public static byte[] encryptByPrivateKey(byte[] data, byte[] privateKey) throws Exception {
// 得到私钥
byte[] decoded = Base64.decode(privateKey, Base64.DEFAULT);
RSAPrivateKey keyPrivate = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
// 数据加密
Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING);
cipher.init(Cipher.ENCRYPT_MODE, keyPrivate);
return cipher.doFinal(data);
}
/**
*
*
* @param data
* @param publicKey
* @return byte[]
*/
public static byte[] decryptByPublicKey(byte[] data, byte[] publicKey) throws Exception {
// 得到公钥
byte[] decoded = Base64.decode(publicKey, Base64.DEFAULT);
RSAPublicKey keyPublic = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
// 数据解密
Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING);
cipher.init(Cipher.DECRYPT_MODE, keyPublic);
return cipher.doFinal(data);
}
/**
* 使
*/
public static byte[] decryptByPrivateKey(byte[] encrypted, byte[] privateKey) throws Exception {
// 得到私钥
byte[] decoded = Base64.decode(privateKey, Base64.DEFAULT);
RSAPrivateKey keyPrivate = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
// 解密数据
Cipher cp = Cipher.getInstance(ECB_PKCS1_PADDING);
cp.init(Cipher.DECRYPT_MODE, keyPrivate);
byte[] arr = cp.doFinal(encrypted);
return arr;
}
/**
*
*/
public static byte[] encryptByPublicKeyForSpilt(byte[] data, byte[] publicKey) throws Exception {
int dataLen = data.length;
if (dataLen <= DEFAULT_BUFFERSIZE) {
return encryptByPublicKey(data, publicKey);
}
List<Byte> allBytes = new ArrayList<Byte>(2048);
int bufIndex = 0;
int subDataLoop = 0;
byte[] buf = new byte[DEFAULT_BUFFERSIZE];
for (int i = 0; i < dataLen; i++) {
buf[bufIndex] = data[i];
if (++bufIndex == DEFAULT_BUFFERSIZE || i == dataLen - 1) {
subDataLoop++;
if (subDataLoop != 1) {
for (byte b : DEFAULT_SPLIT) {
allBytes.add(b);
}
}
byte[] encryptBytes = encryptByPublicKey(buf, publicKey);
for (byte b : encryptBytes) {
allBytes.add(b);
}
bufIndex = 0;
if (i == dataLen - 1) {
buf = null;
} else {
buf = new byte[Math.min(DEFAULT_BUFFERSIZE, dataLen - i - 1)];
}
}
}
byte[] bytes = new byte[allBytes.size()];
{
int i = 0;
for (Byte b : allBytes) {
bytes[i++] = b.byteValue();
}
}
return bytes;
}
/**
* 使
*
* @param data
* @param privateKey
*/
public static byte[] encryptByPrivateKeyForSpilt(byte[] data, byte[] privateKey) throws Exception {
int dataLen = data.length;
if (dataLen <= DEFAULT_BUFFERSIZE) {
return encryptByPrivateKey(data, privateKey);
}
List<Byte> allBytes = new ArrayList<Byte>(2048);
int bufIndex = 0;
int subDataLoop = 0;
byte[] buf = new byte[DEFAULT_BUFFERSIZE];
for (int i = 0; i < dataLen; i++) {
buf[bufIndex] = data[i];
if (++bufIndex == DEFAULT_BUFFERSIZE || i == dataLen - 1) {
subDataLoop++;
if (subDataLoop != 1) {
for (byte b : DEFAULT_SPLIT) {
allBytes.add(b);
}
}
byte[] encryptBytes = encryptByPrivateKey(buf, privateKey);
for (byte b : encryptBytes) {
allBytes.add(b);
}
bufIndex = 0;
if (i == dataLen - 1) {
buf = null;
} else {
buf = new byte[Math.min(DEFAULT_BUFFERSIZE, dataLen - i - 1)];
}
}
}
byte[] bytes = new byte[allBytes.size()];
{
int i = 0;
for (Byte b : allBytes) {
bytes[i++] = b.byteValue();
}
}
return bytes;
}
/**
*
*
* @param encrypted
* @param publicKey
*/
public static byte[] decryptByPublicKeyForSpilt(byte[] encrypted, byte[] publicKey) throws Exception {
int splitLen = DEFAULT_SPLIT.length;
if (splitLen <= 0) {
return decryptByPublicKey(encrypted, publicKey);
}
int dataLen = encrypted.length;
List<Byte> allBytes = new ArrayList<Byte>(1024);
int latestStartIndex = 0;
for (int i = 0; i < dataLen; i++) {
byte bt = encrypted[i];
boolean isMatchSplit = false;
if (i == dataLen - 1) {
// 到data的最后了
byte[] part = new byte[dataLen - latestStartIndex];
System.arraycopy(encrypted, latestStartIndex, part, 0, part.length);
byte[] decryptPart = decryptByPublicKey(part, publicKey);
for (byte b : decryptPart) {
allBytes.add(b);
}
latestStartIndex = i + splitLen;
i = latestStartIndex - 1;
} else if (bt == DEFAULT_SPLIT[0]) {
// 这个是以split[0]开头
if (splitLen > 1) {
if (i + splitLen < dataLen) {
// 没有超出data的范围
for (int j = 1; j < splitLen; j++) {
if (DEFAULT_SPLIT[j] != encrypted[i + j]) {
break;
}
if (j == splitLen - 1) {
// 验证到split的最后一位都没有break则表明已经确认是split段
isMatchSplit = true;
}
}
}
} else {
// split只有一位则已经匹配了
isMatchSplit = true;
}
}
if (isMatchSplit) {
byte[] part = new byte[i - latestStartIndex];
System.arraycopy(encrypted, latestStartIndex, part, 0, part.length);
byte[] decryptPart = decryptByPublicKey(part, publicKey);
for (byte b : decryptPart) {
allBytes.add(b);
}
latestStartIndex = i + splitLen;
i = latestStartIndex - 1;
}
}
byte[] bytes = new byte[allBytes.size()];
{
int i = 0;
for (Byte b : allBytes) {
bytes[i++] = b.byteValue();
}
}
return bytes;
}
/**
* 使
*/
public static byte[] decryptByPrivateKeyForSpilt(byte[] encrypted, byte[] privateKey) throws Exception {
int splitLen = DEFAULT_SPLIT.length;
if (splitLen <= 0) {
return decryptByPrivateKey(encrypted, privateKey);
}
int dataLen = encrypted.length;
List<Byte> allBytes = new ArrayList<Byte>(1024);
int latestStartIndex = 0;
for (int i = 0; i < dataLen; i++) {
byte bt = encrypted[i];
boolean isMatchSplit = false;
if (i == dataLen - 1) {
// 到data的最后了
byte[] part = new byte[dataLen - latestStartIndex];
System.arraycopy(encrypted, latestStartIndex, part, 0, part.length);
byte[] decryptPart = decryptByPrivateKey(part, privateKey);
for (byte b : decryptPart) {
allBytes.add(b);
}
latestStartIndex = i + splitLen;
i = latestStartIndex - 1;
} else if (bt == DEFAULT_SPLIT[0]) {
// 这个是以split[0]开头
if (splitLen > 1) {
if (i + splitLen < dataLen) {
// 没有超出data的范围
for (int j = 1; j < splitLen; j++) {
if (DEFAULT_SPLIT[j] != encrypted[i + j]) {
break;
}
if (j == splitLen - 1) {
// 验证到split的最后一位都没有break则表明已经确认是split段
isMatchSplit = true;
}
}
}
} else {
// split只有一位则已经匹配了
isMatchSplit = true;
}
}
if (isMatchSplit) {
byte[] part = new byte[i - latestStartIndex];
System.arraycopy(encrypted, latestStartIndex, part, 0, part.length);
byte[] decryptPart = decryptByPrivateKey(part, privateKey);
for (byte b : decryptPart) {
allBytes.add(b);
}
latestStartIndex = i + splitLen;
i = latestStartIndex - 1;
}
}
byte[] bytes = new byte[allBytes.size()];
{
int i = 0;
for (Byte b : allBytes) {
bytes[i++] = b.byteValue();
}
}
return bytes;
}
/**
*
*/
public static String encryptByPublicKeyForSpiltStr(String data, String publicKey) throws Exception {
byte[] encryptBytes = encryptByPublicKeyForSpilt(data.getBytes(StandardCharsets.UTF_8), publicKey.getBytes());
return encryptBASE64(encryptBytes);
}
/**
* 使
*
* @param data
* @param privateKey
*/
public static String encryptByPrivateKeyForSpiltStr(String data, String privateKey) throws Exception {
byte[] encryptBytes = encryptByPrivateKeyForSpilt(data.getBytes(StandardCharsets.UTF_8), privateKey.getBytes());
return encryptBASE64(encryptBytes);
}
/**
*
*
* @param encrypted
* @param publicKey
*/
public static String decryptByPublicKeyForSpiltStr(String encrypted, String publicKey) throws Exception {
byte[] decryptBytes = decryptByPublicKeyForSpilt(decryptBASE64(encrypted), publicKey.getBytes());
return new String(decryptBytes,StandardCharsets.UTF_8);
}
/**
* 使
*/
public static String decryptByPrivateKeyForSpiltStr(String encrypted, String privateKey) throws Exception {
byte[] decryptBytes = decryptByPrivateKeyForSpilt(decryptBASE64(encrypted), privateKey.getBytes());
return new String(decryptBytes,StandardCharsets.UTF_8);
}
}

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save