如何解决使用wifi_configuration Flutter
请问如何使用wifi_configuration包连接到开放热点-无线 因为我刚刚找到了一种允许连接到加密无线网络的方法。
WifiConfiguration.connectToWifi("wirelessname","wirelesspassword","packagename");
在WifiConfiguration类中,只有一种方法可用于连接。 是否有其他可以连接到打开的热点的库,或者是否可以通过使用wifi_configuration库来做到这一点? 苹果提到我们只需要传递两个参数,即ssid和packagename
init(ssid: String)
Creates a new hotspot configuration,identified by an SSID,for an open Wi-Fi network.
我重写了connectToWifi方法以仅接收一个参数,但这不起作用。 预先感谢
解决方法
您可以在下面复制粘贴运行完整代码
您可以使用软件包https://pub.dev/packages/wifi_utils
您可以致电Wifi.connection
并提供ssid
和password
代码段
import 'package:wifi/wifi.dart';
...
Future<Null> connection() async {
Wifi.connection(ssid,password).then((v) {
print(v);
});
}
工作演示
完整代码
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:wifi/wifi.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Wifi',theme: new ThemeData(
primarySwatch: Colors.blue,),home: new MyHomePage(),);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _wifiName = 'click button to get wifi ssid.';
int level = 0;
String _ip = 'click button to get ip.';
List<WifiResult> ssidList = [];
String ssid = '',password = '';
@override
void initState() {
super.initState();
loadData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Wifi'),centerTitle: true,body: SafeArea(
child: ListView.builder(
padding: EdgeInsets.all(8.0),itemCount: ssidList.length + 1,itemBuilder: (BuildContext context,int index) {
return itemSSID(index);
},);
}
Widget itemSSID(index) {
if (index == 0) {
return Column(
children: [
Row(
children: <Widget>[
RaisedButton(
child: Text('ssid'),onPressed: _getWifiName,Offstage(
offstage: level == 0,child: Image.asset(
level == 0 ? 'images/wifi1.png' : 'images/wifi$level.png',width: 28,height: 21),Text(_wifiName),],Row(
children: <Widget>[
RaisedButton(
child: Text('ip'),onPressed: _getIP,Text(_ip),TextField(
decoration: InputDecoration(
border: UnderlineInputBorder(),filled: true,icon: Icon(Icons.wifi),hintText: 'Your wifi ssid',labelText: 'ssid',keyboardType: TextInputType.text,onChanged: (value) {
ssid = value;
},icon: Icon(Icons.lock_outline),hintText: 'Your wifi password',labelText: 'password',onChanged: (value) {
password = value;
},RaisedButton(
child: Text('connection'),onPressed: connection,);
} else {
return Column(children: <Widget>[
ListTile(
leading: Image.asset('images/wifi${ssidList[index - 1].level}.png',title: Text(
ssidList[index - 1].ssid,style: TextStyle(
color: Colors.black87,fontSize: 16.0,dense: true,Divider(),]);
}
}
void loadData() async {
Wifi.list('').then((list) {
setState(() {
ssidList = list;
});
});
}
Future<Null> _getWifiName() async {
int l = await Wifi.level;
String wifiName = await Wifi.ssid;
setState(() {
level = l;
_wifiName = wifiName;
});
}
Future<Null> _getIP() async {
String ip = await Wifi.ip;
setState(() {
_ip = ip;
});
}
Future<Null> connection() async {
Wifi.connection(ssid,password).then((v) {
print(v);
});
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。