在android中更改Wifi直连名称

在android中更改Wifi直连名称,android,device,wifi-direct,Android,Device,Wifi Direct,我想在Wifi direct中更改设备名称…到目前为止我已经尝试过了 try { Method m = wpm.getClass().getMethod( "setDeviceName", new Class[] { WifiP2pManager.Channel.class, String.class, WifiP2pManager.ActionListener.class }); m.i

我想在Wifi direct中更改设备名称…到目前为止我已经尝试过了

 try {
    Method m = wpm.getClass().getMethod(
            "setDeviceName",
            new Class[] { WifiP2pManager.Channel.class, String.class,
                    WifiP2pManager.ActionListener.class });

    m.invoke(WifiP2pManager wifimngr,WifiP2pManager.Channel wifichannel, new_name, new WifiP2pManager.ActionListener() {
        public void onSuccess() {
            //Code for Success in changing name
        }

        public void onFailure(int reason) {
            //Code to be done while name change Fails
        }
    });
} catch (Exception e) {

    e.printStackTrace();
}

但这对我不起作用……有没有办法实现这个目标

看一看,我已经在wiko darkfull上测试了它的工作

更改WifiDirect名称-Android Java

这对我来说很有用,无论如何,我不建议使用反射来访问WifiP2pManager中的隐藏api

Android操作系统:5.1.1

public void setDeviceName(String devName) {
    try {
        Class[] paramTypes = new Class[3];
        paramTypes[0] = WifiP2pManager.Channel.class;
        paramTypes[1] = String.class;
        paramTypes[2] = WifiP2pManager.ActionListener.class;
        Method setDeviceName = manager.getClass().getMethod("setDeviceName", paramTypes);
        setDeviceName.setAccessible(true);
        Object arglist[] = new Object[3];
        arglist[0] = channel;
        arglist[1] = devName;
        arglist[2] = new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                updateLog("setDeviceName: onSuccess");
            }

            @Override
            public void onFailure(int reason) {
                updateLog("setDeviceName: onSuccess");
            }
        };
        setDeviceName.invoke(manager, arglist);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}

请参阅:

您在哪个设备上测试此代码?