code_geek
code_geek

Reputation: 163

Change the Wifi Direct name in android

I want change the device name in Wifi direct....So far i have tried

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

But it doesn't work for me.....Is there any idea how can I achieve this goal?

Upvotes: 3

Views: 1703

Answers (2)

Googlian
Googlian

Reputation: 6723

Changing WifiDirect Name - Android Java

This is what worked for me, anyhow I don't recommend using reflection to access hidden APIs in the WifiP2pManager.

Worked Android OS: 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();
    }
}

Refer: https://gist.github.com/aslamanver/39aa668b3073bc4f52aeb09b5a7ea3be

Upvotes: 0

letroll
letroll

Reputation: 1068

Have a look at https://github.com/spawrks/WifiDirectNameChanger I have tested it on a wiko darkfull it's work

Upvotes: 1

Related Questions