前言
现在有一个需求,需要能够知道手机端网络类型,如果是WiFi则去使用局域网通信。在这里我选用了connectivity_plus这个库
connectivity_plus的平台支持
安卓 | iOS系统 | 苹果系统 | 网络 | Linux | 视窗 |
---|---|---|---|---|---|
✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
connectivity_plus的API
API | 描述 |
---|---|
checkConnectivity | 检查设备的连接状态 |
onConnectivityChanged | 设备的连接状态更改时激发。 在iOS上,WiFi状态发生变化时,连接状态可能不会更新,这是一个只影响模拟器的已知问题 |
示例代码
void scanType() async {
final connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
// I am connected to a mobile network.
print("移动网络");
} else if (connectivityResult == ConnectivityResult.wifi) {
// I am connected to a wifi network.
print("wifi网络");
} else if (connectivityResult == ConnectivityResult.ethernet) {
// I am connected to a ethernet network.
print("以太网");
} else if (connectivityResult == ConnectivityResult.vpn) {
// I am connected to a vpn network.
// Note for iOS and macOS:
// There is no separate network interface type for [vpn].
// It returns [other] on any device (also simulator)
print("vpn网络");
} else if (connectivityResult == ConnectivityResult.bluetooth) {
// I am connected to a bluetooth.
print("蓝牙");
} else if (connectivityResult == ConnectivityResult.other) {
// I am connected to a network which is not in the above mentioned networks.
print("其他网络");
} else if (connectivityResult == ConnectivityResult.none) {
// I am not connected to any network.
print("无网络");
}
}
changeScan() {
StreamSubscription subscription = Connectivity()
.onConnectivityChanged
.listen((ConnectivityResult result) {
print("改变了情况:$result");
});
}
标签:network,ConnectivityResult,am,网络,获取,connected,print,connectivityResult,Flutter
From: https://blog.csdn.net/nonagontech/article/details/143027424