How to get the deviceName of my device using wifi

2019-09-14 07:55发布

问题:

I'm developing an app based on Google's wifi p2p API and I would like to get the name of the device I'm using (not the peers) but the only way I found to retrieve the name is in case I change it by using something like this:

if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {  
          // this device details has changed(name, connected, etc)
          mApp.mThisDevice = (WifiP2pDevice) intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);

          mApp.mDeviceName = mApp.mThisDevice.deviceName;
          PTPLog.d(TAG, "processIntent: WIFI_P2P_THIS_DEVICE_CHANGED_ACTION " + mApp.mThisDevice.deviceName);
          if( mApp.mHomeActivity != null ){
              mApp.mHomeActivity.updateThisDevice(mApp.mThisDevice);
          }
      }

But I don't want that (to wait until a change), I want to know my own name from the beginning, in order to show it when I just open the app. Is this possible?

回答1:

If Wi-Fi is enabled and you register P2P and everything on load, then the device name should be available right away when that intent fires. If Wi-Fi is disabled, then you would have to wait for it to be enabled if you want to get the device name from WifiP2pManager. That's the only way I know how to get the name.



回答2:

The only way I see, is using reflection to change the device name and then using your code to get the original device name, after that you can set the device name back to the original.