Android rename device name for wifi-direct

I am trying to connect two devices using Wifi Direct, but I want to implement it programmatically not at the initiative of the user.

And for this I need to change the name of the WifiDirect device, as shown below:

enter image description here

Now find the peers using the 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 a specific peer using the 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 do not know how to change the device name for Wi-Fi Direct?

+5
source share
1 answer

this is what worked for me, although I do not recommend using reflection to access the hidden APIs in 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(); } } 
+4
source

Source: https://habr.com/ru/post/1208423/


All Articles