'Cannot use WifiP2pManager.setDeviceName on Android 11 (Wi-Fi Direct)

My team and I are working with Wi-Fi Direct technology on Android devices. Until now, the used devices were on Android 8, 9 and 10. We were able to change the Wifi P2P device name of the devices via the WifiP2pManager.setDeviceName method.

Unfortunately, from Android 11 it is impossible to call this method without system permissions.

I came here to ask you if there is a solution to change the WifiP2p device name of non-rooted Android 11 devices programmatically.

If not, is there an alternative to Wi-Fi Direct (excepted Bluetooth) supported from Android 8 on which you can start a connection between two (or more) devices, communicate and send files programmatically without a connection to the internet?

Thank you



Solution 1:[1]

Maybe now it's too late but just in case : We had to use reflection to change our device names. We're using this implementation :

public void setDeviceName(String name) {

        if(name.length() > 32) { // Name size limit is 32 chars.
            name = name.substring(0, 32);
        }

        Class[] paramTypes = new Class[3];
        paramTypes[0] = WifiP2pManager.Channel.class;
        paramTypes[1] = String.class;
        paramTypes[2] = WifiP2pManager.ActionListener.class;
        Object[] argList = new Object[3];
        argList[0] = mChannel; // Your current channel
        argList[1] = name;
        argList[2] = new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                Log.d(TAG, "Wifi name successfully set to " + name);
            }

            @Override
            public void onFailure(int reason) {
                Log.e(TAG, "Device name set failed: reason = " + reason);
            }
        };

        try {
            Method setDeviceNameMethod = WifiP2pManager.class.getMethod("setDeviceName", paramTypes);
            setDeviceNameMethod.setAccessible(true);
            setDeviceNameMethod.invoke(mWifiP2pMgr, argList);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException |
                IllegalArgumentException e) {
            Log.e(TAG, "Exception while trying to set device name.\n" + e.toString());
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tabouley