Android: how to get the current WiFi-direct device name

In P2P setup, I understand how to get a different device name, but how to get my own device name? (the one that displays in WiFi mode right below the settings).

I checked WiFiManager , WiFiInfo and more without success.

+8
android wifi wifi-direct
source share
2 answers

After turning on Wi-Fi on your device, it will send the broadcast WIFI_P2P_THIS_DEVICE_CHANGED_ACTION. You can catch this with the broadcast receiver, and you can get the WifiP2pDevice object, i.e. your device.

  @Override public void onReceive(Context context, Intent intent) { WifiP2pDevice device = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE); String thisDeviceName = device.deviceName; } 
+10
source share

Try this code:

 public static String getHostName(String defValue) { try { Method getString = Build.class.getDeclaredMethod("getString", String.class); getString.setAccessible(true); return getString.invoke(null, "net.hostname").toString(); } catch (Exception ex) { return defValue; } } 
0
source share

All Articles