android开发中wifi模块移植

最近刚上一个项目,是关于在机智平台上使用esp8266芯片进行wifi通信控制,其实我对esp8266这块不是很熟悉,因为以前做的事zigbee的,现在使用wifi对智能灯进行控制。我在移植中碰到的问题做一简单总结。

首先,将esp8266的wifi模块demo全部文件(espressif文件夹所有)的复制到机智云的app中,根据demo的Activity的实现在AirlinkActivity.java中写一个异步任务的内部类,重写doInBackground方法

@Override
protected Void doInBackground(String... params) {

    String strSsid = params[0];
    String strBssid = params[1];
    String strPassword = params[2];
    String isSsidHiddenStr = params[3];
    boolean isSsidHidden = false;
    if(isSsidHiddenStr.equals("YES"))
    {
        isSsidHidden = true;
    }

    Log.i(TAG, "strSsid = :" + strSsid + ", strBssid = :" + strBssid
            + ", strPassword = :" + strPassword + ", isSsidHiddenStr = :" + isSsidHiddenStr);

    new EsptouchTask(strSsid, strBssid, strPassword, isSsidHidden, AirlinkActivity.this).executeForResult();
    return null;
}
new EsptouchTask(strSsid, strBssid, strPassword, isSsidHidden, AirlinkActivity.this).executeForResult();一定调用executeForResult()方法。
 
 
完整demo_Activity
package com.espressif.iot.esptouch.demo_activity;


import com.demo.smarthome.R;
import com.espressif.iot.esptouch.EsptouchTask;
import com.espressif.iot.esptouch.IEsptouchResult;
import com.espressif.iot.esptouch.IEsptouchTask;
import com.espressif.iot.esptouch.task.__IEsptouchTask;




import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;


public class EsptouchDemoActivity extends Activity implements OnClickListener {


	private static final String TAG = "EsptouchDemoActivity";


	private TextView mTvApSsid;


	private EditText mEdtApPassword;


	private Button mBtnConfirm;
	
	private Switch mSwitchIsSsidHidden;


