2014-10-30 82 views
8

在P2P设置中,我了解如何获取其他设备的名称,但我如何获取自己的设备名称? (在WiFi-direct下显示的设置)。Android:如何获取当前设备WiFi-direct名称

我检查了WiFiManagerWiFiInfo,以及更多没有成功。

+0

你能编辑你的问题和显示代码吗? – Raptor 2014-10-30 02:26:40

+0

不知道要添加什么...我想要做的就是访问设备的名称,类似于蓝牙,你可以调用'BluetoothAdapter.getDefaultAdapter()。getName()' – 2014-10-30 02:34:07

+0

'WifiP2pDevice.deviceName'?请参阅[本](http://developer.android.com/training/connect-devices-wirelessly/nsd-wifi-direct.html) – Raptor 2014-10-30 03:07:29

回答

0

试试这个代码:

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; 
    } 
} 
+1

您应该说明您的代码。 – Raptor 2014-10-31 02:16:59

10

你打开设备上的WiFi后,是发送一个广播WIFI_P2P_THIS_DEVICE_CHANGED_ACTION。你可以用广播接收器来捕捉这个,你可以得到一个WifiP2pDevice对象,这就是你的设备。

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

如果wifi已经打开,该怎么办? – 2015-01-27 13:04:29

+1

当您注册WifiP2pBroadcastReceiver时,您将收到此电话,WiFi已经打开并不重要。 – Jonathan 2015-02-11 22:41:05

+0

'device'为null,因此得到'NullPointerException' – Touhid 2017-07-13 14:29:30