|  | 
 
| 本帖最后由 notname 于 2019-6-2 13:26 编辑 
 想通过代码实现WIFI热点的创建,参考了网上JAVA的代码调用wifimanager,总是出现闪退。。。
 
   
 以下是参考的JAVA代码
 
 
 复制代码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; 
  } 
 } 
}
 
 
 | 
 |