10

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

我检查了WiFiManager,WiFiInfo和更多但没有成功。

4

3 回答 3

13

在您打开设备上的 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;
 }
于 2014-11-18T14:51:39.720 回答
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;
    }
}
于 2014-10-30T06:41:37.307 回答
0

只需在您的广播接收器中,

 if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { 
        WifiP2pDevice myDevice =(WifiP2pDevice)intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
        Log.d("Wifi Direct: My Device",myDevice.devicename); 
    }
于 2018-08-11T17:50:00.933 回答