Search code examples
androidandroid-wifiwifi-directwifip2p

Android wi-fi p2p service is not discovered


I'm trying to advertise Bonjour service via Wi-Fi direct. Here is code:

manager.clearLocalServices(channel, new WifiP2pManager.ActionListener() {
    @Override
    public void onSuccess() {
        Map<String, String> record = new HashMap<>();
        record.put("qwe", "123");
        WifiP2pDnsSdServiceInfo serviceInfo = WifiP2pDnsSdServiceInfo.newInstance("_test", "_presence._tcp", record);
        manager.addLocalService(channel, serviceInfo, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
                    @Override
                    public void onSuccess() {}
                    @Override
                    public void onFailure(int reason) {}
                });
            }
            @Override
            public void onFailure(int reason) {}
        });
    }
    @Override
    public void onFailure(int reason) {}
});

On Kali Linux I use wpi_cli to discover service

> p2p_serv_disc_req 00:00:00:00:00:00 02000001
> p2p_find

Kali successfully discovers Android, I see corresponding message:

P2P-DEVICE-FOUND 70:0b:c0:ac:63:11 p2p_dev_addr=70:0b:c0:ac:63:11 pri_dev_type=10-0050F204-5 name='PSP5506DUO' config_methods=0x188 dev_capab=0x25 group_capab=0x0 new=1

But no following information about service is discovered.

How could I ensure, on what side is problem? Whether Android does not advertise service, or Kali does not discover it?

And more generic question - is there a working example of wi-fi p2p services advertising and discovering Android application?


Solution

  • I suspect that the problem is not on the Android side simply because i have gotten WiFi direct service discovery to work with extra text info between 2 Android devices. If memory serves me right, the following is an example Activity that you would run on both devices:

    https://github.com/mholzel/Dump/blob/master/NetworkServiceDiscoveryViaWifiDirect.java

    So the approach I would take to diagnosis the problem is to first make sure the above code works on your devices. That would ensure that indeed SOMETHING is being broadcast. After that point I think you have to suspect that Kali is the problem.

    Two more gotchas:

    1. service discovery is usually a 2 step process: get the service name, and then go get its info. In some apis, you register different listeners for getting the name than those that get the info. Some apis also require you to call something like resolve service after you have the name, which will explicitly go get the info. Not sure about Kali though...

    2. The builtin Android WiFi (not WiFi direct) API does not work on most devices less than 6.0 (or maybe 7, I forget) for retrieving the info. Specially, on older devices, you can get the name, but not the info due to a bug in the implementation that has been sitting there for years, and Google is not resolving. This is why I suggest you try Android to Android first. Just to rule out the possibility that there is something not working on your specific android device.