【问题标题】:Which android classes to use for controlling wifi hotspot?哪些 android 类用于控制 wifi 热点?
【发布时间】:2015-03-12 14:22:08
【问题描述】:

我想将硬件设备连接到 android 热点。 我的应用将设置热点并检测设备连接。

我尝试使用 SDK 21 提供的 p2p 示例,但是当启用热点时,WIFI_P2P_STATE_ENABLED 告诉我 P2P 已禁用: http://developer.android.com/guide/topics/connectivity/wifip2p.html

因此我假设P2P不等同于android热点管理,如果我错了,请纠正我。

谁能推荐使用哪个库来设置和检测 wifi 热点上的连接? 谢谢, 罗

【问题讨论】:

  • fi 你想连接到任何wifi热点这将是apis-->stackoverflow.com/questions/26645943/…,你能具体提到你想连接的配置是什么
  • 感谢回复,我正在尝试创建一个设备可以连接的AP。阅读您的示例,您是否认为使用 WifiManager.AddNetwork 为 wifi 网络创建配置?
  • 我刚刚完成了测试,WifiManager 似乎专门用于管理手机可以连接的网络,而不是我想要控制的 wifi AP。任何指针表示赞赏,谢谢
  • 好的,我能理解的是你想控制的android中的wifi列表?
  • No diva,我想检测连接到手机安卓 AP 的设备。谢谢

标签: android client android-wifi wifimanager


【解决方案1】:

我找到了以编程方式打开 AP 的解决方案:

Android turn On/Off WiFi HotSpot programmatically

看来你需要使用反射来访问WifiManager中的ap函数

【讨论】:

    【解决方案2】:

    创建热点使用这个函数

    /**
         * 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

    【讨论】:

      猜你喜欢
      • 2011-10-26
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      相关资源
      最近更新 更多