packagecom.example.administrator.easylinkdemo;
importandroid.app.AlertDialog;
importandroid.app.Dialog;
importandroid.content.DialogInterface;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.support.v7.app.AppCompatActivity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.widget.AdapterView;
importandroid.widget.ArrayAdapter;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.ImageView;
importandroid.widget.LinearLayout;
importandroid.widget.Spinner;
importandroid.widget.TextView;
importandroid.widget.Toast;
importorg.json.JSONArray;
importorg.json.JSONException;
importorg.json.JSONObject;
importjava.util.List;
importio.fogcloud.easylink.api.EasyLink;
importio.fogcloud.easylink.helper.EasyLinkCallBack;
importio.fogcloud.easylink.helper.EasyLinkParams;
importio.fog.fog2sdk.MiCODevice;
importio.fogcloud.fog_mdns.helper.SearchDeviceCallBack;
public classMainActivity extendsAppCompatActivity implementsView.OnClickListener {
private static finalString TAG= "MainActivity";
privateEasyLink elink;
privateMiCODevice mMiCODevice;
privateEditText ex_password;
privateSpinner mSpinner;
privateList mList;
privateArrayAdapter mAdapter;
privateWifiUtils mWifiUtils;
privateString ssId;
privateLayoutInflater mLayoutInflater;
privateLinearLayout lin_showmdns;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lin_showmdns= (LinearLayout) findViewById(R.id.showmdns);
mLayoutInflater= LayoutInflater.from(this);
mWifiUtils= newWifiUtils(this);
elink= newEasyLink(this);
mMiCODevice= newMiCODevice(this);
Button btn_start = (Button) findViewById(R.id.start);
Button btn_end = (Button) findViewById(R.id.end);
btn_start.setOnClickListener(MainActivity.this);
btn_end.setOnClickListener(MainActivity.this);
ex_password= (EditText) findViewById(R.id.password);
mSpinner= (Spinner) findViewById(R.id.spinner);
initWidget();
}
private voidinitWidget(){
mList= mWifiUtils.getScanWifiResult();
mList= mWifiUtils.getNewList(mList);//去重复
//适配器
mAd
4000
apter= newArrayAdapter(this, android.R.layout.simple_spinner_item, mList);
//设置样式
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//加载适配器
mSpinner.setAdapter(mAdapter);
//刷新适配器
mAdapter.notifyDataSetChanged();
mSpinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {
@Override
public voidonItemSelected(AdapterView> parent, View view,
intpos, longid) {
ssId= mList.get(pos);
}
@Override
public voidonNothingSelected(AdapterView> parent) {
// Another interface callback
}
});
}
@Override
public voidonClick(View v) {
switch(v.getId()){
caseR.id.start:
configureNetwork();
searchDevice();
break;
caseR.id.end:
stopConfig();
stopSearchDevice();
break;
}
}
//配网
public voidconfigureNetwork(){
EasyLinkParams easylinkPara = newEasyLinkParams();
easylinkPara.ssid= ssId;
easylinkPara.password= ex_password.getText().toString().trim();
easylinkPara.runSecond= 20000;
easylinkPara.sleeptime= 50;
Log.e(TAG,"=========ssId="+ssId+"password="+ easylinkPara.password);
elink.startEasyLink(easylinkPara, newEasyLinkCallBack() {
@Override
public voidonSuccess(intcode, String message) {
Message msg = newMessage();
msg.what= 0x00001;
msg.arg1= code;
msg.obj= message;
myHandle.sendMessage(msg);
}
@Override
public voidonFailure(intcode, String message) {
Message msg = newMessage();
msg.what= 0x00002;
msg.arg1= code;
msg.obj= message;
myHandle.sendMessage(msg);
}
});
}
//查找设备 如果查到设备 代表配网成功
private voidsearchDevice(){
String serviceName = "_easylink._tcp.local.";
mMiCODevice.startSearchDevices(serviceName, newSearchDeviceCallBack() {
@Override
public voidonSuccess(intcode, String message) {
super.onSuccess(code, message);
Log.e(TAG,"=========startSearchDevices onSuccess");
}
@Override
public voidonFailure(intcode, String message) {
super.onFailure(code, message);
Log.e(TAG,"=========startSearchDevices onFailure");
}
@Override
public voidonDevicesFind(intcode, JSONArray deviceStatus) {
super.onDevicesFind(code, deviceStatus);
Log.e(TAG,"=========startSearchDevices onDevicesFind"+deviceStatus.toString());
Message msg = newMessage();
msg.what= 0x00005;
msg.obj= deviceStatus.toString();
myHandle.sendMessage(msg);
}
});
}
//停止查找设备
public voidstopSearchDevice(){
mMiCODevice.stopSearchDevices(newSearchDeviceCallBack() {
@Override
public voidonSuccess(intcode, String message) {
super.onSuccess(code, message);
Log.e(TAG,"=========stopSearchDevices onSuccess");
}
@Override
public voidonFailure(intcode, String message) {
super.onFailure(code, message);
Log.e(TAG,"=========stopSearchDevices onFailure");
}
@Override
public voidonDevicesFind(intcode, JSONArray deviceStatus) {
super.onDevicesFind(code, deviceStatus);
Log.e(TAG,"=========stopSearchDevices onDevicesFind"+deviceStatus.toString());
}
});
}
//停止配网
public voidstopConfig(){
elink.stopEasyLink(newEasyLinkCallBack() {
@Override
public voidonSuccess(intcode, String message) {
Message msg = newMessage();
msg.what= 0x00003;
msg.arg1= code;
msg.obj= message;
myHandle.sendMessage(msg);
}
@Override
public voidonFailure(intcode, String message) {
Message msg = newMessage();
msg.what= 0x00004;
msg.arg1= code;
msg.obj= message;
myHandle.sendMessage(msg);
}
});
}
privateMyHandle myHandle= newMyHandle();
private classMyHandle extendsHandler{
@Override
public voidhandleMessage(Message msg) {
super.handleMessage(msg);
String codeString = codeDescribe(msg.arg1);
switch(msg.what){
case0x00001:
//Toast.makeText(MainActivity.this,codeString,Toast.LENGTH_LONG).show();
break;
case0x00002:
Toast.makeText(MainActivity.this,codeString,Toast.LENGTH_LONG).show();
break;
case0x00003:
Toast.makeText(MainActivity.this,codeString,Toast.LENGTH_LONG).show();
break;
case0x00004:
Toast.makeText(MainActivity.this,codeString,Toast.LENGTH_LONG).show();
break;
case0x00005:
updateDeviceList(msg.obj.toString());
break;
}
}
}
private voidupdateDeviceList(String message) {
lin_showmdns.removeAllViews();
if(null!= mLayoutInflate
cadc
r) {
try{
JSONArray jsonArray = newJSONArray(message);
if(jsonArray.length()>0)
Toast.makeText(MainActivity.this,"查找到设备配网成功",Toast.LENGTH_LONG).show();
JSONObject temp;
for(inti = 0; i < jsonArray.length(); i++) {
View viewOne1 = mLayoutInflater.inflate(R.layout.deviceitem, null);
temp = (JSONObject) jsonArray.get(i);
//更新页面内容
TextView mdnsname = (TextView) viewOne1.findViewById(R.id.mdnsname);
TextView mdnsmac = (TextView) viewOne1.findViewById(R.id.mdnsmac);
TextView mdnsip = (TextView) viewOne1.findViewById(R.id.mdnsip);
ImageView mdnsbtn = (ImageView) viewOne1.findViewById(R.id.mdnsbtn);
LinearLayout mdnsinfo = (LinearLayout) viewOne1.findViewById(R.id.mdnsinfo);
finalString devinfo = temp.toString();
finalString devip = temp.getString("IP");
finalString devmac = temp.getString("MAC");
mdnsname.setText(temp.getString("Name"));
mdnsmac.setText("MAC:"+ temp.getString("MAC"));
mdnsip.setText("IP:"+ temp.getString("IP"));
mdnsinfo.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View v) {
Toast.makeText(MainActivity.this,"详细信息",Toast.LENGTH_LONG).show();
}
});
lin_showmdns.addView(viewOne1, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
}
} catch(JSONException e) {
e.printStackTrace();
}
}
}
publicString codeDescribe(intarg1){
Log.e(TAG,"=========="+arg1);
switch(arg1){
case0:
return"各种成功";
case4000:
return"stopeasylink成功";
case4003:
return"EasyLink正在工作中";
case4004:
return"EasyLink已经关闭";
case4006:
return"异常";
}
return""+arg1;
}