Android用wifi热点组建局域网创建socket。Android手机开启热点,并开启手机GPRS联网的代码实现。
-- 判断是否有网络,编程- Android
public boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
> WI-FI直连, WifiP2p
Wi-Fi直连的Demo_Public- https://github.com/coderminer/Demo_Public/tree/master/Demo_wifi_direct , https://github.com/coderminer/Demo_Public
一旦两个设备之间建立了Wi-Fi直连,那么这两个设备之间就可以通过socket传输数据。
从Android4.0(API Level=14)开始,允许通过Wi-Fi模块在两个移动设备之间建立直接连接(这种技术称为Wi-Fi Direct),这种连接不需要无线路由作为中介,而只是像蓝牙一样在两个设备之间直接建立的数据传输通道。在Android SDK中提供了一些API,通过这些API可以发现其它支持Wi-Fi Direct的设备,也可以接收其它设备的Wi-Fi Direct请求。在Wi-Fi Direct出现之前,通常使用蓝牙或利用无线路由连接多部Android设备,蓝牙的有效距离比较短,而且传输速率不如Wi-Fi。不过有了Wi-Fi Direct后,两部设备可以直接连接,既方便又快速,非常适合数据分享。
> 便携式热点,Wifi热点
android的便携式热点打开与关闭都会发出 android.net.wifi.WIFI_AP_STATE_CHANGED 的广播,但是便携式热点相关的信息在WiFimanage中是隐藏的,这个广播是隐藏的。
private BroadcastReceiver wifiReceiver = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ("android.net.wifi.WIFI_AP_STATE_CHANGED".equals(action)) {
//便携式热点的状态为:10---正在关闭;11---已关闭;12---正在开启;13---已开启
int state = intent.getIntExtra("wifi_state", 0);
Log.i(TAG, "state= "+state);
wiFiAPObserver.stateChanged(state);
}
}
};
//创建热点
public void createAp() {
if (mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(false);
}
try {
WifiConfiguration apConfiguration = new WifiConfiguration();
apConfiguration.SSID = WifiAdmin.SSID;
apConfiguration.preSharedKey = WifiAdmin.PassWord;
apConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(mWifiManager, apConfiguration, true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 关闭WiFi热点
*/
public void closeWifiAp() {
if (isWifiApEnabled()) {
try {
Method method = mWifiManager.getClass().getMethod("getWifiApConfiguration");
method.setAccessible(true);
WifiConfiguration config = (WifiConfiguration) method.invoke(mWifiManager);
Method method2 = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method2.invoke(mWifiManager, config, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 判断热点是否打开
*
* @return
*/
public boolean isWifiApEnabled() {
try {
Method method = mWifiManager.getClass().getMethod("isWifiApEnabled");
method.setAccessible(true);
return (Boolean) method.invoke(mWifiManager);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
> wifi的打开与关闭
//WiFi开关与状态监听
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {//这个监听wifi的打开与关闭,与wifi的连接无关
Log.v("my2", "收到WIFI_STATE_CHANGED_ACTION");
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, -1111);
switch (wifiState) {
case WifiManager.WIFI_STATE_DISABLED:
Log.v("my2", "收到"+"WIFI_STATE_DISABLED");
break;
case WifiManager.WIFI_STATE_DISABLING:
Log.v("my2", "收到"+"WIFI_STATE_DISABLING");
break;
case WifiManager.WIFI_STATE_ENABLED:
Log.v("my2", "收到"+"WIFI_STATE_ENABLED");
break;
case WifiManager.WIFI_STATE_ENABLING:
Log.v("my2", "收到"+"WIFI_STATE_ENABLING");
break;
case WifiManager.WIFI_STATE_UNKNOWN:
Log.v("my2", "WIFI_STATE_UNKNOWN");
}
}
}
若开机时默认为打开WIFI状态会检测到WIFI_STATE_ENABLED,只会发送这一条广播.
// 监听网络连接,包括wifi和移动数据的打开和关闭,以及连接上可用的连接都会接到监听
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
//获取联网状态的NetworkInfo对象
NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (info != null) {
//如果当前的网络连接成功并且网络连接可用
if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) {
if (info.getType() == ConnectivityManager.TYPE_WIFI || info.getType() == ConnectivityManager.TYPE_MOBILE) {
Log.i("TAG", getConnectionType(info.getType()) + "连上");
}
} else {
Log.i("TAG", getConnectionType(info.getType()) + "断开");
}
}
}
> Android网络状态监听 有网无网状态,
class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable()) {
switch (networkInfo.getType()) {
case TYPE_MOBILE:
Toast.makeText(context, "正在使用2G/3G/4G网络", Toast.LENGTH_SHORT).show();
break;
case TYPE_WIFI:
Toast.makeText(context, "正在使用wifi上网", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
} else {
Toast.makeText(context, "当前无网络连接", Toast.LENGTH_SHORT).show();
}
}
}
}
public class NetworkConnectChangedReceiver extends BroadcastReceiver{
private static final String TAG = "NetworkChangedReceiver";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == ConnectivityManager.CONNECTIVITY_ACTION) {
/*判断当前网络时候可用以及网络类型*/
boolean isConnected = NetworkUtils.isConnected();
NetworkUtils.NetworkType networkType = NetworkUtils.getNetworkType();
EventBus.getDefault().post(new NetworkChangeEvent(isConnected, networkType));
}
}
}
-- 做到WIFI网络和GPRS网络的切换
由于Android没有提供直接切换GPRS网络的方法,通过查看系统源码发现,系统是调用IConnectivityManager类中的setMobileDataEnabled(boolean)方法来设置GPRS网络的,由于方法不可见,只能采用反射来调用。
public static void toggleMobileData(Context context, boolean enabled) {
ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Class conMgrClass = null; // ConnectivityManager类
Field conMgrField = null; // ConnectivityManager类中的字段
Object iConMgr = null; // IConnectivityManager类的引用
Class iConMgrClass = null; // IConnectivityManager类
Method setMobileDataEnabledMethod = null; // setMobileDataEnabled方法
try {
// 取得ConnectivityManager类
conMgrClass = Class.forName(conMgr.getClass().getName());
// 取得ConnectivityManager类中的对象mService
conMgrField = conMgrClass.getDeclaredField("mService");
// 设置mService可访问
conMgrField.setAccessible(true);
// 取得mService的实例化类IConnectivityManager
iConMgr = conMgrField.get(conMgr);
// 取得IConnectivityManager类
iConMgrClass = Class.forName(iConMgr.getClass().getName());
// 取得IConnectivityManager类中的setMobileDataEnabled(boolean)方法
setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
// 设置setMobileDataEnabled方法可访问
setMobileDataEnabledMethod.setAccessible(true);
// 调用setMobileDataEnabled方法
setMobileDataEnabledMethod.invoke(iConMgr, enabled);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (NoSuchFieldException e) {
e.printStackTrace();
}
catch (SecurityException e) {
e.printStackTrace();
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
}