Android Question WiFiDirect Example: How to get my own device name

toby

Well-Known Member
Licensed User
Longtime User
I'm wondering how to let my app retrieve the device name under WiFi Direct settings, not the name of the discovered peers.

I found a piece of code (see below) from here. If someone could make it work in B4A, that would be great.

B4X:
//After you turn on wifi on your device, it is send a WIFI_P2P_THIS_DEVICE_CHANGED_ACTION broadcast. You can catch this with a broadcast receiver and you can get a WifiP2pDevice object, that is your device.

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

Erel

B4X founder
Staff member
Licensed User
Longtime User
Add a service module and add this manifest editor code:

B4X:
AddReceiverText(NewService,
<intent-filter>
    <action android:name="android.net.wifi.p2p.THIS_DEVICE_CHANGED" />
</intent-filter>)

Add to Service_Start of NewService:
B4X:
Log(StartingIntent)
Log(StartingIntent.ExtrasToString)
What is the output?
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
The output indeed includes the device's own name (the text in bold red). Thank you very much!

The output doesn't look like json to me. There must be some easy way to extract the device name, unfortunately all I know is to treat it as a regular string.

*** Service (newservice) Create ***
** Service (newservice) Start **
starting NewService
(Intent) Intent { act=android.net.wifi.p2p.THIS_DEVICE_CHANGED flg=0x4000010 cmp=b4a.example/.newservice$newservice_BR (has extras) }
Bundle[{wifiP2pDevice=Device: zenfone3_a8
deviceAddress: 62:45:cb:28:51:bd
primary type: 10-0050F204-5
secondary type: null
wps: 0
grpcapab: 0
devcapab: 0
status: 3
wfdInfo: WFD enabled: trueWFD DeviceInfo: 16
WFD CtrlPort: 7236
WFD MaxThroughput: 50}]
 
Upvote 0
Top