I can receive broadcast packets without any problems within a windows running chrome app. However, when I take that app and compile 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":["system.network" , "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.
Chrome Apps can act as a network client for TCP and UDP connections. This doc shows you how to use TCP and UDP to send and receive data over the network. For more information, see the Sockets UDP, Sockets TCP and Sockets TCP Server APIs.
Connecting via UDP Set the IP address of the PC's Network card to the same subnet than your Audia/Nexia units. In Audia/Nexia/DaVinci software, go to the Tools menu > Options > Network tab. In the Network Device Discovery Method section, make sure UDP Broadcast is selected.
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.com/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 command)
ionic platform android
4) then build for your app: (ionic example below, phonegap has similar command)
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.com/bfalzarano/e530ca80767a0aea71a145be44943941
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With