Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Cannot send UDP packet #27

Open
classictoni opened this issue Oct 18, 2018 · 3 comments
Open

Cannot send UDP packet #27

classictoni opened this issue Oct 18, 2018 · 3 comments

Comments

@classictoni
Copy link

classictoni commented Oct 18, 2018

Hi,
I am trying to send a UDP packet from my Android device. My code looks as follows:

chrome.sockets.udp.create({}, function(createInfo) {
    const socketId = createInfo.socketId;
    const arrayBuffer = new ArrayBuffer(2);
    arrayBuffer[0] = 0x01;
    arrayBuffer[1] = 0xff;
    console.log('socketId: ' + createInfo.socketId);
    chrome.sockets.udp.send(socketId, arrayBuffer,
      '127.0.0.1', 1337, function(sendInfo) { 
        console.log('sent ' + sendInfo.bytesSent); // this log never happens
      });
});

Why isn't it sending anything?

  • socketId is 0
  • Do I need to set any Android permissions? In AndroidManifest.xml I have
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

I am using

  • cordova version 8.1.2
  • ionic version 3.9.2
  • npm version 6.4.1
@classictoni
Copy link
Author

I found a solution that works, when binding the socket before sending data (from receiving data example):

var socketId;

// Handle the "onReceive" event.
var onReceive = function(info) {
  if (info.socketId !== socketId)
    return;
  console.log(info.data);
};

// Create the Socket
chrome.sockets.udp.create({}, function(socketInfo) {
  socketId = socketInfo.socketId;
  // Setup event handler and bind socket.
  chrome.sockets.udp.onReceive.addListener(onReceive);
  chrome.sockets.udp.bind(socketId,
    "0.0.0.0", 0, function(result) {
      if (result < 0) {
        console.log("Error binding socket.");
        return;
      }
      chrome.sockets.udp.send(socketId, arrayBuffer,
        '127.0.0.1', 1337, function(sendInfo) {
          console.log("sent " + sendInfo.bytesSent);
      });
  });
});

@ebrahemimorteza
Copy link

For me this code does not work I checked everything the plugin is installed but it does not work

@ebrahemimorteza
Copy link

If you have a solution, tell me, thank you in advance

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants