无法在 Android 11 (Wi-Fi Direct) 上使用 WifiP2pManager.setDeviceName

问题描述

我和我的团队正在Android 设备上使用Wi-Fi Direct 技术。到目前为止,所使用的设备都在 Android 8、9 和 10 上。我们能够通过 wifip2pManager.setDeviceName 方法更改设备的 Wifi P2P 设备名称

遗憾的是,从 Android 11 开始,如果没有系统权限,就无法调用方法

我是来询问您是否有解决方案可以以编程方式更改 非 root 的 Android 11 设备的 wifip2p 设备名称

如果没有,是否有 Android 8 支持的 Wi-Fi Direct(蓝牙除外)的替代方案,您可以在其上启动两个(或多个)设备之间的连接,以编程方式进行通信和发送文件没有连接到互联网?

谢谢

解决方法

也许现在为时已晚,但以防万一: 我们不得不使用反射来更改我们的设备名称。 我们正在使用这个实现:

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());
        }
    }