Android 通过代码设置、打开wifi热点及热点连接的实现代码

Android 通过代码设置、打开wifi热点及热点连接的实现代码,第1张

概述用过快牙的朋友应该知道它们在两天设备之间传输文件的时候使用的是wifi热点,然后另一台便连接这个热点再进行传输。快牙传输速度惊人应该跟它的这种机制有关系吧。不知道它的搜索机制是怎样的,但我想应该可以通过热

用过快牙的朋友应该知道它们在两天设备之间传输文件的时候使用的是wifi热点,然后另一台便连接这个热点再进行传输。快牙传输速度惊人应该跟它的这种机制有关系吧。不知道它的搜索机制是怎样的,但我想应该可以通过热点的名字来进行判断吧。下面我们就来探讨一下如何自动创建一个wifi热点吧大笑

  创建wifi热点首先需要手机支持,建议开发的哥们整个好点的手机,我们公司那些个山寨设备,几近有一半是不支持热点的;其实创建热点很简单,先获取到wifi的服务,再配置热点名称、密码等等,然后再通过反射打开它就OK了。

  下面我们看看创建热点的代码实现:

package com.tel.lajoin.wifi.hotspot; import java.lang.reflect.Method; import androID.app.Activity; import androID.content.Context; import androID.net.wifi.WifiConfiguration; import androID.net.wifi.WifiManager; import androID.os.Bundle; import androID.vIEw.VIEw; import androID.Widget.button; public class HotspotActivity extends Activity {  private WifiManager wifiManager;  private button open;  private boolean flag=false;  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {   // Todo auto-generated method stub   super.onCreate(savedInstanceState);   setContentVIEw(R.layout.main);   //获取wifi管理服务   wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);   open=(button)findVIEwByID(R.ID.open_hotspot);   //通过按钮事件设置热点   open.setonClickListener(new VIEw.OnClickListener() {    @OverrIDe    public voID onClick(VIEw v) {     //如果是打开状态就关闭,如果是关闭就打开     flag=!flag;     setWifiApEnabled(flag);    }   });  }  // wifi热点开关  public boolean setWifiApEnabled(boolean enabled) {   if (enabled) { // disable WiFi in any case    //wifi和热点不能同时打开,所以打开热点的时候需要关闭wifi    wifiManager.setWifIEnabled(false);   }   try {    //热点的配置类    WifiConfiguration apConfig = new WifiConfiguration();    //配置热点的名称(可以在名字后面加点随机数什么的)    apConfig.SSID = "YRCCONNECTION";    //配置热点的密码    apConfig.preSharedKey="12122112";     //通过反射调用设置热点    Method method = wifiManager.getClass().getmethod(      "setWifiApEnabled",WifiConfiguration.class,Boolean.TYPE);    //返回热点打开状态    return (Boolean) method.invoke(wifiManager,apConfig,enabled);   } catch (Exception e) {    return false;   }  } } 

  布局就不写了吧,就一按钮,人人都知道的东西,写了也没啥意思。要实现文件传输,当然我们还需要写一个连接热点的客户端吧。连接热点的流程首先是搜索热点然后再判断热点是否符合规则然后再进行连接。

package com.tel.lajoin.wifiscan; import java.util.ArrayList; import java.util.List; import androID.app.Activity; import androID.content.broadcastReceiver; import androID.content.Context; import androID.content.Intent; import androID.content.IntentFilter; import androID.net.wifi.ScanResult; import androID.net.wifi.WifiConfiguration; import androID.net.wifi.WifiManager; import androID.os.Bundle; public class MainActivity extends Activity {   private List<ScanResult> wifiList;   private WifiManager wifiManager;   private List<String> passableHotsPot;   private WifiReceiver wifiReceiver;   private boolean isConnected=false;   private button connect;   @OverrIDe   public voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     init();   }   /* 初始化参数 */   public voID init() {     setContentVIEw(R.layout.main);     connect=(button)findVIEwByID(R.ID.connect);     wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);     wifiReceiver = new WifiReceiver();     //通过按钮事件搜索热点     connect.setonClickListener(new VIEw.OnClickListener() {       @OverrIDe       public voID onClick(VIEw v) {         wifiManager.startScan();       }     });      }   /* 监听热点变化 */   private final class WifiReceiver extends broadcastReceiver {     @OverrIDe     public voID onReceive(Context context,Intent intent) {       wifiList = wifiManager.getScanResults();       if (wifiList == null || wifiList.size() == 0 || isConnected)         return;       onReceiveNewNetworks(wifiList);     }   }   /*当搜索到新的wifi热点时判断该热点是否符合规格*/   public voID onReceiveNewNetworks(List<ScanResult> wifiList){     passableHotsPot=new ArrayList<String>();     for(ScanResult result:wifiList){       System.out.println(result.SSID);       if((result.SSID).contains("YRCCONNECTION"))         passableHotsPot.add(result.SSID);     }     synchronized (this) {       connectToHotpot();     }   }   /*连接到热点*/   public voID connectToHotpot(){     if(passableHotsPot==null || passableHotsPot.size()==0)       return;     WifiConfiguration wifiConfig=this.setWifiParams(passableHotsPot.get(0));     int wcgID = wifiManager.addNetwork(wifiConfig);     boolean flag=wifiManager.enableNetwork(wcgID,true);     isConnected=flag;     System.out.println("connect success? "+flag);   }   /*设置要连接的热点的参数*/   public WifiConfiguration setWifiParams(String ssID){     WifiConfiguration apConfig=new WifiConfiguration();     apConfig.SSID="\""+ssID+"\"";     apConfig.preSharedKey="\"12122112\"";     apConfig.hIDdenSSID = true;     apConfig.status = WifiConfiguration.Status.ENABLED;     apConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);     apConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);     apConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);     apConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);     apConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);     apConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);     return apConfig;   }   @OverrIDe   protected voID onDestroy() {     super.onDestroy();     /*销毁时注销广播*/     unregisterReceiver(wifiReceiver);   } }

   代码很简单,而且都有注释的,相信大伙儿能够看明白。  那就这样吧,至于文件传输建议还是去看看socket相关的文章吧。

总结

以上所述是小编给大家介绍的AndroID 通过代码设置、打开wifi热点及热点的连接的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

您可能感兴趣的文章:Android 连接Wifi和创建Wifi热点的实例Android获取周围WIFI热点服务android编程实现设置、打开wifi热点共享供他人连接的方法Android连接指定Wifi的方法实例代码Android判断是Wifi还是4G网络代码Android获取当前手机网络类型(2g、3g、4g、wifi)以及手机型号、版本号代码设置Android设备WIFI在休眠时永不断开的代码实现Android中判断有无可用网络的代码(是否是3G或者WIFI网络) 总结

以上是内存溢出为你收集整理的Android 通过代码设置、打开wifi热点及热点连接的实现代码全部内容,希望文章能够帮你解决Android 通过代码设置、打开wifi热点及热点连接的实现代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1142686.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存