Search code examples
androidcordovatelnetzeroconfmdns

Detecting MDNS/Zeroconf device works on Android 5.1.1 but not on Android 7


I am detecting a device on a private network with no internet access. The only devices on the network are the device I'm detecting and the mobile devices or PC's that have app I am currently working on. This words just fine on Android 5.1.1 but on 7, I'm getting no response like its not finding anything. Here is the code I'm using to detect the device. The type I am using is _telnet._tcp. and the Cordova plugin I am using here is cordova-plugin-zeroconf

find(type) {// javascript
    return new Promise((resolve, reject) => {
        cordova.plugins.zeroconf.watch(type, "local.", (result) => {
            var action = result.action;
            var service = result.service;

            if (action == 'added') {
                console.debug('service added', service);
            }
            else if (action == 'resolved') {
                console.debug('service resolved', service);
                resolve({ status: "resolved", service: service });
            }
            else {
                console.debug('service removed', service);
                resolve({ status: "removed", service: service });
            }
        }, (error) => {
            reject(error);
        });
    });
}

I originally thought it might be because of permissions so I added this to the MainActivity in the Cordova Android project. And I can see it logs "has permission".

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//java
    Log.d(TAG, "version 23 or more");
    if (checkSelfPermission(Manifest.permission.ACCESS_WIFI_STATE) == PackageManager.PERMISSION_GRANTED &&
        checkSelfPermission(Manifest.permission.CHANGE_WIFI_MULTICAST_STATE) == PackageManager.PERMISSION_GRANTED &&
        checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
    Log.d(TAG, "has permission");
}
else {
    Log.d(TAG, "no permission");
    requestPermissions(new String[] { Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.CHANGE_WIFI_MULTICAST_STATE, 
            Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
    }
}

Solution

  • I ended up redoing the plugin to make use of Android's Network Service Discovery (NSD). I was able to get it to work with that. For the life of me I have no idea why the plugin wouldn't work for me. I never did find out what was causing it to not work.