我是靠谱客的博主 落后西牛,最近开发中收集的这篇文章主要介绍android EasyLink给wifi模块配网,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

相关文章 博客 http://blog.csdn.net/u010924834/article/details/49491349

本文使用的是庆科wifi模块

http://developer.mico.io/downloads 庆科的开发者地址 有配网的sdk demo下载 

下载下来的demo 有可能gradle不过 可以找他们工程师 android studio项目 ,看别人项目 细心 耐心 下面是主要配网代码


package com.example.administrator.easylinkdemo;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import io.fogcloud.easylink.api.EasyLink;
import io.fogcloud.easylink.helper.EasyLinkCallBack;
import io.fogcloud.easylink.helper.EasyLinkParams;
import io.fog.fog2sdk.MiCODevice;
import io.fogcloud.fog_mdns.helper.SearchDeviceCallBack;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";
    private EasyLink elink;
    private MiCODevice mMiCODevice;
    private EditText ex_password;
    private Spinner mSpinner;
    private List<String> mList;
    private ArrayAdapter<String> mAdapter;
    private WifiUtils mWifiUtils;
    private String ssId;
    private LayoutInflater mLayoutInflater;
    private LinearLayout lin_showmdns;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lin_showmdns = (LinearLayout) findViewById(R.id.showmdns);
        mLayoutInflater = LayoutInflater.from(this);
        mWifiUtils = new WifiUtils(this);
        elink = new EasyLink(this);
        mMiCODevice = new MiCODevice(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 void initWidget(){
        mList = mWifiUtils.getScanWifiResult();
        mList = mWifiUtils.getNewList(mList);//去重复
        //适配器
        mAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mList);
        //设置样式
        mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        mSpinner.setAdapter(mAdapter);
        //刷新适配器
        mAdapter.notifyDataSetChanged();

        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {
                ssId = mList.get(pos);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.start:
                configureNetwork();
                searchDevice();
                break;
            case R.id.end:
                stopConfig();
                stopSearchDevice();
                break;
        }
    }

    //配网
    public void configureNetwork(){
        EasyLinkParams easylinkPara = new EasyLinkParams();
        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, new EasyLinkCallBack() {
            @Override
            public void onSuccess(int code, String message) {
                Message msg = new Message();
                msg.what = 0x00001;
                msg.arg1 = code;
                msg.obj = message;
                myHandle.sendMessage(msg);
            }
            @Override
            public void onFailure(int code, String message) {
                Message msg = new Message();
                msg.what = 0x00002;
                msg.arg1 = code;
                msg.obj = message;
                myHandle.sendMessage(msg);
            }
        });
    }

    //查找设备 如果查到设备 代表配网成功
    private void searchDevice(){
        String serviceName = "_easylink._tcp.local.";
        mMiCODevice.startSearchDevices(serviceName, new SearchDeviceCallBack() {
            @Override
            public void onSuccess(int code, String message) {
                super.onSuccess(code, message);
                Log.e(TAG,"=========startSearchDevices onSuccess");
            }

            @Override
            public void onFailure(int code, String message) {
                super.onFailure(code, message);
                Log.e(TAG,"=========startSearchDevices onFailure");
            }

            @Override
            public void onDevicesFind(int code, JSONArray deviceStatus) {
                super.onDevicesFind(code, deviceStatus);
                Log.e(TAG,"=========startSearchDevices onDevicesFind"+deviceStatus.toString());

                Message msg = new Message();
                msg.what = 0x00005;
                msg.obj = deviceStatus.toString();
                myHandle.sendMessage(msg);
            }
        });
    }

    //停止查找设备
    public void stopSearchDevice(){
        mMiCODevice.stopSearchDevices(new SearchDeviceCallBack() {
            @Override
            public void onSuccess(int code, String message) {
                super.onSuccess(code, message);
                Log.e(TAG,"=========stopSearchDevices onSuccess");
            }

            @Override
            public void onFailure(int code, String message) {
                super.onFailure(code, message);
                Log.e(TAG,"=========stopSearchDevices onFailure");
            }

            @Override
            public void onDevicesFind(int code, JSONArray deviceStatus) {
                super.onDevicesFind(code, deviceStatus);
                Log.e(TAG,"=========stopSearchDevices onDevicesFind" +deviceStatus.toString());
            }
        });
    }

    //停止配网
    public void stopConfig(){
        elink.stopEasyLink(new EasyLinkCallBack() {
            @Override
            public void onSuccess(int code, String message) {
                Message msg = new Message();
                msg.what = 0x00003;
                msg.arg1 = code;
                msg.obj = message;
                myHandle.sendMessage(msg);
            }
            @Override
            public void onFailure(int code, String message) {
                Message msg = new Message();
                msg.what = 0x00004;
                msg.arg1 = code;
                msg.obj = message;
                myHandle.sendMessage(msg);
            }
        });
    }

    private MyHandle myHandle = new MyHandle();

    private class MyHandle extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String codeString = codeDescribe(msg.arg1);
            switch (msg.what){
                case 0x00001:
                    //Toast.makeText(MainActivity.this,codeString,Toast.LENGTH_LONG).show();
                    break;
                case 0x00002:
                    Toast.makeText(MainActivity.this,codeString,Toast.LENGTH_LONG).show();
                    break;
                case 0x00003:
                    Toast.makeText(MainActivity.this,codeString,Toast.LENGTH_LONG).show();
                    break;
                case 0x00004:
                    Toast.makeText(MainActivity.this,codeString,Toast.LENGTH_LONG).show();
                    break;
                case 0x00005:
                    updateDeviceList(msg.obj.toString());
                    break;
            }
        }
    }

    private void updateDeviceList(String message) {
        lin_showmdns.removeAllViews();
        if (null != mLayoutInflater) {
            try {
                JSONArray jsonArray = new JSONArray(message);
                if(jsonArray.length()>0)
                    Toast.makeText(MainActivity.this,"查找到设备配网成功",Toast.LENGTH_LONG).show();
                JSONObject temp;
                for (int i = 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);

                    final String devinfo = temp.toString();
                    final String devip = temp.getString("IP");
                    final String devmac = temp.getString("MAC");

                    mdnsname.setText(temp.getString("Name"));
                    mdnsmac.setText("MAC:" + temp.getString("MAC"));
                    mdnsip.setText("IP:" + temp.getString("IP"));

                    mdnsinfo.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(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();
            }
        }
    }

    public String codeDescribe(int arg1){
        Log.e(TAG,"=========="+arg1);
        switch (arg1){
            case 0:
                return "各种成功";
            case 4000:
                return "stopeasylink成功";
            case 4003:
                return "EasyLink正在工作中";
            case 4004:
                return "EasyLink已经关闭";
            case 4006:
                return "异常";
        }
        return ""+arg1;
    }

给智能家居wifi模块配网 总结 

1,wifi模块有个EasyLink按钮 当按钮按下只有绿灯亮时 此时进入配网模式 android端可以通过startEasyList方法发送SSID和PASSWORD实现配网

2,绿灯配网模式设定为60s 当规定时间一过 就不能在进行配网 需重新按下EasyLink按钮

3,配网模式配置成功后通过startSearchDevices方法查询设备 显示在android端 如果查到设备 证明配网成功

4,长按EasyLink按钮 可以清楚已经配置好的网络

5,发送的SSID和PASSWORD 一定是手机当前连接的网络 才能配网成功?(待测试)

6,当wifi模块已经配置好A网络的情况,要更换配置B网络 需先长按EasyLink清空配网

7,配置好网络 路由器会给wifi模块分配动态IP地址 PC机可以通过ping ipAddress的方式查看

8,不同路由器给wifi模块分配的IP地址不一样

最后

以上就是落后西牛为你收集整理的android EasyLink给wifi模块配网的全部内容,希望文章能够帮你解决android EasyLink给wifi模块配网所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部