user1519665
user1519665

Reputation: 511

Android naming a P2P device

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).

Upvotes: 1

Views: 324

Answers (1)

Bartek Lipinski
Bartek Lipinski

Reputation: 31448

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).

Upvotes: 1

Related Questions