700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > android怎么开启wifi热点 android 开启wifi热点api

android怎么开启wifi热点 android 开启wifi热点api

时间:2023-06-08 05:23:28

相关推荐

android怎么开启wifi热点 android 开启wifi热点api

android 开启wifi热点api

android默认开启wifi热点方式只能通过设置-移动网络共享-便携式wlan热点,为了开启一个热点要进行3部操作,实属麻烦!

因此通过网上查资料,实现wifi热点的一键启动!

通过google获知,android把wifi相关的api给隐藏了,因此只能通过反射机制进行调用!

/**

* Start AccessPoint mode with the specified

* configuration. If the radio is already running in

* AP mode, update the new configuration

* Note that starting in access point mode disables station

* mode operation

* @param wifiConfig SSID, security and channel details as

* part of WifiConfiguration

* @return {@code true} if the operation succeeds, {@code false} otherwise

*

* @hide Dont open up yet

*/

public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {

try {

mService.setWifiApEnabled(wifiConfig, enabled);

return true;

} catch (RemoteException e) {

return false;

}

}

配置AP的选项是属于wifi管理的一部分!获取当前系统wifi实例的方式如下:

WifiManger manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

获取当前ap状态的方式如下:

public Boolean getApState() {

Boolean bApState = FALSE;

try {

Method method = manager.getClass().getMethod("isWifiApEnabled");

bApState = (Boolean)method.invoke(manager);

} catch (Exception e) {

e.printStackTrace();

bApState = FALSE;

}

return bApState;

}

配置ap使用的类为:WifiConfiguration //本例直接采用默认配置好的

public WifiConfiguration getApConfiguration() {

WifiConfiguration apConfigure = new WifiConfiguretion();

return apConfigure;

}

使能ap方式如下:

public void setWifiAp(Boolean bOpen) {

if (bOpen) {

if (getApState()) {

return;

}

manager.setWifiEnabled(false);

}

try {

Method method = manager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);

method.invoke(manager, getApConfiguration()/*采用默认的配置,则置为null*/, bOpen);

} catch (Exception e) {

e.printStackTrance();

}

}

源码已上传到github:/aqi/WifiAssistant

发表评论:

昵称

邮件地址 (选填)

个人主页 (选填)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。