android編程實現(xiàn)設(shè)置、打開wifi熱點共享供他人連接的方法
本文實例講述了android編程實現(xiàn)設(shè)置、打開wifi熱點共享供他人連接的方法。分享給大家供大家參考,具體如下:
用過快牙的朋友應(yīng)該知道它們在兩天設(shè)備之間傳輸文件的時候使用的是wifi熱點,然后另一臺便連接這個熱點再進(jìn)行傳輸。快牙傳輸速度驚人應(yīng)該跟它的這種機制有關(guān)系吧。不知道它的搜索機制是怎樣的,但我想應(yīng)該可以通過熱點的名字來進(jìn)行判斷吧。下面我們就來探討一下如何自動創(chuàng)建一個wifi熱點吧
創(chuàng)建wifi熱點首先需要手機支持,建議開發(fā)的哥們整個好點的手機,我們公司那些個山寨設(shè)備,幾近有一半是不支持熱點的;其實創(chuàng)建熱點很簡單,先獲取到wifi的服務(wù),再配置熱點名稱、密碼等等,然后再通過反射打開它就OK了。
下面我們看看創(chuàng)建熱點的代碼實現(xiàn):
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管理服務(wù)
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
open=(Button)findViewById(R.id.open_hotspot);
//通過按鈕事件設(shè)置熱點
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//如果是打開狀態(tài)就關(guān)閉,如果是關(guān)閉就打開
flag=!flag;
setWifiApEnabled(flag);
}
});
}
// wifi熱點開關(guān)
public boolean setWifiApEnabled(boolean enabled) {
if (enabled) { // disable WiFi in any case
//wifi和熱點不能同時打開,所以打開熱點的時候需要關(guān)閉wifi
wifiManager.setWifiEnabled(false);
}
try {
//熱點的配置類
WifiConfiguration apConfig = new WifiConfiguration();
//配置熱點的名稱(可以在名字后面加點隨機數(shù)什么的)
apConfig.SSID = "YRCCONNECTION";
//配置熱點的密碼
apConfig.preSharedKey="12122112";
//通過反射調(diào)用設(shè)置熱點
Method method = wifiManager.getClass().getMethod(
"setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
//返回?zé)狳c打開狀態(tài)
return (Boolean) method.invoke(wifiManager, apConfig, enabled);
} catch (Exception e) {
return false;
}
}
}
布局就不寫了吧,就一按鈕,人人都知道的東西,寫了也沒啥意思。要實現(xiàn)文件傳輸,當(dāng)然我們還需要寫一個連接熱點的客戶端吧。連接熱點的流程首先是搜索熱點然后再判斷熱點是否符合規(guī)則然后再進(jìn)行連接。
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();
}
/* 初始化參數(shù) */
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();
}
});
}
/* 監(jiān)聽熱點變化 */
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);
}
}
/*當(dāng)搜索到新的wifi熱點時判斷該熱點是否符合規(guī)格*/
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);
}
/*設(shè)置要連接的熱點的參數(shù)*/
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相關(guān)的文章吧。
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android通過ViewModel保存數(shù)據(jù)實現(xiàn)多頁面的數(shù)據(jù)共享功能
- Android開發(fā)之5.0activity跳轉(zhuǎn)時共享元素的使用方法
- Android數(shù)據(jù)共享 sharedPreferences 的使用方法
- android與asp.net服務(wù)端共享session的方法詳解
- 詳解Android(共享元素)轉(zhuǎn)場動畫開發(fā)實踐
- Android 仿摩拜單車共享單車進(jìn)度條實現(xiàn)StepView效果
- Android開發(fā)中多進(jìn)程共享數(shù)據(jù)簡析
- Android設(shè)備間實現(xiàn)藍(lán)牙(Bluetooth)共享上網(wǎng)
- Android實現(xiàn)不同apk間共享數(shù)據(jù)的方法(2種方法)
- Android編程實現(xiàn)兩個Activity之間共享數(shù)據(jù)及互相訪問的方法
- android不同activity之間共享數(shù)據(jù)解決方法
- Android 7.0應(yīng)用之間如何共享文件
相關(guān)文章
詳解Android studio ndk配置cmake開發(fā)native C
這篇文章主要介紹了詳解Android studio ndk配置cmake開發(fā)native C,非常具有實用價值,需要的朋友可以參考下2017-09-09
Android 中ListView和GridView賦值錯位
這篇文章主要介紹了Android 中ListView和GridView賦值錯位的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10
Android仿新浪微博自定義ListView下拉刷新(4)
這篇文章主要為大家詳細(xì)介紹了Android仿新浪微博自定義ListView下拉刷新,重點介紹了Adapter的詳細(xì)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Android 自定義gradle property詳解及實例代碼
這篇文章主要介紹了Android 自定義gradle property詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android自定義控件ViewFipper實現(xiàn)豎直跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件ViewFipper實現(xiàn)豎直跑馬燈效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12

