如何通过Flutter设置Android热点?求Kotlin完整实现代码示例
Flutter 设置 Android Wi-Fi 热点(Method Channel 实现)
由于wifi_iot包多数功能已被弃用,直接通过Method Channel调用Android原生API是更可靠的方案,以下是完整的实现代码:
Flutter 端代码
在Dart文件中创建Method Channel,用于和原生层通信:
import 'package:flutter/services.dart'; class HotspotManager { static const MethodChannel _channel = MethodChannel('com.example/hotspot'); // 调用原生方法开启热点,传入SSID和密码 static Future<bool> startHotspot(String ssid, String password) async { try { final bool result = await _channel.invokeMethod( 'startHotspot', {'ssid': ssid, 'password': password}, ); return result; } on PlatformException catch (e) { print("开启热点失败: ${e.message}"); return false; } } }
Android 端 Kotlin 代码
在android/app/src/main/kotlin/[你的包路径]/MainActivity.kt中实现Method Channel的处理逻辑,通过反射调用Android隐藏的热点API:
import android.content.Context import android.net.wifi.WifiConfiguration import android.net.wifi.WifiManager import android.os.Build import androidx.annotation.NonNull import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.engine.FlutterEngine import io.flutter.plugin.common.MethodChannel class MainActivity : FlutterActivity() { private val CHANNEL = "com.example/hotspot" override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result -> when (call.method) { "startHotspot" -> { val ssid = call.argument<String>("ssid") val password = call.argument<String>("password") if (ssid.isNullOrEmpty() || password.isNullOrEmpty()) { result.error("参数错误", "SSID和密码不能为空", null) return@setMethodCallHandler } val success = startWifiHotspot(ssid, password) result.success(success) } else -> result.notImplemented() } } } private fun startWifiHotspot(ssid: String, password: String): Boolean { val wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager // 开启热点前先关闭当前Wi-Fi连接 if (wifiManager.isWifiEnabled) { wifiManager.isWifiEnabled = false } return try { val hotspotConfig = WifiConfiguration().apply { this.SSID = ssid preSharedKey = password allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN) allowedProtocols.set(WifiConfiguration.Protocol.RSN) allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK) allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP) allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP) } // 适配Android版本差异,调用对应的隐藏API if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // Android 10+ 分两步:配置热点 + 启动热点 val setConfigMethod = wifiManager.javaClass.getMethod("setWifiApConfiguration", WifiConfiguration::class.java) setConfigMethod.invoke(wifiManager, hotspotConfig) val startMethod = wifiManager.javaClass.getMethod("startWifiAp") startMethod.invoke(wifiManager) as Boolean } else { // Android 9及以下直接设置并启动 val setEnabledMethod = wifiManager.javaClass.getMethod("setWifiApEnabled", WifiConfiguration::class.java, Boolean::class.javaPrimitiveType) setEnabledMethod.invoke(wifiManager, hotspotConfig, true) as Boolean } true } catch (e: Exception) { e.printStackTrace() false } } }
权限配置
在android/app/src/main/AndroidManifest.xml中添加必要权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- Android 10+ 需要WRITE_SETTINGS权限,需引导用户手动授权 --> <uses-permission android:name="android.permission.WRITE_SETTINGS" tools:ignore="ProtectedPermissions" />
注意事项
- Android 10及以上版本,
WRITE_SETTINGS权限无法自动获取,需要在Flutter端引导用户跳转到系统设置页面开启权限。 - 反射调用的是Android系统隐藏API,后续系统版本更新可能导致方法失效,需注意兼容性测试。
- 部分定制ROM可能限制热点的开启,需针对目标设备做适配。
内容的提问来源于stack exchange,提问作者Amr Hassan
相关产品推荐
相关产品推荐

