How change the device name in WiFi direct p2p?
Asked Answered
W

3

10

Is it possibile to change the device name of WiFi direct through the code? I've tried to:

private WifiP2pDevice wDevice;
wDevice.deviceName = "newName";

But, obviously it doesn't work. Any idea?!

Wembley answered 20/7, 2014 at 15:25 Comment(1)
Is this question similar to the following? #27315698 As in you want to change the device name of the one broadcasting?Idiotic
K
10

Following code use Reflection api of Java,It is less preferrable due to lack of efficiency but Android does not provide another way so, You can use it works with charm :

    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();
    }
Konstanz answered 14/11, 2014 at 13:55 Comment(2)
could you possibly elaborate and explain why this code would hep out, and what it does? ThanksBotvinnik
Could you please answer this question https://mcmap.net/q/1162473/-how-to-wifip2pdevice-devicename-for-current-device/9640177 - how to get current device nameRecitativo
B
9

This code works for me.

WifiP2pManager manager;
WifiP2pManager.Channel channel;
    try {
       manager = (WifiP2pManager)getSystemService(Context.WIFI_P2P_SERVICE);
        channel = manager.initialize(this, getMainLooper(), new WifiP2pManager.ChannelListener() {
            @Override
            public void onChannelDisconnected() {
                manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
            }
        });
        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() {
                Log.d("setDeviceName succeeded", "true");
            }

            @Override
            public void onFailure(int reason) {
                Log.d("setDeviceName failed", "true");
            }
        };
        setDeviceName.invoke(manager, arglist);

    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
Bithynia answered 14/10, 2015 at 13:47 Comment(4)
Tested it on Android 6.0.1 today - works great. Thank you.Wadmal
it works, are there any risk for security? what about the intent broadcast with WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION?Hypothec
Also tested on 6.0 today. works perfectly! big thanks.Basically
Does this still work on latest android versions?Afroamerican
A
0
if (action.equals(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION))
{
    WifiP2pDevice device = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
    // device.deviceName
}

this code works

Reference- How to get wifi direct devices name from WifiP2pDeviceList

Albumenize answered 30/3, 2019 at 20:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.