扫描蓝牙二维码,准备配网
This commit is contained in:
@@ -220,7 +220,7 @@ public class AddDeviceActivity_copy extends BaseActivity<AddDeviceLayoutBinding,
|
||||
ToastUtil.showShort(mContext, "扫描失败,错误码: " + errorCode);
|
||||
}
|
||||
};
|
||||
scanner.startScan(null, settings, mScanCallback);
|
||||
scanner.startScan(filters, settings, mScanCallback);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -133,7 +133,11 @@ public class DeviceConnectActivity extends BaseActivity<DeviceConnectLayoutBindi
|
||||
handler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
goTo(mContext, DeviceReNameActivity.class, null);
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("wifi_name", mWifiName);
|
||||
bundle.putString("wifi_password", mWifiPassword);
|
||||
bundle.putSerializable("ble_device_entity", mBleDeviceEntity);
|
||||
goTo(mContext, DeviceReNameActivity.class, bundle);
|
||||
finish();
|
||||
}
|
||||
}, 800);
|
||||
|
||||
@@ -23,6 +23,11 @@ import com.gyf.immersionbar.ImmersionBar;
|
||||
*/
|
||||
public class DeviceReNameActivity extends BaseActivity<SetDeviceNameLayoutBinding, DevicePresenter>
|
||||
implements DeviceContract.DeviceView {
|
||||
|
||||
private String mWifiName;
|
||||
private String mWifiPassword;
|
||||
private WaterDeviceEntity mBleDeviceEntity;
|
||||
|
||||
@Override
|
||||
public DevicePresenter createPresenter() {
|
||||
return new DevicePresenter(new DeviceImpl());
|
||||
@@ -31,6 +36,13 @@ public class DeviceReNameActivity extends BaseActivity<SetDeviceNameLayoutBindin
|
||||
@Override
|
||||
public void initData(Bundle savedInstanceState) {
|
||||
super.initData(savedInstanceState);
|
||||
Bundle bundle = getIntent().getExtras();
|
||||
if (bundle != null) {
|
||||
mWifiName = bundle.getString("wifi_name", "");
|
||||
mWifiPassword = bundle.getString("wifi_password", "");
|
||||
mBleDeviceEntity = (WaterDeviceEntity) bundle.getSerializable("ble_device_entity");
|
||||
}
|
||||
|
||||
ImmersionBar.with(this)
|
||||
.statusBarColor(R.color.color_F9F9F9) // 设置状态栏颜色
|
||||
.statusBarDarkFont(true) // 设置状态栏字体为黑色
|
||||
@@ -60,10 +72,11 @@ public class DeviceReNameActivity extends BaseActivity<SetDeviceNameLayoutBindin
|
||||
ToastUtil.showShort(mContext,getString(R.string.input_device_name_hint));
|
||||
return;
|
||||
}
|
||||
String deviceName = viewBinding.etDeviceName.getText().toString().trim();
|
||||
// ToastUtil.showShort(mContext,"名称保存成功");
|
||||
// goTo(mContext,DeviceControlActivity.class,null);
|
||||
showLoadingDialog();
|
||||
mPresenter.addDevice();
|
||||
mPresenter.addDevice(deviceName,mWifiName,mWifiPassword,mBleDeviceEntity);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -80,7 +93,9 @@ public class DeviceReNameActivity extends BaseActivity<SetDeviceNameLayoutBindin
|
||||
new Handler().postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
goTo(mContext,DeviceControlActivity.class,null);
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putSerializable("device_info",mBleDeviceEntity);
|
||||
goTo(mContext,DeviceControlActivity.class,bundle);
|
||||
finish();
|
||||
}
|
||||
},800);
|
||||
|
||||
@@ -309,6 +309,7 @@ public class SelectWifiActivity extends BaseActivity<DeviceResetWifiBinding, Dev
|
||||
* 连接设备
|
||||
*/
|
||||
private void scheduleBleConnect() {
|
||||
showLoadingDialog();
|
||||
bleConnectHandler.removeCallbacksAndMessages(null);
|
||||
bleConnectHandler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
@@ -325,7 +326,6 @@ public class SelectWifiActivity extends BaseActivity<DeviceResetWifiBinding, Dev
|
||||
if (isBleConnected || isBleConnecting) {
|
||||
return;
|
||||
}
|
||||
showLoadingDialog();
|
||||
espProvisionManager.connect(mBleDeviceEntity.getMacAddress(), mBleDeviceEntity.getDeviceName());
|
||||
}
|
||||
|
||||
|
||||
@@ -21,18 +21,21 @@ public class DevicePresenter extends BasePresenter<DeviceContract.DeviceView> {
|
||||
|
||||
/**
|
||||
* 添加设备
|
||||
* @param mWifiName
|
||||
* @param mWifiPassword
|
||||
* @param mBleDeviceEntity
|
||||
*/
|
||||
public void addDevice() {
|
||||
public void addDevice(String deviceName,String mWifiName, String mWifiPassword, WaterDeviceEntity mBleDeviceEntity) {
|
||||
try {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("deviceNo",System.currentTimeMillis() + "");
|
||||
json.put("deviceName",new Random().nextInt(99) + "号设备");
|
||||
json.put("wifiName","橙空科技WiFi");
|
||||
json.put("wifiPassword","12321212");
|
||||
json.put("deviceEm","213123123123");
|
||||
json.put("deviceSn","sdfsd32423gdfgdf3");
|
||||
json.put("fwVer","fwVer-56565644");
|
||||
json.put("macAddress","2E:8H:00:KK:N7");
|
||||
json.put("deviceName",deviceName);
|
||||
json.put("wifiName",mWifiName);
|
||||
json.put("wifiPassword",mWifiPassword);
|
||||
json.put("macAddress",mBleDeviceEntity.getMacAddress());
|
||||
json.put("deviceNo", "01");
|
||||
// json.put("deviceEm","213123123123");
|
||||
// json.put("deviceSn","sdfsd32423gdfgdf3");
|
||||
// json.put("fwVer","fwVer-56565644");
|
||||
impl.addDevice(json, new IndexContract.CallBack<BaseBean>() {
|
||||
@Override
|
||||
public void success(BaseBean bean,int total) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.Manifest;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
@@ -27,11 +28,15 @@ import com.ckkj.water.network.BaseBean;
|
||||
import com.ckkj.water.permission.PermissionActivity;
|
||||
import com.ckkj.water.plan.entity.WaterPlanWeekEntity;
|
||||
import com.ckkj.water.utils.show.ToastUtil;
|
||||
import com.google.gson.Gson;
|
||||
import com.maning.mlkitscanner.scan.MNScanManager;
|
||||
import com.maning.mlkitscanner.scan.callback.act.MNScanCallback;
|
||||
import com.scwang.smart.refresh.footer.ClassicsFooter;
|
||||
import com.scwang.smart.refresh.layout.api.RefreshHeader;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -135,9 +140,41 @@ public class IndexFragment extends BaseFragment<IndexFragmentLayoutBinding, Inde
|
||||
public void onActivityResult(int resultCode, Intent data) {
|
||||
switch (resultCode) {
|
||||
case MNScanManager.RESULT_SUCCESS:
|
||||
try {
|
||||
ArrayList<String> results = data.getStringArrayListExtra(MNScanManager.INTENT_KEY_RESULT_SUCCESS);
|
||||
if(results != null){
|
||||
ToastUtil.showShort(mContext,results.get(0).toString());
|
||||
|
||||
if (results == null || results.isEmpty()) {
|
||||
ToastUtil.showShort(mContext, "二维码内容不能为空");
|
||||
return;
|
||||
}
|
||||
|
||||
String scanContent = results.get(0);
|
||||
if (TextUtils.isEmpty(scanContent)) {
|
||||
ToastUtil.showShort(mContext, "二维码内容不能为空");
|
||||
return;
|
||||
}
|
||||
|
||||
JSONObject json;
|
||||
try {
|
||||
json = new JSONObject(scanContent);
|
||||
} catch (JSONException e) {
|
||||
ToastUtil.showShort(mContext, "二维码格式错误");
|
||||
return;
|
||||
}
|
||||
WaterDeviceEntity entity = new WaterDeviceEntity();
|
||||
if(TextUtils.isEmpty(json.getString("mac"))
|
||||
|| TextUtils.isEmpty(json.getString("name"))){
|
||||
ToastUtil.showShort(mContext,"请扫描正确的二维码");
|
||||
return;
|
||||
}
|
||||
entity.setMacAddress(json.getString("mac"));
|
||||
entity.setDeviceName(json.getString("name"));
|
||||
if(entity != null){
|
||||
//这里拿到Mac地址,需要去服务器查询当前Mac的绑定状态
|
||||
mPresenter.bindDeviceStatus(entity);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
break;
|
||||
case MNScanManager.RESULT_FAIL:
|
||||
@@ -198,6 +235,21 @@ public class IndexFragment extends BaseFragment<IndexFragmentLayoutBinding, Inde
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询当前设备绑定状态
|
||||
* @param entity
|
||||
*/
|
||||
@Override
|
||||
public void queryDeviceBindStatusSuccess(WaterDeviceEntity entity) {
|
||||
switch (entity.getBindDeviceStatus()){
|
||||
case 302:
|
||||
//Todo 即这是一台没有在mqtt中注册的设备,App 需要通过蓝牙连接,并实现配网
|
||||
//Todo entity 中有Mac地址,和设备名称,根据代码中现有的扫描、连接、配网代码,实现功能
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
|
||||
@@ -45,6 +45,8 @@ public class WaterDeviceEntity implements Serializable {
|
||||
private String macAddress;
|
||||
private Object expirationTime;
|
||||
private List<DeviceListBean> deviceList;
|
||||
private int bindDeviceStatus;
|
||||
private String bindDeviceStatusName;
|
||||
|
||||
@NoArgsConstructor
|
||||
@Data
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.ckkj.water.index.mvp;
|
||||
import com.ckkj.water.base.IView;
|
||||
import com.ckkj.water.index.entity.WaterDeviceEntity;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -21,10 +23,14 @@ public class IndexContract {
|
||||
|
||||
//获取设备列表
|
||||
void getDeviceList(Map<String,String> map,CallBack callBack);
|
||||
//查询二维码当前设备绑定状态
|
||||
void queryDeviceBindStatus(JSONObject json, CallBack callBack);
|
||||
|
||||
}
|
||||
|
||||
public interface IndexView extends IView{
|
||||
void getDeviceListSuccess(List<WaterDeviceEntity> list, int total);
|
||||
//查询二维码当前设备绑定状态
|
||||
void queryDeviceBindStatusSuccess(WaterDeviceEntity entity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import com.ckkj.water.index.entity.WaterDeviceEntity;
|
||||
import com.ckkj.water.network.BaseBean;
|
||||
import com.ckkj.water.network.NetWorkManager;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -49,4 +51,44 @@ public class IndexImpl implements IndexContract.DataModel{
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询当前设备绑定状态
|
||||
* @param json
|
||||
* @param callBack
|
||||
*/
|
||||
@Override
|
||||
public void queryDeviceBindStatus(JSONObject json, IndexContract.CallBack callBack) {
|
||||
try {
|
||||
NetWorkManager.getRequest().bindDeviceStatus(NetWorkManager.getToken(),NetWorkManager.toRequestBody(json))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.unsubscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Observer<BaseBean<WaterDeviceEntity>>() {
|
||||
@Override
|
||||
public void onSubscribe(@NonNull Disposable d) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(@NonNull BaseBean<WaterDeviceEntity> bean) {
|
||||
if (bean.getCode() == 200) {
|
||||
callBack.success(bean.getData(),0);
|
||||
} else {
|
||||
callBack.fail(bean.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(@NonNull Throwable e) {
|
||||
callBack.fail(e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,10 @@ package com.ckkj.water.index.mvp;
|
||||
|
||||
import com.ckkj.water.base.BasePresenter;
|
||||
import com.ckkj.water.index.entity.WaterDeviceEntity;
|
||||
import com.ckkj.water.network.BaseBean;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -35,4 +39,35 @@ public class IndexPresenter extends BasePresenter<IndexContract.IndexView>{
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定设备状态
|
||||
* @param entity
|
||||
*/
|
||||
public void bindDeviceStatus(WaterDeviceEntity entity) {
|
||||
try {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("macAddress",entity.getMacAddress());
|
||||
mImpl.queryDeviceBindStatus(json, new IndexContract.CallBack<WaterDeviceEntity>() {
|
||||
@Override
|
||||
public void success(WaterDeviceEntity bean, int total) {
|
||||
entity.setBindDeviceStatus(bean.getBindDeviceStatus());
|
||||
entity.setBindDeviceStatusName(bean.getBindDeviceStatusName());
|
||||
if(IndexPresenter.this.getView() != null){
|
||||
IndexPresenter.this.getView().queryDeviceBindStatusSuccess(entity);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fail(String msg) {
|
||||
if(IndexPresenter.this.getView() != null){
|
||||
IndexPresenter.this.getView().showError(msg);
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (JSONException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +57,12 @@ public interface Request {
|
||||
//首页设备列表
|
||||
@GET("app/v1/deviceList")
|
||||
Observable<BaseBean<List<WaterDeviceEntity>>> getIndexDeviceList(@HeaderMap Map<String, String> headMap);
|
||||
|
||||
//查询设备编码,是否被绑定过,即当前设备状态
|
||||
@POST("app/v1/bindDeviceStatus")
|
||||
Observable<BaseBean<WaterDeviceEntity>> bindDeviceStatus(@HeaderMap Map<String, String> headMap, @Body RequestBody requestBody);
|
||||
|
||||
|
||||
//添加设备
|
||||
@POST("app/v1/addDevice")
|
||||
Observable<BaseBean> addDevice(@HeaderMap Map<String, String> headMap, @Body RequestBody requestBody);
|
||||
|
||||
Reference in New Issue
Block a user