Android Question WiFi Direct Example

Robert Valentino

Well-Known Member
Licensed User
Longtime User
It's been like 8 years since Erel's WiFiDirect Example and today I tried it on two different Samsung devices 1 running Android 5.1 and another running Android 11 SDK 30

Could not get it to work properly.

I would think WiFi Direct would be very stable by now????? But I am sure I am wrong.

My users are in a bowling alley collecting scores, usually the internet is poor or not available and cell service inside centers can be spotty

I need an easy way to let people transfer scores (small file) and WiFi Direct seems like perfect answer if I can it to work reliable

My App (a B4XPages is running min SDK 19 and will be max SDK 30 as soon as new B4A is out of beta.

Anyone have a solid working example (prefer B4XPages) that I can use.

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I figured out partially why WiFiDirect wasn't working

The example assumes that there is only 1 other device around and uses discoveredDevices entry 0
I have lots of devices around;
So for the time being I hard coded my real mac addresses and look for them and use that discoveredDevices entry

    If Success Then
        discoveredDevices = Devices
        For i = 0 To discoveredDevices.Size-1
            Dim dev As WifiDevice = discoveredDevices.Get(i)
            Log($"Device:${dev.Name} ${dev.MacAddress}"$)      
            If  dev.MacAddress.EqualsIgnoreCase("16:---------:e0") Or _        ' Galaxy Tab 7A    Mac Addresses hidden for posting
                dev.MacAddress.EqualsIgnoreCase("1e:---------:39") Then        ' Bobs S20 FE
                UseDeviceEntry = i
            End If
            Log($"Device:${i} - ${discoveredDevices.Get(i)}"$)      
            Log(" ")
        lblDevicesText = discoveredDevices.Size
        lblDevicesText = "0"
    End If

When I click Connect the other device shows a message asking to connect.
I reply Yes to the message, after a while the "client failed to connect" message shows up.

But have no idea on how to debug this

When I look at my WiFi connections on the client side I see the peer entry and if I click on it I am asked for a password to connect.

Makes me wonder if there is some type of hidden password I need to provide to connect.
Going to research this after posting


PS: I've also added
to the manifest and runtime permissions to the example.

If I get it working I will convert to B4XPages and upload

Interesting MyWifiMesh
Last edited:
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
OK, got it working.

What I needed to do clicking on Connect was to put up an InputList showing all the devices discovered and let the user pick one and then do the connect.

That got me connected and working.

The biggest problem I have is that if you try it too often then you cannot connect until you turn off wifi and turn it back on (on both devices).

Is there someway of resetting the connection without the user having to do this?

Also is what would be the proper way to end the connection after sending the data you need to send.

I was able to send a 97kb text file and it just happened (very quick)

Attaching Erel's modified program - as soon as I figure out how to reset the WiFi if connecting isn't working and how terminate the connection when done sending I will convert to B4XPages and re-post



    10.1 KB · Views: 351
Upvote 0