Android 打开热点的工具类
2018-03-15 本文已影响45人
小吉快跑呀
基于kotlin编写,喜欢的朋友可以直接拿去用,
效果演示
start.gif
使用方法
ApManager.openAp(this, "SSID", "`PASSWORD")
/**
* 需要以下权限才能正常使用
* <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
* <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
* <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
*/
class ApManager {
companion object {
/**
* 检查是否允许修改系统设置
*/
fun checkPermission(context: Context): Boolean {
//安卓6以上的特殊处理
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.System.canWrite(context)) {
AlertDialog.Builder(context)
.setMessage("请找到该应用并添加修改系统设置权限以打开热点")
.setPositiveButton("确定", DialogInterface.OnClickListener { _, _ ->
context.startActivity(Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS))
})
.create().show()
return false
}
}
return true
}
/**
* 检测是否打开了热点
*/
fun isApOn(context: Context): Boolean {
val wifimanager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
val method = wifimanager.javaClass.getDeclaredMethod("isWifiApEnabled")
method.isAccessible = true
return method.invoke(wifimanager) as Boolean
}
/**
* 关闭Wi-Fi
*/
fun closeWifi(context: Context) {
val wifimanager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
if (wifimanager.isWifiEnabled) {
wifimanager.isWifiEnabled = false
}
}
/**
* 开启便携热点
* @param context 上下文
* @param SSID 便携热点SSID
* @param password 便携热点密码
*/
fun openAp(context: Context, SSID: String, password: String): Boolean {
if (TextUtils.isEmpty(SSID)) {
return false
}
if(!checkPermission(context)) return false
closeWifi(context)
val wifimanager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
if (wifimanager.isWifiEnabled) {
wifimanager.isWifiEnabled = false
}
val wifiConfiguration = setupConfig(SSID, password)
if (isApOn(context)) {
return false
}
//使用反射开启Wi-Fi热点
val method = wifimanager.javaClass.getMethod("setWifiApEnabled", WifiConfiguration::class.java, Boolean::class.javaPrimitiveType)
method.invoke(wifimanager, wifiConfiguration, true)
return true
}
/**
* 关闭便携热点
*/
fun closeAp(context: Application) {
val wifimanager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
val method = wifimanager.javaClass.getMethod("setWifiApEnabled", WifiConfiguration::class.java, Boolean::class.javaPrimitiveType)
method.invoke(wifimanager, null, false)
}
/**
* 获取开启便携热点后自身热点IP地址
* @param context
*/
fun getHotspotLocalIpAddress(context: Application): String? {
val wifimanager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
val dhcpInfo = wifimanager.dhcpInfo
if (dhcpInfo != null) {
val address = dhcpInfo.serverAddress
return ((address and 0xFF).toString()
+ "." + (address shr 8 and 0xFF)
+ "." + (address shr 16 and 0xFF)
+ "." + (address shr 24 and 0xFF))
}
return null
}
/**
* 设置有密码的热点信息
* @param SSID 便携热点SSID
* @param pwd 便携热点密码
*/
private fun setupConfig(SSID: String, pwd: String): WifiConfiguration? {
if (TextUtils.isEmpty(pwd)) {
return null
}
val config = WifiConfiguration()
config.SSID = SSID
config.preSharedKey = pwd
config.status = WifiConfiguration.Status.ENABLED
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP)
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP)
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK)
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP)
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP)
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN)
return config
}
}
}