1

I have the similar question with this post Android WiFi Direct device details However, it seem din't get any solution from that post.

Is it any method can used to set the wifi-direct name that similar with function of "setName()" or "setServiceName" in bluetoohAdapter & NsdServiceInfo.

Community
  • 1
  • 1
user2301281
  • 757
  • 2
  • 12
  • 27

2 Answers2

3

I know this is late. But you can try this. Here you have to pass WifiP2pManager instance alogn with WifiP2pManager.Channel instance and your custom name that you want to set.

This worked for me, Hope it will help you too.

    /*
    Set WifiP2p Device Name
     */
    public void setDeviceName(WifiP2pManager manager, WifiP2pManager.Channel channel, String deviceName){
        //set device name programatically
        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] = deviceName;
            arglist[2] = new WifiP2pManager.ActionListener() {

                @Override
                public void onSuccess() {
                }

                @Override
                public void onFailure(int reason) {
                }
            };

            setDeviceName.invoke(manager, arglist);

        }
        catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
Anuj Sharma
  • 4,294
  • 2
  • 37
  • 53
1

You can rename the device WiFi Direct name using reflection. Check the solution here: Android rename device's name for wifi-direct

Additionally, there is a file called p2p_supplicant.conf in the /data/misc/wifi/p2p_supplicant.conf that contains a field where the WiFi Direct name is specified. You can edit it using my answer on with a slight adjustment: editing a value in the file p2p_supplicant.conf which is located on /root/data/misc/wifi/p2p_supplicant.conf

Just a note that you can manually change the name of the WiFi Direct device on the phone without the need for an App. There is a rename device option in WiFi->Wifi Direct on Android phones.

Hope this helps. Let me know if you have any questions.

Community
  • 1
  • 1
Ziad Halabi
  • 964
  • 11
  • 31
  • Thanks. May I know what is mean by using "reflection"? Cause the name mentioned "don't recommend using reflection to access hidden APIs". For the second method, is it only available for rooted device? Both the client & server sides have to rooted to use this method? – user2301281 May 13 '15 at 00:39
  • 1
    Reflection is the ability to dynamically call classes, methods, etc at Runtime. One basic use of Reflection is to access data fields or methods of class because they are declared private. (Take a look at: http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful) . For the second method, the phone that needs to change its name needs to be rooted. – Ziad Halabi May 13 '15 at 12:24
  • Thank for your answer. It's solve my problem. Really thanks for you help and patient to explain for me. =D – user2301281 May 13 '15 at 13:35