【问题标题】:Android: How to get current device WiFi-direct nameAndroid:如何获取当前设备 WiFi 直接名称
【发布时间】:2014-10-30 02:23:22
【问题描述】:

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

我检查了WiFiManagerWiFiInfo 等等,但都没有成功。

【问题讨论】:

  • 你能编辑你的问题和显示代码吗?
  • 不确定要添加什么...我要做的就是访问设备的名称,类似于在蓝牙中您会调用BluetoothAdapter.getDefaultAdapter().getName()
  • WifiP2pDevice.deviceName ?见this
  • 如何为自己的设备获取 WifiP2pDevice?
  • 你找到解决办法了吗?

标签: android wifi wifi-direct device-name


【解决方案1】:

在您打开设备上的 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;
 }

【讨论】:

  • 注册WifiP2pBroadcastReceiver时会收到这个电话,不管wifi是否已经开启。
  • device 为空,因此得到NullPointerException
【解决方案2】:

试试这个代码:

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

【讨论】:

    【解决方案3】:

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

     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); 
        }
    

    【讨论】:

      猜你喜欢
      • 2019-05-25
      • 1970-01-01
      • 2019-03-01
      • 2014-08-05
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多