最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to receive UDP broadcast packets in Chrome App running on Android - Stack Overflow

programmeradmin4浏览0评论

I can receive broadcast packets without any problems within a windows running chrome app. However, when I take that app and pile using the cordova/crosswalk tools I can't seem to receive any packets. I see all the packets in wireshark. My packet is transmitted from: 172.24.0.42 and broadcasted on 172.24.255.255 (a broadcast on 255.255.255.255 does not work on Android, but it does work on the windows chrome app).

This is my (manifest.json):

"sockets":{
        "udp": {
            "bind": "*"
        }
    }, 
    "permissions":["systemwork" , "power"],

This is my code for my network:

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", 4213, 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);
    //  })
    //chrome.sockets.udp.setBroadcast(socketId, true, function(){})
    });
});

This is when I receive the packets:

var onReceive = function(info) {
    if (info.socketId !== socketId)
        return;
    chrome.sockets.udp.setPaused(socketId, true, function(){}); // Set socket paused; Essentially blocking
    //console.log();

    ///processing of my packet

    chrome.sockets.udp.setPaused(socketId, false, function(){}); //unpause socket
};

Edit: I've been trying my best to understand why I can't get any broadcast packets in the chrome app on Android. Unfortunately, I've ran into a wall.

I can receive broadcast packets without any problems within a windows running chrome app. However, when I take that app and pile using the cordova/crosswalk tools I can't seem to receive any packets. I see all the packets in wireshark. My packet is transmitted from: 172.24.0.42 and broadcasted on 172.24.255.255 (a broadcast on 255.255.255.255 does not work on Android, but it does work on the windows chrome app).

This is my (manifest.json):

"sockets":{
        "udp": {
            "bind": "*"
        }
    }, 
    "permissions":["systemwork" , "power"],

This is my code for my network:

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", 4213, 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);
    //  })
    //chrome.sockets.udp.setBroadcast(socketId, true, function(){})
    });
});

This is when I receive the packets:

var onReceive = function(info) {
    if (info.socketId !== socketId)
        return;
    chrome.sockets.udp.setPaused(socketId, true, function(){}); // Set socket paused; Essentially blocking
    //console.log();

    ///processing of my packet

    chrome.sockets.udp.setPaused(socketId, false, function(){}); //unpause socket
};

Edit: I've been trying my best to understand why I can't get any broadcast packets in the chrome app on Android. Unfortunately, I've ran into a wall.

Share Improve this question edited Apr 30, 2015 at 15:33 Xan 77.7k18 gold badges197 silver badges217 bronze badges asked Apr 22, 2015 at 1:39 JParrish88JParrish88 5976 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

setBroadcast has been added to the library. You can now call setBroadcast() to enable broadcast permissions.

1.3.0 (Sep 27, 2016)

Adds chrome.udp.setBroadcast()

https://github./MobileChromeApps/cordova-plugin-chrome-apps-sockets-udp

The cordova plugin for chrome-apps-sockets-udp does not enable broadcasts to be received by default. However with some modification to the plug-in src locally (see below steps) you can enable broadcast messages to be received. The below info was successfully tested on Android and hope this info is helpful to others who might be struggling to get broadcast messages received.

1) Check if you have installed the cordova plugin for chrome-apps-sockets-udp

cordova plugin list

if you see this info then the plugin is already installed

cordova-plugin-chrome-apps-sockets-udp 1.2.2 "Chrome Apps Sockets UDP API"

2) if you don't see the plugin listed then install it:

cordova plugin add cordova-plugin-chrome-apps-sockets-udp

3) next, make sure you've added your platform: (ionic example below, phonegap has similar mand)

ionic platform android

4) then build for your app: (ionic example below, phonegap has similar mand)

ionic build android

5) now let's edit the src file in the Android platform. In a text editor or from your IDE browse to the <appname>/platforms/android/src/org/chromium directory and open the ChromeSocketsUdp.java file. Search for this method void bind(String address, int port) and after this line channel.socket().setReuseAddress(true); add the following line channel.socket().setBroadcast(true); and then save the file.

the bind method should now look like the below:

  void bind(String address, int port) throws SocketException {
    channel.socket().setReuseAddress(true);
    channel.socket().setBroadcast(true);
    channel.socket().bind(new InetSocketAddress(port));

    if (multicastSocket != null) {
      bindMulticastSocket();
    }
  }

6) Run your application e.g. ionic run android and broadcast udp messages should now be received by your Android application.

NOTE: these local changes you've made above will be overridden during the next build. so if you are happy with your test results you can then modify the plugin src file located at <appname>/plugins/cordova-plugin-chrome-apps-sockets-udp/src/android/ChromeSocketsUdp.java

here is the gist link to the relevant code sections of the ionic app I wrote for testing the receive of UDP broadcasts on Android gist.github./bfalzarano/e530ca80767a0aea71a145be44943941

Have you installed the cordova plugin for chrome-sockets-udp?

From the terminal at the root of your project, type in:

cordova plugin add cordova-plugin-chrome-apps-sockets-udp
发布评论

评论列表(0)

  1. 暂无评论