Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socket.open fails with error ENETUNREACH (Network is unreachable) #24

Open
bmwertman opened this issue Oct 16, 2015 · 0 comments
Open

Comments

@bmwertman
Copy link

I'm trying to connect to a Wifi OnBoard Diagnostic(OBD) connector interface for a car's OBD port from my ionic application. But the connection fails with the error message given below my code.

I even changed from $ionicPlatform.ready() to angular.bootstrap method of initializing my app like some other posts had suggested. But still the same issue. Any ideas?

function str2ab(str) {
    // var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
    var bufView = new Uint8Array(str.length);
    for (var i=0; i<bufView.length; i++) {
      bufView[i] = str.charCodeAt(i);
    }
    return bufView;
  }

  function connectToHost(host, port) {
    var socket = new Socket();
    socket.onError = function(errorMessage) {
      console.log("Error occured, error: " + errorMessage);
    };
    socket.onClose = function(hasError) {
      console.info("Socket closed, hasErrors=" + hasError);
    };
    socket.onData = function (data){
      var chars = new Array(data.length);
      for (var i = 0; i < data.length; i++) {
          chars.push(String.fromCharCode(data[i]));
      }
      var dataString = chars.join("");
      console.log(dataString);
    };
    socket.open(
      host,
      port,
      function(success) {
        console.log(success);
      },
      function(errorMessage) {
        console.log("Error during connection, error: " + errorMessage);
      }
    );
  }

  $scope.OBDPlugNames = [];
  function connectToPlug(){
    var config = $cordovaWifiWizard.formatWPAConfig($localstorage.get('wifiName'), $localstorage.get('wifiPassword'));
    $cordovaWifiWizard.isWifiEnabled().then(function (bool){
      if(bool){//It's enabled
        $cordovaWifiWizard.getCurrentSSID().then(function (res){
          if(res !== config.SSID){//connected to a wifi network besides the OBDII plug?
            $cordovaWifiWizard.disconnect(res).then(function (){//disconnect from current network
              $cordovaWifiWizard.connectNetwork(config.SSID)//connect to OBDII plug
              .then(function (res){
                $scope.OBDConnected = true;
                var PID = str2ab("010F0D");
                var disableEcho = str2ab("ATE0");
                connectToHost('MY_IP', MY_PORT)
              });
            });
          } else {
            $cordovaWifiWizard.connectNetwork(config.SSID)//connect to OBDII plug
            .then(function (res){
              $scope.OBDConnected = true;
              var PID = str2ab("010F0D");
              var disableEcho = str2ab("ATE0");
              connectToHost('MY_IP', MY_PORT)
            });
          }
        });
      } else {//it's disabled
        $ionicPopup.alert({
          template:  "<h3>Wifi is disabled</h3><br><strong><p>Please enable wifi and try again</p></strong>"
        });
      }
    });
  }

returns the following error;

Error during connection, error: failed to connect to /MY_IP (port MY_PORT): connect failed: ENETUNREACH (Network is unreachable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant