使用哪些Android类来控制WiFi热点?

3
我想将硬件设备连接到Android热点。我的应用程序将设置一个热点并检测设备的连接。
我尝试使用SDK 21提供的P2P示例,但是当启用热点时,WIFI_P2P_STATE_ENABLED告诉我P2P已禁用: http://developer.android.com/guide/topics/connectivity/wifip2p.html 因此,我认为P2P不等同于Android热点管理,请纠正我如果我错了。
有人可以推荐哪个库用于设置和检测WiFi热点上的连接吗? 谢谢, Ro

如果您想连接到任何WiFi热点,这将是API-->http://stackoverflow.com/questions/26645943/how-to-change-wifi-advanced-option-from-code-that-chrome-lost-access-to-internet,您能具体说明您想连接的配置是什么吗? - KOTIOS
谢谢回复,我正在尝试创建一个设备可以连接的AP。根据您的示例,您认为是否可以使用WifiManager.AddNetwork创建wifi网络的配置? - RonanE
我刚刚完成了测试,WifiManager似乎专门用于管理手机可以连接到哪些网络,而不是我想要控制的wifi AP。如果有任何指针,请告诉我,谢谢。 - RonanE
好的,我理解的是您想要控制Android中的WiFi列表? - KOTIOS
不用担心,我想检测连接到手机Android AP的设备。谢谢。 - RonanE
好的,我会查看这个并回复。 - KOTIOS
2个回答

0
使用此函数创建热点:

createHotspot()

/**
     * Start AccessPoint mode with the specified
     * configuration. If the radio is already running in
     * AP mode, update the new configuration
     * Note that starting in access point mode disables station
     * mode operation
     * @param wifiConfig SSID, security and channel details as part of WifiConfiguration
     * @return {@code true} if the operation succeeds, {@code false} otherwise
     */
    public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
        try {
            if (enabled) { // disable WiFi in any case
                mWifiManager.setWifiEnabled(false);
            }

            Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
            return (Boolean) method.invoke(mWifiManager, wifiConfig, enabled);
        } catch (Exception e) {
            Log.e(this.getClass().toString(), "", e);
            return false;
        }
    }

要获取热点列表,请使用以下方法:

/**
     * Gets a list of the clients connected to the Hotspot, reachable timeout is 300
     * @param onlyReachables {@code false} if the list should contain unreachable (probably disconnected) clients, {@code true} otherwise
     * @param finishListener, Interface called when the scan method finishes
     */
    public void getClientList(boolean onlyReachables, FinishScanListener finishListener) {
        getClientList(onlyReachables, 300, finishListener );
    }

    /**
     * Gets a list of the clients connected to the Hotspot 
     * @param onlyReachables {@code false} if the list should contain unreachable (probably disconnected) clients, {@code true} otherwise
     * @param reachableTimeout Reachable Timout in miliseconds
     * @param finishListener, Interface called when the scan method finishes 
     */
    public void getClientList(final boolean onlyReachables, final int reachableTimeout, final FinishScanListener finishListener) {


        Runnable runnable = new Runnable() {
            public void run() {

                BufferedReader br = null;
                final ArrayList<ClientScanResult> result = new ArrayList<ClientScanResult>();

                try {
                    br = new BufferedReader(new FileReader("/proc/net/arp"));
                    String line;
                    while ((line = br.readLine()) != null) {
                        String[] splitted = line.split(" +");

                        if ((splitted != null) && (splitted.length >= 4)) {
                            // Basic sanity check
                            String mac = splitted[3];

                            if (mac.matches("..:..:..:..:..:..")) {
                                boolean isReachable = InetAddress.getByName(splitted[0]).isReachable(reachableTimeout);

                                if (!onlyReachables || isReachable) {
                                    result.add(new ClientScanResult(splitted[0], splitted[3], splitted[5], isReachable));
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    Log.e(this.getClass().toString(), e.toString());
                } finally {
                    try {
                        br.close();
                    } catch (IOException e) {
                        Log.e(this.getClass().toString(), e.getMessage());
                    }
                }

                // Get a handler that can be used to post to the main thread
                Handler mainHandler = new Handler(context.getMainLooper());
                Runnable myRunnable = new Runnable() {
                    @Override
                    public void run() {
                        finishListener.onFinishScan(result);
                    }
                };
                mainHandler.post(myRunnable);
            }
        };

        Thread mythread = new Thread(runnable);
        mythread.start();
    }

连接到热点,请使用以下方法:

public Boolean connectToHotspot(WifiManager wifiManager, String ssid) 
    {
        this.wifiManager = wifiManager;
        WifiConfiguration wc = new WifiConfiguration();
        wc.SSID = "\"" +encodeSSID(ssid) +"\"";
        wc.preSharedKey  = "\"" + generatePassword(new StringBuffer(ssid).reverse().toString())  +  "\"";
        wifiManager.addNetwork(wc);
        List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        for( WifiConfiguration i : list ) {
            if(i!=null && i.SSID != null && i.SSID.equals(wc.SSID)) 
            {
                 wifiManager.disconnect();
                 boolean status = wifiManager.enableNetwork(i.networkId, true);
                 wifiManager.reconnect();               
                 return status;
            }
         }
        return false;
    }

参考资料:https://github.com/nickrussler/Android-Wifi-Hotspot-Manager-Class/blob/master/src/com/whitebyte/wifihotspotutils/WifiApManager.java

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接