830 lines
40 KiB
Java
830 lines
40 KiB
Java
package com.ifish.activity;
|
||
|
||
import android.app.Activity;
|
||
import android.content.Intent;
|
||
import android.os.Bundle;
|
||
import android.os.CountDownTimer;
|
||
import android.os.Handler;
|
||
import android.os.Message;
|
||
import android.util.Log;
|
||
import android.widget.ImageView;
|
||
|
||
import androidx.annotation.Nullable;
|
||
|
||
import com.bumptech.glide.Glide;
|
||
import com.ifish.basebean.BaseBean;
|
||
import com.ifish.basebean.ConnectMessageEvent;
|
||
import com.ifish.basebean.Device;
|
||
import com.ifish.basebean.EventBean;
|
||
import com.ifish.baseclass.BaseActivity;
|
||
import com.ifish.baseclass.BaseGradeActivity;
|
||
import com.ifish.tcp.BackFunctionCode7_11;
|
||
import com.ifish.tcp.ModelCodec;
|
||
import com.ifish.tcp.OrderDeviceConnectModel;
|
||
import com.ifish.tcp.OrderModel;
|
||
import com.ifish.utils.AnimationUtil;
|
||
import com.ifish.utils.AppRoute;
|
||
import com.ifish.utils.ByteUtil;
|
||
import com.ifish.utils.Commons;
|
||
import com.ifish.utils.Commons.LoginSPKey;
|
||
import com.ifish.utils.Commons.NetWork;
|
||
import com.ifish.utils.Commons.Text;
|
||
import com.ifish.utils.GlideApp;
|
||
import com.ifish.utils.HttpListener;
|
||
import com.ifish.utils.HttpManager;
|
||
import com.ifish.utils.L;
|
||
import com.ifish.utils.SPUtil;
|
||
import com.ifish.utils.ToastUtil;
|
||
|
||
import java.io.BufferedReader;
|
||
import java.io.IOException;
|
||
import java.io.InputStreamReader;
|
||
import java.net.DatagramPacket;
|
||
import java.net.DatagramSocket;
|
||
import java.net.InetAddress;
|
||
import java.net.InetSocketAddress;
|
||
|
||
import de.greenrobot.event.EventBus;
|
||
|
||
public class HotSpotConnentDeviceActivity extends BaseActivity {
|
||
private HttpManager hm = HttpManager.getInstance();
|
||
DatagramSocket ds;
|
||
private String device_mac;
|
||
private SPUtil sp;
|
||
private Activity act;
|
||
private ConnectDevice connectDevice;
|
||
@Override
|
||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||
super.onCreate(savedInstanceState);
|
||
act = this;
|
||
setContentView(R.layout.activity_hotconnect_gif);
|
||
connectDevice = new ConnectDevice(20000, 1000);
|
||
initTitle("尝试与设备建立连接");
|
||
ImageView iv_wifiset = findMyViewById(R.id.iv_wifiset);
|
||
GlideApp.with(this).load(R.drawable.hotconnectgif).into(iv_wifiset);
|
||
sp = SPUtil.getInstance(HotSpotConnentDeviceActivity.this);
|
||
// device_mac="5ccf7f99e929";
|
||
// doPostBindDevice();
|
||
// showProgressDialog();
|
||
isNext = 0;
|
||
String wifiname = getIntent().getStringExtra("wifiname");
|
||
String wifipwd = getIntent().getStringExtra("wifipwd");
|
||
|
||
L.i("jjia----hot--"+wifiname);
|
||
L.i("jjia----pwd--"+wifipwd);
|
||
new Thread(new ConnectDeviceWifi(wifiname, wifipwd)).start();
|
||
}
|
||
|
||
public int isNext = 0;
|
||
|
||
private class ConnectDeviceWifi implements Runnable {
|
||
private String ssidname;
|
||
private String ssidpwd;
|
||
|
||
public ConnectDeviceWifi(String ssidname, String ssidpwd) {
|
||
this.ssidname = ssidname;
|
||
this.ssidpwd = ssidpwd;
|
||
}
|
||
|
||
@Override
|
||
public void run() {
|
||
try {
|
||
while (isNext < 10) {
|
||
isNext++;
|
||
Thread.sleep(1000);
|
||
OrderDeviceConnectModel model = OrderModel.OrderDeviceConnectModel(ssidname, ssidpwd,false);//设置正式环境域名
|
||
byte[] bys = ModelCodec.enCode(model);//解析数据
|
||
InetAddress address = InetAddress.getByName("192.168.4.1");
|
||
DatagramPacket dp = new DatagramPacket(bys, bys.length, address, 333);
|
||
if (ds == null) {
|
||
ds = new DatagramSocket(null);
|
||
ds.setReuseAddress(true);
|
||
ds.bind(new InetSocketAddress(9954));
|
||
}
|
||
DeviceConnectUdpReceiveThread rt = new DeviceConnectUdpReceiveThread(ds);
|
||
Thread thread = new Thread(rt);
|
||
thread.start();
|
||
ds.send(dp);
|
||
}
|
||
if (isNext != 20) {
|
||
// ds.close();
|
||
HotSpotConnentDeviceActivity.this.runOnUiThread(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
// dismissProgressDialog();
|
||
ToastUtil.show(getApplicationContext(), "连接设备失败,请重试");
|
||
finish();
|
||
}
|
||
});
|
||
}
|
||
} catch (Exception e) {
|
||
Log.i("sssr", "444" + e.toString());
|
||
if (isNext != 20) {
|
||
// ds.close();
|
||
HotSpotConnentDeviceActivity.this.runOnUiThread(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
// dismissProgressDialog();
|
||
ToastUtil.show(getApplicationContext(), "连接设备失败,请重试");
|
||
finish();
|
||
}
|
||
});
|
||
}
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
public class DeviceConnectUdpReceiveThread implements Runnable {
|
||
private DatagramSocket ds;
|
||
|
||
public DeviceConnectUdpReceiveThread(DatagramSocket ds) {
|
||
this.ds = ds;
|
||
}
|
||
|
||
@Override
|
||
public void run() {
|
||
try {
|
||
Log.i("sssr", "111");
|
||
byte[] bys = new byte[105];
|
||
DatagramPacket dp = new DatagramPacket(bys, bys.length);
|
||
//接收数据
|
||
ds.receive(dp);
|
||
Log.i("sssr", "222");
|
||
bys = dp.getData();
|
||
int check_code = bys[1];
|
||
L.i("jjia---------bys--"+new String(bys));
|
||
L.i("jjia---------check_code--"+check_code);
|
||
int length = bys[14];
|
||
L.i("jjia---------length--"+length);
|
||
byte[] dataByte = new byte[length];
|
||
for (int i = 0; i < dataByte.length; i++) {
|
||
dataByte[i] = bys[i];
|
||
}
|
||
//System.out.println("接受长度("+length+"):"+ByteUtil.bytesToHexString(dataByte));
|
||
String ip = dp.getAddress().getHostAddress();
|
||
boolean b = packageData(dataByte, check_code, ip);
|
||
Log.i("jjia", b + "---");
|
||
if (b) {
|
||
isNext = 20;
|
||
boolean isSameName = false;
|
||
devicePosition = 0;
|
||
try {
|
||
if (Commons.DEVICE != null) {
|
||
if (Commons.DEVICE.size() != 0) {
|
||
for (int i = 0; i < Commons.DEVICE.size(); i++) {
|
||
if (device_mac.equals(Commons.DEVICE.get(i).getMacAddress())) {
|
||
deviceObj = Commons.DEVICE.get(i);
|
||
devicePosition = i;
|
||
isSameName = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
if (isSameName) {
|
||
HotSpotConnentDeviceActivity.this.runOnUiThread(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
new ReStartDeviceUtil(HotSpotConnentDeviceActivity.this, new ReStartDeviceUtil.ReStartCallBack() {
|
||
@Override
|
||
public void onSucess() {
|
||
// dismissProgressDialog();
|
||
finish();
|
||
toDevice();
|
||
}
|
||
|
||
@Override
|
||
public void onFail() {
|
||
// dismissProgressDialog();
|
||
finish();
|
||
toDevice();
|
||
}
|
||
}, ds).startDs();
|
||
}
|
||
});
|
||
} else {
|
||
// ds.close();
|
||
HotSpotConnentDeviceActivity.this.runOnUiThread(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
// ToastUtil.show(getApplicationContext(), "指令发送成功,请参照按钮下方的提示操作");
|
||
isContinue = true;
|
||
connectDevice.cancel();
|
||
connectDevice.start();
|
||
new ReStartDeviceUtil(HotSpotConnentDeviceActivity.this, new ReStartDeviceUtil.ReStartCallBack() {
|
||
@Override
|
||
public void onSucess() {
|
||
}
|
||
|
||
@Override
|
||
public void onFail() {
|
||
}
|
||
}, ds).startDs();
|
||
// findViewById(R.id.tv_one).setVisibility(View.VISIBLE);
|
||
// findViewById(R.id.tv_two).setVisibility(View.VISIBLE);
|
||
// findViewById(R.id.tv_three).setVisibility(View.VISIBLE);
|
||
}
|
||
});
|
||
}
|
||
}
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
int devicePosition = 0;
|
||
|
||
public void toDevice() {
|
||
Intent i = new Intent();
|
||
// try {
|
||
// Integer controlAmount = Commons.DEVICE.get(devicePosition).getControlAmount();
|
||
// Integer timerAmount = Commons.DEVICE.get(devicePosition).getTimerAmount();
|
||
// String type = Commons.DEVICE.get(devicePosition).type;
|
||
// if (Commons.FishKey.On.equals(Commons.DEVICE.get(devicePosition).isBlacklist)) {//黑名单
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, BlackListActivity.class);
|
||
// } else {
|
||
// if (Device.TYPE_1C.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThree1CControlActivity.class);
|
||
// } else if (Device.TYPE_AA.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFiveControlActivity.class);
|
||
// } else if (Device.TYPE_BD.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFiveBDControlActivity.class);
|
||
// } else if (Device.TYPE_2A.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainSix_2A_ControlActivity.class);
|
||
// } else if (Device.TYPE_2B.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFour_2B_ControlActivity.class);
|
||
// } else if (Device.TYPE_3A.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainHot_3A_ControlActivity.class);
|
||
// } else if (Device.TYPE_2F.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainSeven_2F_ControlActivity.class);
|
||
// } else if (Device.TYPE_3F.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFour_3F_ControlActivity.class);
|
||
// } else if (Device.TYPE_4F.equals(type) || Device.TYPE_5F.equals(type)){
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainSix_4F_ControlActivity.class);
|
||
// } else {
|
||
// if (controlAmount == null) {//如果控制器返回空 则默认给三控的页面
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThreeControlActivity.class);
|
||
// } else {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThreeControlActivity.class);
|
||
// switch (controlAmount) {
|
||
// case 0: {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainZeroControlActivity.class);
|
||
// break;
|
||
// }
|
||
// case 2: {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainTwoControlActivity.class);
|
||
// break;
|
||
// }
|
||
// case 3: {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThreeControlActivity.class);
|
||
// break;
|
||
// }
|
||
// case 4: {
|
||
// if (timerAmount == 6) { // 可丽爱
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFiveControlActivity.class);
|
||
// } else {//4控
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFourControlActivity.class);
|
||
// }
|
||
// break;
|
||
// }
|
||
// case 5: {
|
||
// if (6 == timerAmount) {//5控六定时器 询多页面
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainSixControlActivity.class);
|
||
// }
|
||
// break;
|
||
// }
|
||
// case 8: {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainEightControlActivity.class);
|
||
// break;
|
||
// }
|
||
// default: {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThreeControlActivity.class);
|
||
// break;
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
// } catch (Exception e) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThreeControlActivity.class);
|
||
// }
|
||
// i.putExtra("mac",device_mac);
|
||
// i.putExtra("title",title);
|
||
i.putExtra(BaseGradeActivity.BINDDEVICE, true);//绑定成功增加经验
|
||
AppRoute.newToStart(HotSpotConnentDeviceActivity.this,i,deviceObj);
|
||
AnimationUtil.startAnimation(HotSpotConnentDeviceActivity.this);
|
||
|
||
EventBean eventBean =new EventBean(EventBean.LINK_FINISH,0);
|
||
EventBus.getDefault().post(eventBean);
|
||
finish();
|
||
}
|
||
|
||
/**
|
||
* 封装数据
|
||
*
|
||
* @return
|
||
*/
|
||
public boolean packageData(byte[] bytes, int check_code, String ip) {
|
||
L.i("jjia----data"+new String(bytes));
|
||
L.i("jjia----check_code"+check_code);
|
||
L.i("jjia----ip"+ip);
|
||
Object object = ModelCodec.deCode(bytes);
|
||
if (object == null) {
|
||
return false;
|
||
} else if (object instanceof BackFunctionCode7_11) {
|
||
BackFunctionCode7_11 model = (BackFunctionCode7_11) object;
|
||
device_mac = ByteUtil.bytesToHexString(model.getSrc());
|
||
Log.i("jjia------hot--mac--", device_mac);
|
||
//配置域名回复
|
||
// if (check_code == 7) {
|
||
// EventBus.getDefault().post(object);//告诉Activity 设置tcp域名成功
|
||
|
||
// Map<String,Object> json = new HashMap<String, Object>();
|
||
// Map<String,String> resultMap = new HashMap<String, String>();
|
||
// resultMap.put(ByteUtil.bytesToHexString(model.getSrc())+"Set", ByteUtil.bytesToHexString(model.getSrc()));
|
||
// json.put("result", "setTcp");
|
||
// json.put("data", resultMap);
|
||
// }
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
|
||
private Device deviceObj;
|
||
private boolean deviceIsNull = false;///当服务器返回的设备控制器数和定时器数为null的时候 改变标记的值 不跳转进入主界面
|
||
private int deviceIsNullNum = 0;//循环请求次数
|
||
private CountDownTimer deviceIsNullTimer;
|
||
|
||
/**
|
||
* @param
|
||
* @Description: 给设备发送入网指令
|
||
* @author Jacky dada
|
||
* @date 2015-9-21 下午3:05:01
|
||
*/
|
||
private void doPostBindDevice() {
|
||
try {
|
||
hm.bindDevice(new HttpListener<BaseBean<Device>>() {
|
||
private int result;
|
||
|
||
@Override
|
||
public void success(BaseBean<Device> baseBean) {
|
||
result = baseBean.result;
|
||
if (result == NetWork.Success) {
|
||
deviceObj = baseBean.data;
|
||
|
||
L.i("jjia-----------xx-- "+deviceObj.toString());
|
||
deviceObj.setMacAddress(device_mac);
|
||
/**
|
||
//当服务器返回的设备控制器数和定时器数为null的时候 不跳转进入主界面
|
||
*/
|
||
if (deviceObj.getControlAmount() == null || deviceObj.getTimerAmount() == null) {
|
||
L.i("第一次获取空");
|
||
deviceIsNull = true;
|
||
deviceIsNullNum = 0;
|
||
startDeviceIsNullTimer();//启动倒计时
|
||
}
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void finish() {
|
||
doPostBindDeviceHandler.sendEmptyMessage(result);
|
||
}
|
||
|
||
@Override
|
||
public void error(Exception e, String msg) {
|
||
result = NetWork.ERROR;
|
||
}
|
||
}, Commons.USER.getUserId(), device_mac);
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
public void startDeviceIsNullTimer() {
|
||
if (deviceIsNullTimer == null) {
|
||
deviceIsNullTimer = new CountDownTimer(2000, 1000) {
|
||
@Override
|
||
public void onTick(long arg0) {
|
||
}
|
||
|
||
@Override
|
||
public void onFinish() {
|
||
L.i("重新请求");
|
||
//拿到设备id 去服务器查询 看看是否为空
|
||
try {
|
||
hm.getSingleDeviceInf(new HttpListener<BaseBean<Device>>() {
|
||
private int result;
|
||
|
||
@Override
|
||
public void success(BaseBean<Device> baseBean) {
|
||
result = baseBean.result;
|
||
deviceObj = baseBean.data;
|
||
}
|
||
|
||
@Override
|
||
public void finish() {
|
||
deviceIsNullHandler.sendEmptyMessage(result);
|
||
}
|
||
|
||
@Override
|
||
public void error(Exception e, String msg) {
|
||
result = NetWork.ERROR;
|
||
}
|
||
}, deviceObj.getDeviceId(), Commons.USER.getUserId());
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
};
|
||
}
|
||
deviceIsNullTimer.start();
|
||
}
|
||
|
||
Handler deviceIsNullHandler = new Handler() {
|
||
|
||
|
||
public void handleMessage(Message msg) {
|
||
L.i("请求结束");
|
||
switch (msg.what) {
|
||
case NetWork.Success:
|
||
if (deviceObj.getControlAmount() == null || deviceObj.getTimerAmount() == null) {
|
||
deviceIsNullNum = deviceIsNullNum + 1;
|
||
if (deviceIsNullNum < 3) {
|
||
startDeviceIsNullTimer();
|
||
} else {
|
||
ToastUtil.show(HotSpotConnentDeviceActivity.this, "绑定设备失败 请重试");
|
||
}
|
||
return;
|
||
}
|
||
|
||
boolean isSameName = false;
|
||
try {
|
||
if (Commons.DEVICE != null) {
|
||
if (Commons.DEVICE.size() != 0) {
|
||
for (int i = 0; i < Commons.DEVICE.size(); i++) {
|
||
if (device_mac.equals(Commons.DEVICE.get(i).getMacAddress())) {
|
||
sp.putInt(LoginSPKey.Position, i);
|
||
Commons.DevicePosition = i;
|
||
isSameName = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} catch (Exception e) {
|
||
}
|
||
if (!isSameName) {
|
||
Commons.DEVICE.add(deviceObj);
|
||
try {
|
||
sp.putInt(LoginSPKey.Position, Commons.DEVICE.size() - 1);
|
||
Commons.DevicePosition = Commons.DEVICE.size() - 1;
|
||
} catch (Exception e) {
|
||
}
|
||
}
|
||
Commons.IS_EventBus = true;
|
||
Commons.HAVE_DEVICE = true;//改变判断是否绑定的变量
|
||
Commons.IS_CAMERA = false;
|
||
sp.putBoolean(LoginSPKey.IS_CAMERA, Commons.IS_CAMERA);
|
||
Intent i = new Intent();
|
||
// try {
|
||
// Integer controlAmount = Commons.DEVICE.get(Commons.DevicePosition).getControlAmount();
|
||
// Integer timerAmount = Commons.DEVICE.get(Commons.DevicePosition).getTimerAmount();
|
||
// String type = Commons.DEVICE.get(Commons.DevicePosition).type;
|
||
// if (Commons.FishKey.On.equals(Commons.DEVICE.get(Commons.DevicePosition).isBlacklist)) {//黑名单
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, BlackListActivity.class);
|
||
// } else {
|
||
// if (Device.TYPE_1C.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThree1CControlActivity.class);
|
||
// } else if (Device.TYPE_AA.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFiveControlActivity.class);
|
||
// } else if (Device.TYPE_BD.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFiveBDControlActivity.class);
|
||
// } else if (Device.TYPE_2A.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainSix_2A_ControlActivity.class);
|
||
// } else if (Device.TYPE_2B.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFour_2B_ControlActivity.class);
|
||
// } else if (Device.TYPE_3A.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainHot_3A_ControlActivity.class);
|
||
// } else if (Device.TYPE_2F.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainSeven_2F_ControlActivity.class);
|
||
// } else if (Device.TYPE_3F.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFour_3F_ControlActivity.class);
|
||
// } else if (Device.TYPE_4F.equals(type) || Device.TYPE_5F.equals(type)){
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainSix_4F_ControlActivity.class);
|
||
// } else {
|
||
// if (controlAmount == null) {//如果控制器返回空 则默认给三控的页面
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThreeControlActivity.class);
|
||
// } else {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThreeControlActivity.class);
|
||
// switch (controlAmount) {
|
||
// case 0: {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainZeroControlActivity.class);
|
||
// break;
|
||
// }
|
||
// case 2: {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainTwoControlActivity.class);
|
||
// break;
|
||
// }
|
||
// case 3: {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThreeControlActivity.class);
|
||
// break;
|
||
// }
|
||
// case 4: {
|
||
// if (timerAmount == 6) { // 可丽爱
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFiveControlActivity.class);
|
||
// } else {//4控
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFourControlActivity.class);
|
||
// }
|
||
// break;
|
||
// }
|
||
// case 5: {
|
||
// if (6 == timerAmount) {//5控六定时器 询多页面
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainSixControlActivity.class);
|
||
// }
|
||
// break;
|
||
// }
|
||
// case 8: {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainEightControlActivity.class);
|
||
// break;
|
||
// }
|
||
// default: {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThreeControlActivity.class);
|
||
// break;
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
//
|
||
//
|
||
// }
|
||
// } catch (Exception e) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThreeControlActivity.class);
|
||
// }
|
||
|
||
// i.putExtra("title",title);
|
||
// i.putExtra("mac",device_mac);
|
||
i.putExtra(BaseGradeActivity.BINDDEVICE, true);//绑定成功增加经验
|
||
|
||
AppRoute.newToStart(HotSpotConnentDeviceActivity.this,i,deviceObj);
|
||
ToastUtil.show(HotSpotConnentDeviceActivity.this, "绑定设备成功");
|
||
EventBean eventBean =new EventBean(EventBean.LINK_FINISH,0);
|
||
EventBus.getDefault().post(eventBean);
|
||
AnimationUtil.startAnimation(HotSpotConnentDeviceActivity.this);
|
||
finish();
|
||
L.i("===================绑定设备成功=" + device_mac);
|
||
break;
|
||
case NetWork.ERROR:
|
||
ToastUtil.show(HotSpotConnentDeviceActivity.this, Text.ERROR);
|
||
break;
|
||
case NetWork.Fail:
|
||
ToastUtil.show(HotSpotConnentDeviceActivity.this, Text.Repat);
|
||
break;
|
||
default:
|
||
ToastUtil.show(HotSpotConnentDeviceActivity.this, Text.ServerException);
|
||
break;
|
||
}
|
||
}
|
||
};
|
||
Handler doPostBindDeviceHandler = new Handler() {
|
||
public void handleMessage(Message msg) {
|
||
// dismissProgressDialog();
|
||
switch (msg.what) {
|
||
/**
|
||
* 1:绑定成功
|
||
0:绑定失败
|
||
-4:请求验证失败,请重新登陆
|
||
-5:请求被舍弃,未执行
|
||
*/
|
||
case NetWork.Success:
|
||
if (deviceIsNull) {//当服务器返回的设备控制器数和定时器数为null的时候 不跳转进入主界面
|
||
return;
|
||
}
|
||
Commons.DEVICE.add(deviceObj);
|
||
try {
|
||
sp.putInt(LoginSPKey.Position, Commons.DEVICE.size() - 1);
|
||
Commons.DevicePosition = Commons.DEVICE.size() - 1;
|
||
} catch (Exception e) {
|
||
}
|
||
ToastUtil.show(HotSpotConnentDeviceActivity.this, "设备序号" + Commons.DevicePosition + "---" + Commons.DEVICE.size());
|
||
Commons.IS_EventBus = true;
|
||
Commons.HAVE_DEVICE = true;//改变判断是否绑定的变量
|
||
Commons.IS_CAMERA = false;
|
||
sp.putBoolean(LoginSPKey.IS_CAMERA, Commons.IS_CAMERA);
|
||
Intent i = new Intent();
|
||
// i.putExtra("mac",device_mac);
|
||
// try {
|
||
// Integer controlAmount = Commons.DEVICE.get(Commons.DevicePosition).getControlAmount();
|
||
// Integer timerAmount = Commons.DEVICE.get(Commons.DevicePosition).getTimerAmount();
|
||
// String type = Commons.DEVICE.get(Commons.DevicePosition).type;
|
||
// if (Commons.FishKey.On.equals(Commons.DEVICE.get(Commons.DevicePosition).isBlacklist)) {//黑名单
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, BlackListActivity.class);
|
||
// } else {
|
||
// if (Device.TYPE_1C.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThree1CControlActivity.class);
|
||
// } else if (Device.TYPE_AA.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFiveControlActivity.class);
|
||
// } else if (Device.TYPE_BD.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFiveBDControlActivity.class);
|
||
// } else if (Device.TYPE_2A.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainSix_2A_ControlActivity.class);
|
||
// } else if (Device.TYPE_2B.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFour_2B_ControlActivity.class);
|
||
// } else if (Device.TYPE_3A.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainHot_3A_ControlActivity.class);
|
||
// } else if (Device.TYPE_2F.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainSeven_2F_ControlActivity.class);
|
||
// } else if (Device.TYPE_3F.equals(type)) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFour_3F_ControlActivity.class);
|
||
// } else if (Device.TYPE_4F.equals(type) || Device.TYPE_5F.equals(type)){
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainSix_4F_ControlActivity.class);
|
||
// } else {
|
||
// if (controlAmount == null) {//如果控制器返回空 则默认给三控的页面
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThreeControlActivity.class);
|
||
// } else {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThreeControlActivity.class);
|
||
// switch (controlAmount) {
|
||
// case 0: {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainZeroControlActivity.class);
|
||
// break;
|
||
// }
|
||
// case 2: {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainTwoControlActivity.class);
|
||
// break;
|
||
// }
|
||
// case 3: {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThreeControlActivity.class);
|
||
// break;
|
||
// }
|
||
// case 4: {
|
||
// if (timerAmount == 6) { // 可丽爱
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFiveControlActivity.class);
|
||
// } else {//4控
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainFourControlActivity.class);
|
||
// }
|
||
// break;
|
||
// }
|
||
// case 5: {
|
||
// if (6 == timerAmount) {//5控六定时器 询多页面
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainSixControlActivity.class);
|
||
// }
|
||
// break;
|
||
// }
|
||
// case 8: {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainEightControlActivity.class);
|
||
// break;
|
||
// }
|
||
// default: {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThreeControlActivity.class);
|
||
// break;
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
//
|
||
//
|
||
// }
|
||
// } catch (Exception e) {
|
||
// i.setClass(HotSpotConnentDeviceActivity.this, MainThreeControlActivity.class);
|
||
// }
|
||
// i.putExtra("title",title);
|
||
// i.putExtra("mac",device_mac);
|
||
i.putExtra(BaseGradeActivity.BINDDEVICE, true);//绑定成功增加经验
|
||
AppRoute.newToStart(HotSpotConnentDeviceActivity.this,i,deviceObj);
|
||
ToastUtil.show(HotSpotConnentDeviceActivity.this, "绑定设备成功");
|
||
EventBean eventBean =new EventBean(EventBean.LINK_FINISH,0);
|
||
EventBus.getDefault().post(eventBean);
|
||
AnimationUtil.startAnimation(HotSpotConnentDeviceActivity.this);
|
||
EventBus.getDefault().post(new ConnectMessageEvent(1, "test"));
|
||
finish();
|
||
L.i("===================绑定设备成功=" + device_mac);
|
||
break;
|
||
case NetWork.Fail:
|
||
ToastUtil.show(HotSpotConnentDeviceActivity.this, "绑定设备失败 请重试");
|
||
EventBus.getDefault().post(new ConnectMessageEvent(-1, "fail"));
|
||
finish();
|
||
break;
|
||
case NetWork.DEVICETYPEERROR:
|
||
ToastUtil.show(HotSpotConnentDeviceActivity.this, "设备类型不匹配,请重新选择入口绑定");
|
||
finish();
|
||
break;
|
||
case NetWork.ReLogin:
|
||
ToastUtil.show(getApplicationContext(), "请求验证失败 请重新登陆");
|
||
Commons.clean();
|
||
startActivity(new Intent(HotSpotConnentDeviceActivity.this, LoadingActivity.class));
|
||
finish();
|
||
break;
|
||
case NetWork.ERROR:
|
||
ToastUtil.show(getApplicationContext(), Text.ERROR);
|
||
finish();
|
||
break;
|
||
case NetWork.NotImplemented:
|
||
ToastUtil.show(getApplicationContext(), Text.Unknown);
|
||
finish();
|
||
break;
|
||
default:
|
||
ToastUtil.show(getApplicationContext(), Text.ServerException);
|
||
finish();
|
||
break;
|
||
}
|
||
}
|
||
};
|
||
|
||
private boolean isContinue = true;
|
||
|
||
public class ConnectDevice extends CountDownTimer {
|
||
|
||
public ConnectDevice(long millisInFuture, long countDownInterval) {
|
||
super(millisInFuture, countDownInterval);
|
||
}
|
||
|
||
@Override
|
||
public void onTick(long millisUntilFinished) {
|
||
long sum = millisUntilFinished / 1000 % 2;
|
||
if (sum == 0) {
|
||
if (!act.isFinishing() && isContinue) {
|
||
isNetWorkAvailable("www.baidu.com", new Comparable<Boolean>() {
|
||
|
||
@Override
|
||
public int compareTo(Boolean available) {
|
||
if (available) {
|
||
// TODO 设备访问Internet正常
|
||
isContinue = false;
|
||
connectDevice.cancel();
|
||
doPostBindDevice();
|
||
}
|
||
return 0;
|
||
}
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onFinish() {
|
||
if (isContinue) {
|
||
// dismissProgressDialog();
|
||
}
|
||
}
|
||
}
|
||
|
||
@Override
|
||
protected void onDestroy() {
|
||
super.onDestroy();
|
||
if (ds != null) {
|
||
ds.close();
|
||
}
|
||
connectDevice.cancel();
|
||
}
|
||
|
||
public static void isNetWorkAvailable(final String address, final Comparable<Boolean> callback) {
|
||
final Handler handler = new Handler() {
|
||
|
||
@Override
|
||
public void handleMessage(Message msg) {
|
||
super.handleMessage(msg);
|
||
if (callback != null) {
|
||
callback.compareTo(msg.arg1 == 0);
|
||
}
|
||
}
|
||
|
||
};
|
||
new Thread(new Runnable() {
|
||
|
||
@Override
|
||
public void run() {
|
||
Runtime runtime = Runtime.getRuntime();
|
||
Message msg = new Message();
|
||
try {
|
||
Process pingProcess = runtime.exec("/system/bin/ping -c 1 " + address);
|
||
InputStreamReader isr = new InputStreamReader(pingProcess.getInputStream());
|
||
BufferedReader buf = new BufferedReader(isr);
|
||
if (buf.readLine() == null) {
|
||
msg.arg1 = -1;
|
||
} else {
|
||
msg.arg1 = 0;
|
||
}
|
||
buf.close();
|
||
isr.close();
|
||
} catch (Exception e) {
|
||
msg.arg1 = -1;
|
||
e.printStackTrace();
|
||
} finally {
|
||
runtime.gc();
|
||
handler.sendMessage(msg);
|
||
}
|
||
}
|
||
|
||
}).start();
|
||
}
|
||
|
||
|
||
}
|