hc
2023-11-07 f45e756958099c35d6afb746df1d40a1c6302cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.rockchip.alexa.jacky.async;
 
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
 
import com.rockchip.alexa.jacky.app.BaseApplication;
import com.rockchip.alexa.jacky.app.Context;
import com.rockchip.alexa.jacky.control.WifiControl;
import com.rockchip.alexa.jacky.info.WifiInfo;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * Created by Administrator on 2017/4/27.
 */
 
public class AsyncThread {
 
    private AsyncHandler mAsyncHandler;
    private WifiControl mWifiControl;
    private List<WifiInfo> mWifiList;
    private int mConnetedResult;
 
    private static final int MSG_SCAN_WIFI = 0x000000001;
    private static final int MSG_CONNECT_WIFI = 0x00000002;
 
    public AsyncThread() {
        HandlerThread ht = new HandlerThread("async");
        ht.start();
        mAsyncHandler = new AsyncHandler(ht.getLooper());
        mWifiControl = new WifiControl(BaseApplication.getApplication());
        mWifiList = new ArrayList<WifiInfo>();
    }
 
    private class AsyncHandler extends Handler {
        public AsyncHandler(Looper looper) {
            super(looper);
        }
 
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_SCAN_WIFI:
                    mWifiList = mWifiControl.startScan();
                    break;
                case MSG_CONNECT_WIFI:
                    Bundle bundle = (Bundle) msg.obj;
                    mConnetedResult = mWifiControl.connect(bundle.getString("SSID"), bundle.getString("PWD"), bundle.getInt("TYPE"));
                    break;
            }
        }
 
        /**
         * Waits for all the {@code Message} and {@code Runnable} currently in the queue
         * are processed.
         *
         * @return {@code false} if the wait was interrupted, {@code true} otherwise.
         */
        public boolean waitDone() {
            final Object waitDoneLock = new Object();
            final Runnable unlockRunnable = new Runnable() {
                @Override
                public void run() {
                    synchronized (waitDoneLock) {
                        waitDoneLock.notifyAll();
                    }
                }
            };
 
            synchronized (waitDoneLock) {
                mAsyncHandler.post(unlockRunnable);
                try {
                    waitDoneLock.wait();
                } catch (InterruptedException ex) {
                    return false;
                }
            }
            return true;
        }
    }
 
    public List<WifiInfo> scanWifi() {
        mWifiList.clear();
        mAsyncHandler.obtainMessage(MSG_SCAN_WIFI).sendToTarget();
        mAsyncHandler.waitDone();
        Log.d("AlexaActivity", "scan Wifi end. size:" + mWifiList.size());
        return mWifiList;
    }
 
    public List<WifiInfo> scanDeviceHotspot() {
        Log.d("AlexaActivity", "scanDeviceHotspot");
        List<WifiInfo> list = new ArrayList<>();
        mWifiList = scanWifi();
        for (WifiInfo info : mWifiList) {
            if (info.getSsid().trim().startsWith(Context.HOTSPOT_PREFIX)) {
                if (info.getFlags().contains("WEP") || info.getFlags().contains("PSK") || info.getFlags().contains("EAP"))
                    continue;
                list.add(info);
            }
        }
        return list;
    }
 
    public int connect(String ssid, String pwd, int type) {
        Bundle bundle = new Bundle();
        bundle.putString("SSID", ssid);
        bundle.putString("PWD", pwd);
        bundle.putInt("TYPE", type);
        mAsyncHandler.obtainMessage(MSG_CONNECT_WIFI, bundle).sendToTarget();
        mAsyncHandler.waitDone();
        return mConnetedResult;
    }
 
    public int connect(String ssid) {
        return connect(ssid, "", 0);
    }
}