6

I am trying to connect two devices using Wifi Direct, but i want to implement programmatically not by user initiated.

And for that i have to change Device's WifiDirect's name like below picture :

enter image description here

Now discover peers using following methods :

wifiP2pManager.discoverPeers(channel,
                new WifiP2pManager.ActionListener() {

                    @Override
                    public void onSuccess() {
                        Log.d(TAG, "onSuccess");
                    }

                    @Override
                    public void onFailure(int reason) {
                        Log.d(TAG, "onFailure");
                    }
                });

Connect to particular peer via following code :

public static void connectPeer(WifiP2pDevice device,
        WifiP2pManager manager, Channel channel, final Handler handler) {

    WifiP2pConfig config = new WifiP2pConfig();
    config.groupOwnerIntent = 15;
    config.deviceAddress = device.deviceAddress;
    config.wps.setup = WpsInfo.PBC;

    manager.connect(channel, config, new ActionListener() {

        @Override
        public void onSuccess() {

        }

        @Override
        public void onFailure(int reason) {

        }
    });
}

But i dont know how to change device name for Wi-Fi Direct?

exception
  • 321
  • 2
  • 4
  • 10

1 Answers1

7

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

public void setDeviceName(String devName) {
    try {
        Class[] paramTypes = new Class[3];
        paramTypes[0] = Channel.class;
        paramTypes[1] = String.class;
        paramTypes[2] = 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 ActionListener() {

            @Override
            public void onSuccess() {
                LOG.debug("setDeviceName succeeded");
            }

            @Override
            public void onFailure(int reason) {
                LOG.debug("setDeviceName failed");
            }
        };

        setDeviceName.invoke(manager, arglist);

    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }

}
Silvia H
  • 8,097
  • 7
  • 30
  • 33
  • 4
    manager is WifiP2pManager, and channel is WifiP2pManager.Channel which we use to connect our app to the Wifi p2p framework. by the way if your tried to know the basics about WifiP2p or even read a single tutorial about how to begin, you'd know what manager and channel are, Please refer to this: http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html – Silvia H Sep 30 '15 at 14:39
  • @David these two can be defined as follows `WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); Channel channel = mManager.initialize(this, getMainLooper(), null);` and don't forget import these `import android.net.wifi.p2p.WifiP2pManager;` `import android.net.wifi.p2p.WifiP2pManager.Channel;` – İlter Kağan Öcal Mar 05 '17 at 13:30