1

I was wondering if anybody could shed some light on an issue regarding WifiP2pDevice's name field.

As you may already know, WifiP2pDevice has a devicename field, but no methods to "set" the devicename, only to read it.

As discussed in this post: How change the device name in WiFi direct p2p?

It may be possible to set the device's name via reflection. I know it is bad custom, but since the API doesn't support this and I need this functionality in my application, how would I go about doing this?

As of now, my code:

    try
    {
        method = myManager.getClass().getMethod("setDeviceName",  new Class[] { WifiP2pManager.Channel.class, String.class,
                WifiP2pManager.ActionListener.class });
        method.invoke(myManager, myChannel, getIntent().getStringExtra(NETWORK_NAME));
        Toast.makeText(this, "Name set successful", Toast.LENGTH_SHORT).show();
    }

Currently, the application gets to this point and returns a NoSuchMethodException, implying that the setDeviceName does not exist. Any help/alternatives on java reflection?

EDIT 1: As per the suggestion, I have changed my code to use a WifiP2pManager instead of a WifiP2pDevice to do the getClass() call on, however, the program crashes at the method.invoke line claming that the setDeviceName method expects 3 arguements, but only received 2 (even though mode code as it stands now is sending 3).

Community
  • 1
  • 1
user1519665
  • 511
  • 5
  • 16

1 Answers1

1

Judging from the variable naming from the answer to the question you linked:

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

you should probably call this method (getClass().getMethod(...)) on a WifiP2pManager object and not on the WifiP2pDevice object (I think wpm stands for WifiP2pManager).

Community
  • 1
  • 1
Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
  • I could not make this work either, I'm still tweaking the syntax to see if I make any progress with this insight, however. – user1519665 Feb 22 '15 at 23:07