	private EspWifiAdminSimple mWifiAdmin;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.esptouch_demo_activity);
		TextView title	= (TextView) findViewById(R.id.titleSmartlink);	
		title.setClickable(true);
		title.setOnClickListener(new OnClickListener(){


			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
			
		});
		mWifiAdmin = new EspWifiAdminSimple(this);
		mTvApSsid = (TextView) findViewById(R.id.tvApSssidConnected);
		mEdtApPassword = (EditText) findViewById(R.id.edtApPassword);
		mBtnConfirm = (Button) findViewById(R.id.btnConfirm);
		mSwitchIsSsidHidden = (Switch) findViewById(R.id.switchIsSsidHidden);
		mBtnConfirm.setOnClickListener(this);


	}


	@Override
	protected void onResume() {
		super.onResume();
		// display the connected ap's ssid
		String apSsid = mWifiAdmin.getWifiConnectedSsid();
		if (apSsid != null) {
			mTvApSsid.setText(apSsid);
		} else {
			mTvApSsid.setText("");
		}
		// check whether the wifi is connected
		boolean isApSsidEmpty = TextUtils.isEmpty(apSsid);
		mBtnConfirm.setEnabled(!isApSsidEmpty);
	}


	@Override
	public void onClick(View v) {


		if (v == mBtnConfirm) {
			String apSsid = mTvApSsid.getText().toString();
			String apPassword = mEdtApPassword.getText().toString();
			String apBssid = mWifiAdmin.getWifiConnectedBssid();
			Boolean isSsidHidden = mSwitchIsSsidHidden.isChecked();
			String isSsidHiddenStr = "NO";
			if (isSsidHidden) 
			{
				isSsidHiddenStr = "YES";
			}
			if (__IEsptouchTask.DEBUG) {
				Log.d(TAG, "mBtnConfirm is clicked, mEdtApSsid = " + apSsid
						+ ", " + " mEdtApPassword = " + apPassword);
			}
			new EsptouchAsyncTask2().execute(apSsid, apBssid, apPassword, isSsidHiddenStr);
		}
	}


	private class EsptouchAsyncTask2 extends AsyncTask<String, Void, IEsptouchResult> {


		private ProgressDialog mProgressDialog;


		private IEsptouchTask mEsptouchTask;


		@Override
		protected void onPreExecute() {
			mProgressDialog = new ProgressDialog(EsptouchDemoActivity.this);
			mProgressDialog
					.setMessage("Esptouch is configuring, please wait for a moment...");
			mProgressDialog.setCanceledOnTouchOutside(false);
			mProgressDialog.setOnCancelListener(new OnCancelListener() {
				@Override
				public void onCancel(DialogInterface dialog) {
					if (__IEsptouchTask.DEBUG) {
						Log.i(TAG, "progress dialog is canceled");
					}
					if (mEsptouchTask != null) {
						mEsptouchTask.interrupt();
					}
				}
			});
			mProgressDialog.setButton(DialogInterface.BUTTON_POSITIVE,
					"Waiting...", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
						}
					});
			mProgressDialog.show();
			mProgressDialog.getButton(DialogInterface.BUTTON_POSITIVE)
					.setEnabled(false);
		}


		@Override
		protected IEsptouchResult doInBackground(String... params) {
			String apSsid = params[0];
			String apBssid = params[1];
			String apPassword = params[2];
			String isSsidHiddenStr = params[3];
			boolean isSsidHidden = false;
			if(isSsidHiddenStr.equals("YES"))
			{
				isSsidHidden = true;
			}
			mEsptouchTask = new EsptouchTask(apSsid, apBssid, apPassword, isSsidHidden, EsptouchDemoActivity.this);
			IEsptouchResult result = mEsptouchTask.executeForResult();
			return result;
		}


		@Override
		protected void onPostExecute(IEsptouchResult result) {
			mProgressDialog.getButton(DialogInterface.BUTTON_POSITIVE)
					.setEnabled(true);
			mProgressDialog.getButton(DialogInterface.BUTTON_POSITIVE).setText(
					"Confirm");
			// it is unnecessary at the moment, add here just to show how to use isCancelled()
			if (!result.isCancelled()) {
				if (result.isSuc()) {
					mProgressDialog.setMessage("Esptouch success, bssid = "
							+ result.getBssid() + ",InetAddress = "
							+ result.getInetAddress().getHostAddress());
				} else {
					mProgressDialog.setMessage("Esptouch fail");
				}
			}
		}
	}
}
 
移植的部分代码:
/**
 * start smartlink wifi
 */
private void startSmartlink(){
    secondleft = 60;
    String strBssid = mWifiAdmin.getWifiConnectedBssid();

    Log.i(TAG, "strBssid :" + strBssid);
    boolean isSsidHidden = false;
    if(isSsidHiddenStr.equals("YES")){
        isSsidHidden = true;
    }

    Log.i(TAG, "mBtnConfirm is clicked, strBssid = " + strBssid + "strSsid = " + strSSid
            + ", " + " strPassword = " + strPsw);


    tvTick.setText(secondleft + "");
    showLayout(UI_STATE.Setting);
    timer = new Timer();

    timer.schedule(new TimerTask() {
        @Override
        public void run() {

            Log.i(TAG, " connect ");

            handler.sendEmptyMessage(handler_key.TICK_TIME.ordinal());
        }
    }, 1000, 1000);
    new EsptouchAsyncTask().execute(strSSid, strBssid, strPsw, isSsidHiddenStr);
}

/**
 * 内部类:异步处理wifi信息
 */
private class EsptouchAsyncTask extends AsyncTask<String, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(String... params) {

        String strSsid = params[0];
        String strBssid = params[1];
        String strPassword = params[2];
        String isSsidHiddenStr = params[3];
        boolean isSsidHidden = false;
        if(isSsidHiddenStr.equals("YES"))
        {
            isSsidHidden = true;
        }

        Log.i(TAG, "strSsid = :" + strSsid + ", strBssid = :" + strBssid
                + ", strPassword = :" + strPassword + ", isSsidHiddenStr = :" + isSsidHiddenStr);

        new EsptouchTask(strSsid, strBssid, strPassword, isSsidHidden, AirlinkActivity.this).executeForResult();
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}
具体的实现要看需要什么条件,可能开发的功能不一样。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值