I’m creating a flutter app to connect to a IOT speaker device (which has wifi). I need to connect to it and upload audio files using GET/POST APIs. If I connect my phone to speaker’s wifi using the wifi option already present in the android phone, I am able to upload the file through the POST request. If i connect to the speaker wifi through the Flutter app using wiflutter package, I’m not able to do the POST request.
package:wiflutter/wiflutter.dart’; — It connects to wifi, but it doesnt allow me to do GET/POST requests.
I have tried many other packages as well. They have not been updated for long time.
Below are the packages that i tried:
‘package:wifi_scan/wifi_scan.dart’; — only scans for wifi
package:wifi/wifi.dart’; — it seems to be outdated package
‘package:wifi_connector/wifi_connector.dart’; — I’m getting an error that the package uses outdated Kotlin plugin version. Cant seem to workaround this.
I’m relatively new to flutter. Any help would be appreciated!
Code for connecting to wifi:
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:nspeak/RecordingPage.dart';
import 'package:wifi_scan/wifi_scan.dart';
import 'package:wiflutter/wiflutter.dart';
import 'package:wiflutter/enums/enterprise_certificate_enum.dart';
// import 'package:wifi_iot/wifi_iot.dart'; -- doesnt support Null safety, so cant use it.
// import 'package:wifi_connector/wifi_connector.dart'; --it uses older Kotlin plugin version. So cant use it.
// import 'package:wifi_configuration/wifi_configuration.dart'; -- doesnt support Null safety, so cant use it.
// import 'package:wifi/wifi.dart'; -- doesnt support Null safety, so cant use it.
scanNspeakwifi() async{
resultcanstarscan = await WiFiScan.instance.canStartScan();
// print("passed canstartscan : $resultcanstarscan");
if(resultcanstarscan == CanStartScan.yes){
scansuccess = await WiFiScan.instance.startScan();
// print("scan success: $scansuccess");
if (mounted){
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
padding: EdgeInsets.only(bottom: 25,),
duration: Durations.extralong4,
backgroundColor: Colors.yellow,
content: Center(child:Text('Scanning in progress', style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold, color: Colors.black),),
)),
);
}
}
else {
if(mounted){
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
padding: EdgeInsets.only(bottom: 25,left: 30, right: 30),
duration: Durations.extralong4,
backgroundColor: Colors.red,
content: Center( child: Text('Cannot start scan. Check if all necessary permissions are given!', style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),),
),
)
);
}
}
cangetresults = await WiFiScan.instance.canGetScannedResults();
// print("Passed cangetresults $cangetresults");
if(cangetresults == CanGetScannedResults.yes ){
scanresults = await WiFiScan.instance.getScannedResults();
if(mounted){
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
padding: EdgeInsets.only(bottom: 25, left: 20, right: 20),
duration: Durations.extralong4,
backgroundColor: Colors.green,
content: Center(child: Text('Scan completed! Click on "Get Results" to view nearby Nspeak device', style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),)),
),
);
}
}
else {
if(mounted){
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
padding: EdgeInsets.only(bottom: 25,left: 20, right: 20),
duration: Durations.extralong4,
backgroundColor: Colors.red,
content: Center(child: Text('No scan result available. Check if all necessary permissions are given!', style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),)),
),
);
}
}
}
getNspeakwifi() async {
var resultsavail = await WiFiScan.instance.onScannedResultsAvailable;
var checkresult = await resultsavail.isEmpty;
if (!checkresult){
setState(() {
wifiNetworks = scanresults;
_shownetworks = true;
});
}
else{
if(mounted){
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
padding: EdgeInsets.only(bottom: 25, left: 20, right: 20),
duration: Durations.extralong4,
backgroundColor: Colors.red,
content: Center(child: Text('No Nspeak devices nearby! ', style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),)),
),
);
}
}
}
Widget displayNspeak(){
var value;
nspeakdevices = []; /*Initialzied the variable here again because everytime "Get results" is clicked, it should return a new list. It should not go on adding the Wifi ACcess points to the same list*/
for (int i=0; i<wifiNetworks.length;){
var entry = wifiNetworks[i];
if(entry.ssid.contains("NSPEAK")){
value = entry.ssid;
print("reached List");
nspeakdevices.add(value);
}
i++;
}
if (nspeakdevices.isNotEmpty){
return ListView.builder(itemCount: nspeakdevices.length,
itemBuilder: (BuildContext context, int index){
var nspeakvalue = nspeakdevices[index];
return Row(
children: [
Expanded(child: ListTile(title: Text(nspeakvalue))),
TextButton(onPressed: () async {
var con = await wiflutter.connect(
ssid: nspeakvalue,
password: '********',
);
},
child: Text("Connect", style: TextStyle(fontSize: 10),)
),
TextButton(onPressed: () async {
var con = await wiflutter.disconnect();
},
child: Text("Disconnect", style: TextStyle(fontSize: 10),)),
],
);
}
);
}
else {
return const Text("No Nspeak devices nearby");
}
}
}
Code for uploading the audio file:
fileupload() async{
try {
var headersget = {'Authorization': 'Authorization text'};
requestget = http.Request('GET', Uri.parse('http://192.168.4.1/getlogin'));
requestget.headers.addAll(headersget);
http.StreamedResponse responseget = await requestget.send();
// print(responseget.statusCode);
if (responseget.statusCode == 200){
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
padding: EdgeInsets.only(bottom: 50, left: 50),
duration: Durations.extralong4,
backgroundColor: Colors.white,
content: Text('Login Successfull!', style: TextStyle(fontSize: 15, color: Colors.black , fontWeight: FontWeight.bold),),
),);
}
else{
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
padding: EdgeInsets.only(bottom: 50, left: 50),
duration: Durations.extralong4,
// backgroundColor: Colors.white,
content: Text('Login failed', style: TextStyle(fontSize: 15, color: Colors.black, fontWeight: FontWeight.bold),),
),);
}
var headerspost = {Content-Type': 'multipart/form-data;',
'Authorization': 'authorization text'};
requestpost = http.MultipartRequest('POST', Uri.parse('http://192.168.4.1/upload'));
// print("parse successfull");
requestpost.files.add(await http.MultipartFile.fromPath('mp3', audiofilepath));
print("file add successfull");
requestpost.headers.addAll(headerspost);
print("headers add successfull");
http.StreamedResponse responsepost = await requestpost.send();
print("POST request sent");
// print(requestpost.body);
// print(responsepost.statusCode);
// print(responsepost.headers["Location"]);
if (responsepost.statusCode == 200 || responsepost.statusCode == 302){
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
padding: EdgeInsets.only(bottom: 50, left: 50),
duration: Durations.extralong4,
backgroundColor: Colors.white,
content: Text('File has been uploaded!', style: TextStyle(fontSize: 15, color: Colors.black , fontWeight: FontWeight.bold),),
),);
}
else{
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
padding: EdgeInsets.only(bottom: 50, left: 50),
duration: Durations.extralong4,
// backgroundColor: Colors.white,
content: Text('File upload failed', style: TextStyle(fontSize: 15, color: Colors.black, fontWeight: FontWeight.bold),),
),);
}
}
catch (e){
print('Error uploading audio: $e');
}