0

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?

piet.t
  • 11,718
  • 21
  • 43
  • 52
BlueMountain
  • 197
  • 2
  • 17

2 Answers2

0

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.

p2pkit
  • 1,159
  • 8
  • 11
0

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.

Brendan
  • 1,403
  • 4
  • 18
  • 37
  • sorry, what do you exactly mean by "register P2P"? I do this: `WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); wifiManager.setWifiEnabled(true); // If service not started yet, start it. Intent serviceIntent = new Intent(this, ConnectionService.class); startService(serviceIntent); // start the connection service` And then I process the Intent. – BlueMountain Jul 05 '16 at 14:35
  • Yes, that's what I meant by register P2P. If you run Android's WiFiDirectServiceDiscovery demo, the DEVICE_CHANGED intent fires right away on load if Wi-Fi is enabled. You can log device.deviceName there to see the name. The device name doesn't have to change for the intent to fire, so you won't need to wait for a change. https://github.com/android/platform_development/tree/master/samples/WiFiDirectServiceDiscovery https://groups.google.com/forum/#!topic/wi-fi-direct/uWpuOzHY6y0 – Brendan Jul 05 '16 at 22:17
  • I have another issue then, using the code in your last comment. If I want to transmit some messages (let's say messages in a db I've created) from one device to another and vice versa (at the same time), how can I avoid collision? I'm trying to do that but only works one way. – BlueMountain Jul 13 '16 at 14:42
  • how to get deviceName from p2pManager? – Mayank Kumar Chaudhari Dec 04 '18 at 07:02