Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: getWifiName different behaviour on Android and iOs #1326

Closed
7 tasks done
pietrop88 opened this issue Nov 8, 2022 · 2 comments
Closed
7 tasks done

[Bug]: getWifiName different behaviour on Android and iOs #1326

pietrop88 opened this issue Nov 8, 2022 · 2 comments
Labels
bug Something isn't working triage

Comments

@pietrop88
Copy link

pietrop88 commented Nov 8, 2022

Platform

Android 12 and iOs 16.1

Plugin

network_info_plus

Version

3.0.1

Flutter SDK

3.3.6

Info

The method getWifiName behaves differently on Android and iOs:

  • on Android it returns the wifi name as a string that starts and ends with the " char.
  • on iOs it returns the wifi name without any starting or ending chars.

The following table resumes the behaviour of the method with different package versions (wifi name is WIFIxGUEST without "):

package verison android iOs
2.1.3 WIFIxGUEST WIFIxGUEST
2.1.4 "WIFIxGUEST" WIFIxGUEST
2.1.4+1 "WIFIxGUEST" WIFIxGUEST
2.2.0 "WIFIxGUEST" WIFIxGUEST
2.3.0 null WIFIxGUEST
2.3.1 null WIFIxGUEST
2.3.2 null WIFIxGUEST
3.0.0 "WIFIxGUEST" WIFIxGUEST
3.0.1 "WIFIxGUEST" WIFIxGUEST

Expected behaviour

package verison android iOs
next release WIFIxGUEST WIFIxGUEST

Steps to reproduce

  1. Clone network_info_plus example project https://github.com/fluttercommunity/plus_plugins/tree/main/packages/network_info_plus/network_info_plus/example
  2. Delete in pubspec.yaml all the dependency_overrides section and in the dependencies section change network_info_plus: ^3.0.0.
  3. Do flutter clean and flutter pub get and then run the app again.
  4. Grant location permission make sure that you are connected to a WiFi,

Android:

img_android

iOs:

img_ios

Code Sample

// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: public_member_api_docs

import 'dart:async';
import 'dart:developer' as developer;
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:network_info_plus/network_info_plus.dart';

// Sets a platform override for desktop to avoid exceptions. See
// https://flutter.dev/desktop#target-platform-override for more info.
void _enablePlatformOverrideForDesktop() {
  if (!kIsWeb && (Platform.isWindows || Platform.isLinux)) {
    debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
  }
}

void main() {
  _enablePlatformOverrideForDesktop();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, this.title}) : super(key: key);

  final String? title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _connectionStatus = 'Unknown';
  final NetworkInfo _networkInfo = NetworkInfo();

  @override
  void initState() {
    super.initState();
    _initNetworkInfo();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('NetworkInfoPlus example'),
      ),
      body: Center(
          child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          const Text(
            'Network info',
            style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 16),
          Text(_connectionStatus),
        ],
      )),
    );
  }

  Future<void> _initNetworkInfo() async {
    String? wifiName,
        wifiBSSID,
        wifiIPv4,
        wifiIPv6,
        wifiGatewayIP,
        wifiBroadcast,
        wifiSubmask;

    try {
      if (!kIsWeb && Platform.isIOS) {
        var status = await _networkInfo.getLocationServiceAuthorization();
        if (status == LocationAuthorizationStatus.notDetermined) {
          status = await _networkInfo.requestLocationServiceAuthorization();
        }
        if (status == LocationAuthorizationStatus.authorizedAlways ||
            status == LocationAuthorizationStatus.authorizedWhenInUse) {
          wifiName = await _networkInfo.getWifiName();
        } else {
          wifiName = await _networkInfo.getWifiName();
        }
      } else {
        wifiName = await _networkInfo.getWifiName();
      }
    } on PlatformException catch (e) {
      developer.log('Failed to get Wifi Name', error: e);
      wifiName = 'Failed to get Wifi Name';
    }

    try {
      if (!kIsWeb && Platform.isIOS) {
        var status = await _networkInfo.getLocationServiceAuthorization();
        if (status == LocationAuthorizationStatus.notDetermined) {
          status = await _networkInfo.requestLocationServiceAuthorization();
        }
        if (status == LocationAuthorizationStatus.authorizedAlways ||
            status == LocationAuthorizationStatus.authorizedWhenInUse) {
          wifiBSSID = await _networkInfo.getWifiBSSID();
        } else {
          wifiBSSID = await _networkInfo.getWifiBSSID();
        }
      } else {
        wifiBSSID = await _networkInfo.getWifiBSSID();
      }
    } on PlatformException catch (e) {
      developer.log('Failed to get Wifi BSSID', error: e);
      wifiBSSID = 'Failed to get Wifi BSSID';
    }

    try {
      wifiIPv4 = await _networkInfo.getWifiIP();
    } on PlatformException catch (e) {
      developer.log('Failed to get Wifi IPv4', error: e);
      wifiIPv4 = 'Failed to get Wifi IPv4';
    }

    try {
      if (!Platform.isWindows) {
        wifiIPv6 = await _networkInfo.getWifiIPv6();
      }
    } on PlatformException catch (e) {
      developer.log('Failed to get Wifi IPv6', error: e);
      wifiIPv6 = 'Failed to get Wifi IPv6';
    }

    try {
      if (!Platform.isWindows) {
        wifiSubmask = await _networkInfo.getWifiSubmask();
      }
    } on PlatformException catch (e) {
      developer.log('Failed to get Wifi submask address', error: e);
      wifiSubmask = 'Failed to get Wifi submask address';
    }

    try {
      if (!Platform.isWindows) {
        wifiBroadcast = await _networkInfo.getWifiBroadcast();
      }
    } on PlatformException catch (e) {
      developer.log('Failed to get Wifi broadcast', error: e);
      wifiBroadcast = 'Failed to get Wifi broadcast';
    }

    try {
      if (!Platform.isWindows) {
        wifiGatewayIP = await _networkInfo.getWifiGatewayIP();
      }
    } on PlatformException catch (e) {
      developer.log('Failed to get Wifi gateway address', error: e);
      wifiGatewayIP = 'Failed to get Wifi gateway address';
    }

    setState(() {
      _connectionStatus = 'Wifi Name: $wifiName\n'
          'Wifi BSSID: $wifiBSSID\n'
          'Wifi IPv4: $wifiIPv4\n'
          'Wifi IPv6: $wifiIPv6\n'
          'Wifi Broadcast: $wifiBroadcast\n'
          'Wifi Gateway: $wifiGatewayIP\n'
          'Wifi Submask: $wifiSubmask\n';
    });
  }
}

