用過快牙的朋友應該知道它們在兩天設備之間傳輸文件的時候使用的是wifi熱點,然后另一臺便連接這個熱點再進行傳輸。快牙傳輸速度驚人應該跟它的這種機制有關系吧。不知道它的搜索機制是怎樣的,但我想應該可以通過熱點的名字來進行判斷吧。下面我們就來探討一下如何自動創建一個wifi熱點吧大笑
創建wifi熱點首先需要手機支持,建議開發的哥們整個好點的手機,我們公司那些個山寨設備,幾近有一半是不支持熱點的;其實創建熱點很簡單,先獲取到wifi的服務,再配置熱點名稱、密碼等等,然后再通過反射打開它就OK了。
下面我們看看創建熱點的代碼實現:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
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 ; } } } |
布局就不寫了吧,就一按鈕,人人都知道的東西,寫了也沒啥意思。要實現文件傳輸,當然我們還需要寫一個連接熱點的客戶端吧。連接熱點的流程首先是搜索熱點然后再判斷熱點是否符合規則然后再進行連接。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
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熱點及熱點的連接的實現代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://blog.csdn.net/qq_24451593/article/details/80390172