如何使用Flutter获取Android热点状态与信息?
Flutter 获取设备热点信息的解决方案
一、现有Flutter包情况
目前没有维护活跃、兼容性良好的纯Flutter包可直接获取设备热点信息:
connectivity_plus仅能识别WiFi、移动数据等基础网络类型,不支持热点状态与详细信息查询;wifi_iot已废弃,存在严重兼容性问题,不推荐使用。
二、原生实现方案
由于Flutter生态没有合适的替代包,需要通过原生代码+MethodChannel实现跨平台通信:Android端确实需要编写Kotlin(或Java)代码,iOS端则需要Swift/Objective-C代码。
Android端(Kotlin实现)
- 添加权限
在android/app/src/main/AndroidManifest.xml中添加所需权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 若需修改热点状态,额外添加: --> <!-- <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> -->
- 实现MethodChannel通信
在MainActivity.kt中编写原生方法,调用Android系统的WifiManagerAPI获取热点信息:
import android.content.Context import android.net.wifi.WifiManager 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.yourapp/hotspot" override fun configureFlutterEngine(flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result -> when (call.method) { "getHotspotInfo" -> { val wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager val isHotspotEnabled = wifiManager.isWifiApEnabled val hotspotConfig = wifiManager.wifiApConfiguration val hotspotInfo = mutableMapOf<String, Any?>( "isEnabled" to isHotspotEnabled, "ssid" to hotspotConfig?.SSID, "password" to hotspotConfig?.preSharedKey ) result.success(hotspotInfo) } else -> result.notImplemented() } } } }
- Flutter端调用
创建工具类调用原生方法:
import 'package:flutter/services.dart'; class HotspotUtils { static const MethodChannel _channel = MethodChannel('com.yourapp/hotspot'); static Future<Map<String, dynamic>?> getHotspotInfo() async { try { final Map<dynamic, dynamic>? result = await _channel.invokeMethod('getHotspotInfo'); return result?.cast<String, dynamic>(); } on PlatformException catch (e) { print("获取热点信息失败: ${e.message}"); return null; } } }
iOS端(Swift实现)
iOS需使用NEHotspotManager框架,且需开启定位权限:
- 在
Info.plist中添加定位权限描述:
<key>NSLocationWhenInUseUsageDescription</key> <string>需要定位权限以获取热点信息</string>
- 实现MethodChannel,调用
NEHotspotManager获取热点状态(注:iOS仅能获取热点是否开启,无法直接获取SSID和密码):
import UIKit import Flutter import NetworkExtension @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { private let CHANNEL = "com.yourapp/hotspot" override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { let controller : FlutterViewController = window?.rootViewController as! FlutterViewController let channel = FlutterMethodChannel(name: CHANNEL, binaryMessenger: controller.binaryMessenger) channel.setMethodCallHandler { [weak self] (call: FlutterMethodCall, result: FlutterResult) in guard call.method == "getHotspotInfo" else { result(FlutterMethodNotImplemented) return } let hotspotManager = NEHotspotManager.shared let isEnabled = hotspotManager.isEnabled result(["isEnabled": isEnabled]) } GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } }
总结
目前必须通过原生代码实现热点信息的获取,Android端需要编写Kotlin代码,iOS端需要Swift代码,借助MethodChannel完成Flutter与原生的通信。
内容的提问来源于stack exchange,提问作者Muazzam Soomro
相关产品推荐
相关产品推荐

