fluttercommunity / plus_plugins

Flutter Community Plus Plugins
BSD 3-Clause "New" or "Revised" License
1.55k stars 938 forks source link

[Bug]: Inquiry Regarding network_info_plus Sample Project - WiFi Name and BSSID Returning Null on iOS #3198

Open SD19767 opened 2 weeks ago

SD19767 commented 2 weeks ago

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

miquelbeltran commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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.