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]: Inquiry Regarding network_info_plus Sample Project - WiFi Name and BSSID Returning Null on iOS #3198

Closed
8 tasks done
SD19767 opened this issue Aug 26, 2024 · 4 comments
Labels
bug Something isn't working triage

Comments

@SD19767
Copy link

SD19767 commented Aug 26, 2024

Platform

iPhone 13 Mini running iOS 17.5.1

Plugin

network_info_plus

Version

4.0.1

Flutter SDK

3.22.4

Steps to reproduce

  1. run sample project in network_info_plus

Code Sample

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';
import 'package:permission_handler/permission_handler.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({super.key});

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

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

  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'),
        elevation: 4,
      ),
      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.isAndroid || Platform.isIOS)) {
        // Request permissions as recommended by the plugin documentation:
        // https://github.com/fluttercommunity/plus_plugins/tree/main/packages/network_info_plus/network_info_plus
        if (await Permission.locationWhenInUse.request().isGranted) {
          wifiName = await _networkInfo.getWifiName();
        } else {
          wifiName = 'Unauthorized to get Wifi Name';
        }
      } 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.isAndroid || Platform.isIOS)) {
        // Request permissions as recommended by the plugin documentation:
        // https://github.com/fluttercommunity/plus_plugins/tree/main/packages/network_info_plus/network_info_plus
        if (await Permission.locationWhenInUse.request().isGranted) {
          wifiBSSID = await _networkInfo.getWifiBSSID();
        } else {
          wifiBSSID = 'Unauthorized to get Wifi BSSID';
        }
      } else {
        wifiName = await _networkInfo.getWifiName();
      }
    } 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 {
      wifiIPv6 = await _networkInfo.getWifiIPv6();
    } on PlatformException catch (e) {
      developer.log('Failed to get Wifi IPv6', error: e);
      wifiIPv6 = 'Failed to get Wifi IPv6';
    }

    try {
      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 {
      wifiBroadcast = await _networkInfo.getWifiBroadcast();
    } on PlatformException catch (e) {
      developer.log('Failed to get Wifi broadcast', error: e);
      wifiBroadcast = 'Failed to get Wifi broadcast';
    }

    try {
      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

image

http://127.0.0.1:9100?uri=http://127.0.0.1:57153/-kg
           bKNFv1EY=/
[+1429 ms] Checking for available port on
io.flutter.plugins.networkInfoExample.alfaTest._dart
VmService._tcp.local
[ +118 ms] Checking for available port on
io.flutter.plugins.networkInfoExample.alfaTest._dart
VmService._tcp.local
[ +423 ms] Checking for available port on
io.flutter.plugins.networkInfoExample.alfaTest
(2)._dartVmService._tcp.local
[+1000 ms] Checking for available port on
io.flutter.plugins.networkInfoExample.alfaTest
(2)._dartVmService._tcp.local
[ +108 ms] Checking for available port on
io.flutter.plugins.networkInfoExample.alfaTest
(2)._dartVmService._tcp.local
[+1892 ms] Checking for available port on
io.flutter.plugins.networkInfoExample.alfaTest
(2)._dartVmService._tcp.local
[  +53 ms] Checking for available port on
io.flutter.plugins.networkInfoExample.alfaTest
(2)._dartVmService._tcp.local
[  +46 ms] Checking for available port on
io.flutter.plugins.networkInfoExample.alfaTest._dart
VmService._tcp.local
[  +56 ms] Checking for available port on
io.flutter.plugins.networkInfoExample.alfaTest._dart
VmService._tcp.local
[+3842 ms] Checking for available port on
io.flutter.plugins.networkInfoExample.alfaTest
(2)._dartVmService._tcp.local
[  +46 ms] Checking for available port on
io.flutter.plugins.networkInfoExample.alfaTest
(2)._dartVmService._tcp.local

Flutter Doctor

[!] Flutter (Channel stable, 3.22.4-0.0.pre.1, on
    macOS 14.5 23F79 darwin-arm64, locale
    zh-Hant-TW)
    ! Upstream repository unknown source is not a
      standard remote.
      Set environment variable "FLUTTER_GIT_URL" to
      unknown source to dismiss this error.
[✓] Android toolchain - develop for Android devices
    (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
[✗] Chrome - develop for the web (Cannot find Chrome
    executable at /Applications/Google
    Chrome.app/Contents/MacOS/Google Chrome)
    ! Cannot find Chrome. Try setting
      CHROME_EXECUTABLE to a Chrome executable.
[✓] Android Studio (version 2023.3)
[✓] VS Code (version 1.92.2)
[✓] Connected device (3 available)
    ! Error: Browsing on the local area network for
      Alvin的iPad. Ensure the device is unlocked and
      attached with a cable or associated with the
      same local area network as this Mac.
      The device must be opted into Developer Mode
      to connect wirelessly. (code -27)
[✓] Network resources

Checklist before submitting a bug

  • I searched issues in this repository and couldn't find such bug/problem
  • 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
@SD19767 SD19767 added bug Something isn't working triage labels Aug 26, 2024
@miquelbeltran
Copy link
Member

Your screenshot does not correspond to what you say in the subject line.

If Permission.locationWhenInUse.request().isGranted is false, the app will display Unauthorized to get Wifi Name which is what you see in the screenshot.

@SD19767
Copy link
Author

SD19767 commented Aug 26, 2024

Hi @miquelbeltran 👋,

Thank you for the clarification.
Sorry for the confusion in my previous message. I just want to confirm that the sample project is expected to display “Unauthorized to get Wifi Name” when Permission.locationWhenInUse.request().isGranted is false. Is that correct?

I understand that the sample project primarily demonstrates how to use the library, so handling permission requests is outside its scope. I’m just making sure I understand the intended behavior.

Thanks again for your support!

@miquelbeltran
Copy link
Member

miquelbeltran commented Aug 26, 2024

Last time I checked, the sample app was handling the permission correctly, but could be that this is now broken due to changes on iOS.

The README lists several requirements in order to get the SSID: https://github.com/fluttercommunity/plus_plugins/blob/main/packages/network_info_plus/network_info_plus/README.md

You can also check with the permission_handler package and see if they explain something regarding managing location permissions.

@vbuberen
Copy link
Collaborator

Closing as the answer is available both in the issue and in the README

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

3 participants