package com.espressif.iot.task;
import android.content.Context;
import android.os.Looper;
import android.text.TextUtils;
import com.espressif.iot.esptouch.EsptouchResult;
import com.espressif.iot.esptouch.IEsptouchListener;
import com.espressif.iot.esptouch.IEsptouchResult;
import com.espressif.iot.protocol.EsptouchGenerator;
import com.espressif.iot.udp.UDPSocketClient;
import com.espressif.iot.udp.UDPSocketServer;
import com.espressif.iot.util.ByteUtil;
import com.espressif.iot.util.DebugUtil;
import com.espressif.iot.util.EspNetUtil;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
public class __EsptouchTask implements __IEsptouchTask {
/**
* one indivisible data contain 3 9bits info
*/
private static final int ONE_DATA_LEN = 3;
private static final String TAG = "EsptouchTask";
private volatile List<IEsptouchResult> mEsptouchResultList;
private volatile boolean mIsSuc = false;
private volatile boolean mIsInterrupt = false;
private volatile boolean mIsExecuted = false;
private final UDPSocketClient mSocketClient;
private final UDPSocketServer mSocketServer;
private final String mApSsid;
private final String mApBssid;
private final boolean mIsSsidHidden;
private final byte[] mApPassword;
private final Context mContext;
private AtomicBoolean mIsCancelled;
private IEsptouchTaskParameter mParameter;
private volatile Map<String, Integer> mBssidTaskSucCountMap;
private IEsptouchListener mEsptouchListener;
public __EsptouchTask(String apSsid, String apBssid, byte[] apPassword,
Context context, IEsptouchTaskParameter parameter,
boolean isSsidHidden) {
if (TextUtils.isEmpty(apSsid)) {
throw new IllegalArgumentException(
"the apSsid should be null or empty");
}
if (apPassword == null) {
DebugUtil.debug("esp", "app password ==null");
// apPassword = "";
}
mContext = context;
mApSsid = apSsid;
mApBssid = apBssid;
mApPassword = apPassword;
mIsCancelled = new AtomicBoolean(false);
mSocketClient = new UDPSocketClient();
mParameter = parameter;
mSocketServer = new UDPSocketServer(mParameter.getPortListening(),
mParameter.getWaitUdpTotalMillisecond(), context);
mIsSsidHidden = isSsidHidden;
mEsptouchResultList = new ArrayList<IEsptouchResult>();
mBssidTaskSucCountMap = new HashMap<String, Integer>();
}
private void __putEsptouchResult(boolean isSuc, String bssid,
InetAddress inetAddress) {
synchronized (mEsptouchResultList) {
// check whether the result receive enough UDP response
boolean isTaskSucCountEnough = false;
Integer count = mBssidTaskSucCountMap.get(bssid);
if (count == null) {
count = 0;
}
++count;
if (__IEsptouchTask.DEBUG) {
DebugUtil.debug(TAG, "__putEsptouchResult(): count = " + count);
}
mBssidTaskSucCountMap.put(bssid, count);
isTaskSucCountEnough = count >= mParameter
.getThresholdSucBroadcastCount();
if (!isTaskSucCountEnough) {
if (__IEsptouchTask.DEBUG) {
DebugUtil.debug(TAG, "__putEsptouchResult(): count = " + count
+ ", isn't enough");
}
return;
}
// check whether the result is in the mEsptouchResultList already
boolean isExist = false;
for (IEsptouchResult esptouchResultInList : mEsptouchResultList) {
if (esptouchResultInList.getBssid().equals(bssid)) {
isExist = true;
break;
}
}
// only add the result who isn't in the mEsptouchResultList
if (!isExist) {
if (__IEsptouchTask.DEBUG) {
DebugUtil.debug(TAG, "__putEsptouchResult(): put one more result");
}
final IEsptouchResult esptouchResult = new EsptouchResult(
isSuc, bssid, inetAddress);
mEsptouchResultList.add(esptouchResult);
if (mEsptouchListener != null) {
mEsptouchListener.onEsptouchResultAdded(esptouchResult);
}
}
}
}
private List<IEsptouchResult> __getEsptouchResultList() {
synchronized (mEsptouchResultList) {
if (mEsptouchResultList.isEmpty()) {
EsptouchResult esptouchResultFail = new EsptouchResult(false,
null, null);
esptouchResultFail.setIsCancelled(mIsCancelled.get());
mEsptouchResultList.add(esptouchResultFail);
}
return mEsptouchResultList;
}
}
private synchronized void __interrupt() {
if (!mIsInterrupt) {
mIsInterrupt = true;
mSocketClient.interrupt();
mSocketServer.interrupt();
// interrupt the current Thread which is used to wait for udp
// response
Thread.currentThread().interrupt();
}
}
@Override
public void interrupt() {
if (__IEsptouchTask.DEBUG) {
DebugUtil.debug(TAG, "interrupt()");
}
mIsCancelled.set(true);
__interrupt();
}
private void __listenAsyn(final int expectDataLen) {
new Thread() {
public void run() {
if (__IEsptouchTask.DEBUG) {
DebugUtil.debug(TAG, "__listenAsyn() start");
}
long startTimestamp = System.currentTimeMillis();
// byte[] apSsidAndPassword = ByteUtil.getBytesByString(mApSsid
// + mApPassword);
byte[] ssidbyte = ByteUtil.getBytesByString(mApSsid);
byte[] apSsidAndPassword = new byte[ssidbyte.length
+ mApPassword.length];
int i = 0;
for (; i < ssidbyte.length; i++) {
apSsidAndPassword[i] = ssidbyte[i];
}
for (int j = 0; j < mApPassword.length; j++) {
apSsidAndPassword[i + j] = mApPassword[j];
}
byte expectOneByte = (byte) (apSsidAndPassword.length + 9);
if (__IEsptouchTask.DEBUG) {
DebugUtil.debug(TAG, "expectOneByte: " + (0 + expectOneByte));
}
byte receiveOneByte = -1;
byte[] receiveBytes = null;
while (mEsptouchResultList.size() < mParameter
.getExpectTaskResultCount() && !mIsInterrupt) {
receiveBytes = mSocketServer
.receiveSpecLenBytes(expectDataLen);
if (receiveBytes != null) {
receiveOneByte = receiveBytes[0];
} else {
receiveOneByte = -1;
}
if (receiveOneByte == expectOneByte) {
if (__IEsptouchTask.DEBUG) {
DebugUtil.debug(TAG, "receive correct broadcast");
}
// change the socket's timeout
long consume = System.currentTimeMillis()
- startTimestamp;
int timeout = (int) (mParameter
.getWaitUdpTotalMillisecond() - consume);
if (timeout < 0) {
if (__IEsptouchTask.DEBUG) {
DebugUtil.debug(TAG, "esptouch timeout");
}
break;
} else {
if (__IEsptouchTask.DEBUG) {
DebugUtil.debug(TAG, "mSocketServer's new timeout is "
+ timeout + " milliseconds");
}
mSocketServer.setSoTimeout(timeout);
if (__IEsptouchTask.DEBUG) {
DebugUtil.debug(TAG, "receive correct broadcast");
}
if (receiveBytes != null) {
String bssid = ByteUtil.parseBssid(
receiveBytes,
mParameter.getEsptouchResultOneLen(),
mParameter.getEsptouchResultMacLen());
InetAddress inetAddress = EspNetUtil
.parseInetAddr(
receiveBytes,
mParameter
.getEsptouchResultOneLen()
+ mParameter
.getEsptouchResultMacLen(),
mParameter
.getEsptouchResultIpLen());
__putEsptouchResult(true, bssid, inetAddress);
}
}
} else {
if (__IEsptouchTask.DEBUG) {
DebugUtil.debug(TAG, "receive rubbish message, just ignore");
}
}
}
mIsSuc = mEsptouchResultList.size() >= mParameter
.getExpectTaskResultCount();
__EsptouchTask.this.__interrupt();
if (__IEsptouchTask.DEBUG) {
DebugUtil.debug(TAG, "__listenAsyn() finish");
}
}
}.start();
}
private boolean __execute(IEsptouchGenerator generator) {
long startTime = System.currentTimeMillis();
long currentTime = startTime;
long lastTime = currentTime
- mParameter.getTimeoutTotalCodeMillisecond();
byte[][] gcBytes2 = generator.getGCBytes2();
byte[][] dcBytes2 = generator.getDCB
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
SmartConfig智能设备快连demo,Esptouch快连demo,wifi智能设备快速配网demo,【Android工程师与智能家居产品的第一次接触②】给设备配网 Esp8266 wifi模块的快速配网和AP配网简介(付Android demo),详情参见:https://blog.csdn.net/qq_35350654/article/details/88911965
资源推荐
资源详情
资源评论
收起资源包目录
SmartConfig智能设备快连demo,Esptouch快连demo (100个子文件)
resources.ap_ 71KB
Esptouch.apk 534KB
jarlist.cache 120B
__EsptouchTask.class 10KB
ByteUtil.class 6KB
EsptouchDemoActivity.class 6KB
EsptouchDemoActivity$EsptouchAsyncTask2.class 5KB
UDPSocketServer.class 4KB
DatumCode.class 4KB
EsptouchTaskParameter.class 4KB
__EsptouchTask$1.class 4KB
DebugUtil.class 3KB
EspNetUtil.class 3KB
UDPSocketClient.class 3KB
EspWifiAdminSimple.class 2KB
EsptouchTask.class 2KB
EsptouchDemoActivity$1.class 2KB
DataCode.class 2KB
EsptouchDemoActivity$EsptouchAsyncTask2$1.class 2KB
EsptouchGenerator.class 2KB
GuideCode.class 1KB
CRC8.class 1KB
EsptouchResult.class 1KB
DebugUtil$DebugType.class 1KB
EsptouchDemoActivity$EsptouchAsyncTask2$2.class 1KB
IEsptouchTaskParameter.class 904B
R$id.class 885B
R$array.class 829B
R.class 805B
R$string.class 656B
__IEsptouchTask.class 598B
IEsptouchTask.class 549B
R$color.class 513B
R$dimen.class 500B
R$layout.class 492B
R$drawable.class 446B
R$attr.class 379B
BuildConfig.class 371B
IEsptouchResult.class 276B
IEsptouchListener.class 224B
IEsptouchGenerator.class 192B
ICodeData.class 172B
.classpath 491B
classes.dex 1.88MB
android-support-v4.jar 741KB
okhttp-3.2.0.jar 328KB
android-support-v4-cf9e87a2b410d24f4cfd5f781b5391b2.jar 267KB
okhttp-3.2.0-b85301a9c71c0e7efc033a4a5e994e27.jar 144KB
okio-1.6.0.jar 64KB
gradle-wrapper.jar 52KB
okio-1.6.0-a183b642402e4183b43b11fb99758e82.jar 34KB
__EsptouchTask.java 11KB
ByteUtil.java 9KB
DatumCode.java 8KB
EsptouchDemoActivity.java 7KB
UDPSocketServer.java 4KB
EsptouchTaskParameter.java 4KB
UDPSocketClient.java 3KB
IEsptouchTaskParameter.java 3KB
R.java 3KB
EsptouchGenerator.java 3KB
EsptouchTask.java 3KB
DebugUtil.java 3KB
DataCode.java 2KB
EspNetUtil.java 2KB
EspWifiAdminSimple.java 2KB
IEsptouchTask.java 2KB
__IEsptouchTask.java 2KB
CRC8.java 1KB
EsptouchResult.java 1KB
GuideCode.java 887B
IEsptouchResult.java 619B
ICodeData.java 438B
IEsptouchGenerator.java 340B
IEsptouchListener.java 294B
BuildConfig.java 173B
ic_launcher-web.png 38KB
ic_launcher.png 9KB
ic_launcher.png 7KB
ic_launcher.png 6KB
ic_launcher.png 4KB
ic_launcher.png 4KB
ic_launcher.png 3KB
ic_launcher.png 3KB
ic_launcher.png 2KB
org.eclipse.jdt.core.prefs 598B
org.eclipse.core.resources.prefs 57B
.project 844B
project.properties 563B
esptouch_demo_activity.xml 4KB
esptouch_activity.xml 4KB
arrays.xml 2KB
AndroidManifest.xml 1KB
AndroidManifest.xml 1KB
strings.xml 441B
dimens.xml 269B
dimens.xml 213B
colors.xml 208B
dimens.xml 196B
lint.xml 53B
共 100 条
- 1
资源评论
玉念聿辉
- 粉丝: 343
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功