1. 程式人生 > 其它 >android9.0之後wifi熱點原生介面開發示例

android9.0之後wifi熱點原生介面開發示例

話不多說,直接上程式碼了,這示例是直接呼叫原生介面實現的,沒有使用反射的方式,如果找不到介面無法編譯,請依賴一下對應系統的framewords.jar,並且參考我https://www.cnblogs.com/huhe/p/15848891.html文章的說明配置一下androidStudio編譯環境即可

MainActivity.java
  1 package com.xxx.wifi_ap;
  2 
  3 import static android.net.ConnectivityManager.TETHERING_WIFI;
  4 
  5 import androidx.appcompat.app.AppCompatActivity;
6 7 import android.content.BroadcastReceiver; 8 import android.content.Context; 9 import android.content.Intent; 10 import android.content.IntentFilter; 11 import android.net.ConnectivityManager; 12 import android.net.wifi.WifiConfiguration; 13 import android.net.wifi.WifiManager; 14 import
android.os.Bundle; 15 import android.os.Handler; 16 import android.os.Looper; 17 import android.util.Log; 18 import android.view.MotionEvent; 19 import android.view.View; 20 import android.view.inputmethod.InputMethodManager; 21 import android.widget.Button; 22 import android.widget.EditText;
23 import android.widget.LinearLayout; 24 import android.widget.TextView; 25 import android.widget.Toast; 26 27 public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener { 28 29 private static final IntentFilter WIFI_INTENT_FILTER = new IntentFilter(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); 30 private static final String TAG = "MainActivity"; 31 private ConnectivityManager mConnectivityManager; 32 private WifiManager mWifiManager; 33 private final ConnectivityManager.OnStartTetheringCallback mOnStartTetheringCallback = 34 new ConnectivityManager.OnStartTetheringCallback() { 35 @Override 36 public void onTetheringFailed() { 37 super.onTetheringFailed(); 38 Log.e(TAG,"onTetheringFailed!!!"); 39 } 40 }; 41 42 private LinearLayout root_layout; 43 private Button mAPEnable; 44 private EditText mEditName; 45 private EditText mEditPwd; 46 private TextView mApMsg; 47 private Button mSave; 48 private Button mGoDesktop; 49 private boolean mIsOnClick; 50 private String mApName,mApPwd; 51 52 @Override 53 protected void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 setContentView(R.layout.activity_main); 56 57 mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 58 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 59 60 WIFI_INTENT_FILTER.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); 61 registerReceiver(mReceiver, WIFI_INTENT_FILTER); 62 63 mInitView(); 64 } 65 66 @Override 67 protected void onDestroy() { 68 unregisterReceiver(mReceiver); 69 super.onDestroy(); 70 } 71 72 private void mInitView(){ 73 root_layout = findViewById(R.id.root_layout); 74 root_layout.setOnTouchListener(this); 75 76 mAPEnable = findViewById(R.id.ap_enable); 77 mAPEnable.setText(mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_DISABLED?"開啟熱點":"關閉熱點"); 78 79 mEditName = findViewById(R.id.edit_name); 80 mApName = mWifiManager.getWifiApConfiguration().SSID; 81 mEditName.setText(mApName); 82 83 mEditPwd = findViewById(R.id.edit_pwd); 84 mApPwd = mWifiManager.getWifiApConfiguration().preSharedKey; 85 mEditPwd.setText(mApPwd); 86 87 mApMsg = findViewById(R.id.ap_msg); 88 mSave = findViewById(R.id.save_data); 89 mGoDesktop = findViewById(R.id.go_desktop); 90 91 mAPEnable.setOnClickListener(this); 92 mSave.setOnClickListener(this); 93 mGoDesktop.setOnClickListener(this); 94 } 95 96 private void setApName(String deviceName) { 97 final WifiConfiguration config = mWifiManager.getWifiApConfiguration(); 98 config.SSID = deviceName; 99 // TODO: If tether is running, turn off the AP and restart it after setting config. 100 mWifiManager.setWifiApConfiguration(config); 101 } 102 103 private void setApPassword(String devicePwd) { 104 final WifiConfiguration config = mWifiManager.getWifiApConfiguration(); 105 config.preSharedKey = devicePwd; 106 // TODO: If tether is running, turn off the AP and restart it after setting config. 107 mWifiManager.setWifiApConfiguration(config); 108 } 109 110 private void setApProfile(int deviceProfile) { 111 final WifiConfiguration config = mWifiManager.getWifiApConfiguration(); 112 config.allowedKeyManagement.set(deviceProfile); 113 // TODO: If tether is running, turn off the AP and restart it after setting config. 114 mWifiManager.setWifiApConfiguration(config); 115 } 116 117 private int getApProfile(){ 118 final WifiConfiguration config = mWifiManager.getWifiApConfiguration(); 119 int mSecurityValue; 120 if (config != null && config.getAuthType() == WifiConfiguration.KeyMgmt.NONE) { 121 mSecurityValue = WifiConfiguration.KeyMgmt.NONE; 122 } else { 123 mSecurityValue = WifiConfiguration.KeyMgmt.WPA2_PSK; 124 } 125 return mSecurityValue; 126 } 127 128 private String getApProfileStr(int profile){ 129 return profile == WifiConfiguration.KeyMgmt.NONE?"NONE":"WPA2_PSK"; 130 } 131 132 private void setApEnable(boolean enable){ 133 if(enable){ 134 startTether(); 135 }else { 136 stopTether(); 137 } 138 } 139 140 private void saveConfig(){ 141 if(mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_DISABLED){ 142 runOnUiThread(new Runnable() { 143 @Override 144 public void run() { 145 Toast.makeText(MainActivity.this, "請先開啟熱點再儲存修改!", Toast.LENGTH_LONG).show(); 146 } 147 }); 148 return; 149 } 150 151 if(!mApName.equals(mEditName.getText().toString())){ 152 setApName(mEditName.getText().toString()); 153 } 154 155 if(!mApPwd.equals(mEditPwd.getText().toString())){ 156 setApPassword(mEditPwd.getText().toString()); 157 } 158 } 159 160 @Override 161 public void onClick(View v) { 162 switch (v.getId()){ 163 case R.id.ap_enable: 164 try { 165 setApEnable(mIsOnClick = !mIsOnClick); 166 }catch (Exception e){ 167 e.printStackTrace(); 168 } 169 break; 170 case R.id.save_data: 171 try { 172 saveConfig(); 173 }catch (Exception e){ 174 e.printStackTrace(); 175 } 176 break; 177 case R.id.go_desktop: 178 moveTaskToBack(true); 179 break; 180 } 181 } 182 183 void stopTether() { 184 Log.i(TAG,"stopTether()"); 185 mConnectivityManager.stopTethering(TETHERING_WIFI); 186 } 187 188 void startTether() { 189 Log.i(TAG,"startTether()"); 190 mConnectivityManager.startTethering(TETHERING_WIFI, true, 191 mOnStartTetheringCallback, new Handler(Looper.getMainLooper())); 192 } 193 194 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 195 @Override 196 public void onReceive(Context context, Intent intent) { 197 String action = intent.getAction(); 198 if (WifiManager.WIFI_AP_STATE_CHANGED_ACTION.equals(action)) { 199 final int state = intent.getIntExtra( 200 WifiManager.EXTRA_WIFI_AP_STATE, WifiManager.WIFI_AP_STATE_FAILED); 201 handleWifiApStateChanged(state); 202 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) { 203 } 204 } 205 }; 206 207 private void handleWifiApStateChanged(int state) { 208 String changed; 209 switch (state) { 210 case WifiManager.WIFI_AP_STATE_ENABLING: 211 Log.i(TAG,"WIFI_AP_STATE_ENABLING"); 212 changed = "熱點開啟中"; 213 break; 214 case WifiManager.WIFI_AP_STATE_ENABLED: 215 Log.i(TAG,"WIFI_AP_STATE_ENABLED"); 216 changed = "熱點已開啟"; 217 break; 218 case WifiManager.WIFI_AP_STATE_DISABLING: 219 Log.i(TAG,"WIFI_AP_STATE_DISABLING"); 220 changed = "熱點關閉中"; 221 break; 222 case WifiManager.WIFI_AP_STATE_DISABLED: 223 Log.i(TAG,"WIFI_AP_STATE_DISABLED"); 224 changed = "熱點已關閉"; 225 break; 226 default: 227 Log.i(TAG,"default"); 228 changed = "default"; 229 break; 230 } 231 updateApInfo(changed); 232 } 233 234 private void updateApInfo(String changed) { 235 String name = mWifiManager.getWifiApConfiguration().SSID; 236 String pwd = mWifiManager.getWifiApConfiguration().preSharedKey; 237 String profile = getApProfileStr(getApProfile()); 238 239 mUpdateMsg("stateChanged:"+changed+" name:"+name+" pwd:"+pwd+" profile:"+profile); 240 } 241 242 private void mUpdateMsg(String msg){ 243 runOnUiThread(() -> { 244 mAPEnable.setText(mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_DISABLED?"開啟熱點":"關閉熱點"); 245 mApMsg.setText(msg); 246 }); 247 } 248 249 @Override 250 public boolean onTouch(View view, MotionEvent motionEvent) { 251 if(null != getCurrentFocus()){ 252 InputMethodManager manager= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 253 if(manager != null){ 254 return manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0); 255 } 256 return false; 257 } 258 return true; 259 } 260 }
activity_main.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     android:id="@+id/root_layout"
 5     xmlns:tools="http://schemas.android.com/tools"
 6     android:layout_width="match_parent"
 7     android:layout_height="match_parent"
 8     android:layout_gravity="center"
 9     android:gravity="center"
10     android:orientation="vertical"
11     tools:context=".MainActivity">
12 
13     <Button
14         android:id="@+id/ap_enable"
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
17         android:text="開啟熱點"/>
18 
19     <TextView
20         android:id="@+id/ap_msg"
21         android:layout_width="wrap_content"
22         android:layout_height="wrap_content"
23         android:text="熱點已關閉"/>
24 
25     <EditText
26         android:id="@+id/edit_name"
27         android:layout_width="200dp"
28         android:layout_height="wrap_content"
29         android:hint="熱點名稱"/>
30 
31     <EditText
32         android:id="@+id/edit_pwd"
33         android:layout_width="200dp"
34         android:layout_height="wrap_content"
35         android:hint="熱點密碼"/>
36 
37     <Button
38         android:id="@+id/save_data"
39         android:layout_width="wrap_content"
40         android:layout_height="wrap_content"
41         android:text="儲存修改"/>
42 
43     <Button
44         android:id="@+id/go_desktop"
45         android:layout_width="wrap_content"
46         android:layout_height="wrap_content"
47         android:text="回到桌面"/>
48 
49 </LinearLayout>