android - What permissions are required to fetch the Wi-Fi SSID in React Native? - Stack Overflow

I have tried all possible permissions in React-Native, but I still can not retrieve the Wi-Fi SSID. I h

I have tried all possible permissions in React-Native, but I still can not retrieve the Wi-Fi SSID. I have tested everything I can, and location permission is definitely granted. I was able to get the Wi-Fi SSID before, but now, no matter what I do, it always returns unknown ssid.

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
useEffect(() => {
    console.log('Location permission status:', locationPermission);
    if (locationPermission) {
        fetchWifiSSID();
        checkWifiConnection();
    } else {
        console.log('Wi-Fi SSID cannot be fetched because location permission is not granted.');
    }
}, [locationPermission]);

const checkWifiConnection = async () => {
    const ipAddress = await NetworkInfo.getIPV4Address();
    console.log('Device IP address:', ipAddress);

    if (!ipAddress) {
        console.log('Device is not connected to Wi-Fi or no network connection.');
    } else {
        console.log('Device is connected to Wi-Fi.');
    }
};

const fetchWifiSSID = async () => {
    try {
        console.log('Attempting to fetch Wi-Fi SSID...');
        const ssid = await WifiManager.getCurrentWifiSSID();
        console.log('Fetched SSID:', ssid);

        if (ssid) {
            setWifiSSID(ssid);
        } else {
            console.log('SSID could not be fetched. No Wi-Fi connection or permissions are missing.');
        }
    } catch (error) {
        console.error('Error occurred while fetching Wi-Fi SSID:', error);
    }
};
LOG  Location permission status: true 
LOG  Attempting to fetch Wi-Fi SSID... 
ERROR  Error occurred while fetching Wi-Fi SSID: Error: Location service is turned off    
LOG  Device IP address: 192.163.4.6
LOG Device is connected to Wi-Fi

I have tried both the react-native-network-info and react-native-wifi-reborn packages, but it still does not work. I am getting unknown ssid even though I have tested everything and have the necessary location permissions.

I have tried all possible permissions in React-Native, but I still can not retrieve the Wi-Fi SSID. I have tested everything I can, and location permission is definitely granted. I was able to get the Wi-Fi SSID before, but now, no matter what I do, it always returns unknown ssid.

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
useEffect(() => {
    console.log('Location permission status:', locationPermission);
    if (locationPermission) {
        fetchWifiSSID();
        checkWifiConnection();
    } else {
        console.log('Wi-Fi SSID cannot be fetched because location permission is not granted.');
    }
}, [locationPermission]);

const checkWifiConnection = async () => {
    const ipAddress = await NetworkInfo.getIPV4Address();
    console.log('Device IP address:', ipAddress);

    if (!ipAddress) {
        console.log('Device is not connected to Wi-Fi or no network connection.');
    } else {
        console.log('Device is connected to Wi-Fi.');
    }
};

const fetchWifiSSID = async () => {
    try {
        console.log('Attempting to fetch Wi-Fi SSID...');
        const ssid = await WifiManager.getCurrentWifiSSID();
        console.log('Fetched SSID:', ssid);

        if (ssid) {
            setWifiSSID(ssid);
        } else {
            console.log('SSID could not be fetched. No Wi-Fi connection or permissions are missing.');
        }
    } catch (error) {
        console.error('Error occurred while fetching Wi-Fi SSID:', error);
    }
};
LOG  Location permission status: true 
LOG  Attempting to fetch Wi-Fi SSID... 
ERROR  Error occurred while fetching Wi-Fi SSID: Error: Location service is turned off    
LOG  Device IP address: 192.163.4.6
LOG Device is connected to Wi-Fi

I have tried both the react-native-network-info and react-native-wifi-reborn packages, but it still does not work. I am getting unknown ssid even though I have tested everything and have the necessary location permissions.

Share Improve this question edited Feb 24 at 20:19 President James K. Polk 42.1k30 gold badges110 silver badges146 bronze badges asked Feb 23 at 10:59 FrontmirFrontmir 231 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I finally found the solution, it was a simple mistake on my part. For Android, it's not enough to just grant location permission to the app; the device's location services must be continuously enabled. As for iOS, you need to add the "Access WiFi Information" capability to your Xcode project under the "Signing & Capabilities" section.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745157162a4614193.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信