Logs

It is too long for GitHub comment.

Flutter Doctor

[✓] Flutter (Channel stable, 3.3.6, on Microsoft Windows [Version 10.0.19044.2130], locale it-IT)
    • Flutter version 3.3.6 on channel stable at C:\Users\ppanizza\Documents\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 6928314d50 (2 weeks ago), 2022-10-25 16:34:41 -0400
    • Engine revision 3ad69d7be3
    • Dart version 2.18.2
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at C:\Users\ppanizza\AppData\Local\Android\sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.12+7-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[✓] Visual Studio - develop for Windows (Visual Studio Community 2022 17.3.3)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.3.32825.248
    • Windows 10 SDK version 10.0.19041.0

[✓] Android Studio (version 2021.2)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+7-b1504.28-7817840)

[✓] VS Code (version 1.73.0)
    • VS Code at C:\Users\ppanizza\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.52.0

[✓] Connected device (4 available)
    • DN2103 (mobile)   • CMBY7LDQT4WC6HBI • android-arm64  • Android 12 (API 31)
    • Windows (desktop) • windows          • windows-x64    • Microsoft Windows [Version 10.0.19044.2130]
    • Chrome (web)      • chrome           • web-javascript • Google Chrome 107.0.5304.88
    • Edge (web)        • edge             • web-javascript • Microsoft Edge 107.0.1418.35

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

Checklist before submitting a bug

  • I Google'd a solution and I couldn't find it
  • I searched on StackOverflow for a solution and I couldn't find it
  • I read the README.md file of the plugin
  • I'm using the latest version of the plugin
  • All dependencies are up to date with flutter pub upgrade
  • I did a flutter clean
  • I tried running the example project
@pietrop88 pietrop88 added bug Something isn't working triage labels Nov 8, 2022
@miquelbeltran
Copy link
Member

Unfortunately, that's what the OS returns.

This was already discussed in the past (e.g. here #986 (comment)), and some people tried to fix it, unfortunately the problem is that sometimes is impossible to differentiate between an SSID that indeed contains quotes than when the OS added the quotes.

The decision so far has been that the plugin should be in charge of returning what the OS returns, and the developers should be in charge of processing that data as they wish.

@miquelbeltran
Copy link
Member

Another discussion comment here: #1027 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage
Projects
None yet
Development

No branches or pull requests

2 participants