zoukankan      html  css  js  c++  java
  • android 代码设置、打开wifi热点及热点的连接

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

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

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

     1 package com.tel.lajoin.wifi.hotspot;
     2 
     3 import java.lang.reflect.Method;
     4 
     5 import android.app.Activity;
     6 import android.content.Context;
     7 import android.net.wifi.WifiConfiguration;
     8 import android.net.wifi.WifiManager;
     9 import android.os.Bundle;
    10 import android.view.View;
    11 import android.widget.Button;
    12 
    13 public class HotspotActivity extends Activity {
    14 private WifiManager wifiManager;
    15 private Button open;
    16 private boolean flag=false;
    17 
    18 @Override
    19 protected void onCreate(Bundle savedInstanceState) {
    20 // TODO Auto-generated method stub
    21 super.onCreate(savedInstanceState);
    22 setContentView(R.layout.main);
    23 //获取wifi管理服务
    24 wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    25 open=(Button)findViewById(R.id.open_hotspot);
    26 //通过按钮事件设置热点
    27 open.setOnClickListener(new View.OnClickListener() {
    28 @Override
    29 public void onClick(View v) {
    30 //如果是打开状态就关闭,如果是关闭就打开
    31 flag=!flag;
    32 setWifiApEnabled(flag);
    33 }
    34 });
    35 }
    36 
    37 // wifi热点开关
    38 public boolean setWifiApEnabled(boolean enabled) {
    39 if (enabled) { // disable WiFi in any case
    40 //wifi和热点不能同时打开,所以打开热点的时候需要关闭wifi
    41 wifiManager.setWifiEnabled(false);
    42 }
    43 try {
    44 //热点的配置类
    45 WifiConfiguration apConfig = new WifiConfiguration();
    46 //配置热点的名称(可以在名字后面加点随机数什么的)
    47 apConfig.SSID = "YRCCONNECTION";
    48 //配置热点的密码
    49 apConfig.preSharedKey="12122112";
    50      //通过反射调用设置热点
    51 Method method = wifiManager.getClass().getMethod(
    52 "setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
    53 //返回热点打开状态
    54 return (Boolean) method.invoke(wifiManager, apConfig, enabled);
    55 } catch (Exception e) {
    56 return false;
    57 }
    58 }
    59 }
    View Code

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

    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);
    }
    }
    View Code
    天生我才必有用,千金散去还复来!
  • 相关阅读:
    老毛桃PE修改方法(屏蔽更改主页,屏蔽加装的绿色浏览器)
    老毛桃winpe优盘启动系统个性修改全攻略
    SQL2000和SQL2005和SQL2008同时安装问题
    基于FFmpeg的音频编码(PCM数据编码成AAC android)
    springboot + mybatis +easyUI整合案例
    spring thymeleaf 自定义标签
    速度挑战
    兼顾pc和移动端的textarea字数监控的实现方法
    CSS3+JS 实现的便签应用
    JavaScript实现碰撞检测(分离轴定理)
  • 原文地址:https://www.cnblogs.com/Jack-Lu/p/3678688.html
Copyright © 2011-2022 走看看