Unable to communicate to device with .local domain using android, corova-zeroconf-plugin

人走茶凉 提交于 2020-01-06 17:07:27

问题


I am having an iot device which is advertising in my network with name iotdevice.local, i am able to send http requests to this domain in my windows pc after installing Bonjour. But i am developing an ionic app from which i'll be communicating frequently with this device. So i am using cordova-zeroconf-plugin. (https://www.npmjs.com/package/cordova-plugin-zeroconf). I am seeing the services when i add watch for .local services. but my requestis faield when i use iotdevice.local in $http function. I have this code App.js

if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
  cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
  cordova.plugins.Keyboard.disableScroll(true);
  console.log("device ready");
  var zeroconf = cordova.plugins.zeroconf;
  zeroconf.watch('_http._tcp.local.', function(result) {
    var action = result.action;
    var service = result.service;
    console.log(JSON.stringify(result));
    if (action == 'added') {
        console.log('service added', service);
    } else {
        console.log('service removed', service);
    }
  });


}

It works fine and in logs i see my device

{"action":"added","service":{"application":"http","domain":"local","port":80,"name":"iotdevice","server":"iotdevice.local.","description":"\00","protocol":"tcp","qualifiedname":"iotdevice._http._tcp.local.","type":"_http._tcp.local.","txtRecord":{},"addresses":["192.168.0.4"],"urls":["http://192.168.0.4:80"]}}

But when i use this in controller.js-

$scope.sendRequest2 = function(){
    $http({
      method: 'GET',
      url: 'http://iotdevice.local/events?id=600194067eba&action=socket1off'
    }).then(function successCallback(response) {
        $scope.req2succes=true;
        console.log("Sent");
      }, function errorCallback(response) {
        console.log(JSON.stringify(response));
        console.log("Could not Sent");
        $scope.req2succes=false;
      });
  }

It Doesn't work. Request status is always -1. can anyone help me how i can deal with his. Am i doing something fundamentally wrong ?


回答1:


Use IP address instead of http://iotdevice.local..Use http://192.168.0.4:80.Hope this works.



来源:https://stackoverflow.com/questions/40235229/unable-to-communicate-to-device-with-local-domain-using-android-corova-zerocon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!