[英]How to receive UDP broadcast packets in Chrome App running on Android
I can receive broadcast packets without any problems within a windows running chrome app. 在运行Chrome应用程序的Windows中,我可以毫无问题地接收广播数据包。 However, when I take that app and compile using the cordova/crosswalk tools I can't seem to receive any packets.
但是,当我使用该应用程序并使用cordova / crosswalk工具进行编译时,我似乎无法接收任何数据包。 I see all the packets in wireshark.
我看到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).
我的数据包从172.24.0.42传输并在172.24.255.255上播出(255.255.255.255上的广播在Android上不起作用,但它在Windows Chrome应用程序上有效)。
This is my (manifest.json): 这是我的(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. 编辑:我一直在努力理解为什么我不能在Android上的Chrome应用程序中获取任何广播数据包。 Unfortunately, I've ran into a wall.
不幸的是,我遇到了一堵墙。
The cordova plugin for chrome-apps-sockets-udp does not enable broadcasts to be received by default. chrome-apps-sockets-udp的cordova插件默认不接收广播。 However with some modification to the plug-in src locally (see below steps) you can enable broadcast messages to be received.
但是,如果对本地插件src进行一些修改(请参阅下面的步骤),则可以启用接收广播消息。 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.
以下信息已在Android上成功测试,希望此信息对于可能难以收到广播消息的其他人有所帮助。
1) Check if you have installed the cordova plugin for chrome-apps-sockets-udp 1)检查您是否安装了chrome-apps-sockets-udp的cordova插件
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: 2)如果您没有看到列出的插件,请安装它:
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) 3)接下来,确保你已经添加了你的平台:(下面的离子示例,phonegap有类似的命令)
ionic platform android
4) then build for your app: (ionic example below, phonegap has similar command) 4)然后为你的应用程序构建:(下面的离子示例,phonegap有类似的命令)
ionic build android
5) now let's edit the src file in the Android platform. 5)现在让我们编辑Android平台中的src文件。 In a text editor or from your IDE browse to the
<appname>/platforms/android/src/org/chromium
directory and open the ChromeSocketsUdp.java
file. 在文本编辑器或IDE中,浏览到
<appname>/platforms/android/src/org/chromium
目录并打开ChromeSocketsUdp.java
文件。 Search for this method void bind(String address, int port)
and after this line channel.socket().setReuseAddress(true);
搜索此方法
void bind(String address, int port)
和此行之后的channel.socket().setReuseAddress(true);
add the following line channel.socket().setBroadcast(true);
添加以下行
channel.socket().setBroadcast(true);
and then save the file. 然后保存文件。
the bind method should now look like the below: bind方法现在应该如下所示:
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 eg ionic run android
and broadcast udp messages should now be received by your Android application. 6)运行你的应用程序,例如
ionic run android
和广播udp消息现在应该由你的Android应用程序接收。
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
因此,如果您对测试结果感到满意,则可以修改位于
<appname>/plugins/cordova-plugin-chrome-apps-sockets-udp/src/android/ChromeSocketsUdp.java
的插件src文件。
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 这里是我为测试在Android上接收UDP广播所写的离子应用程序的相关代码部分的要点链接gist.github.com/bfalzarano/e530ca80767a0aea71a145be44943941
setBroadcast
has been added to the library. setBroadcast
已添加到库中。 You can now call setBroadcast()
to enable broadcast permissions. 您现在可以调用
setBroadcast()
来启用广播权限。
1.3.0 (Sep 27, 2016)
1。3。0(2016年9月27日)
Adds chrome.udp.setBroadcast()
添加chrome.udp.setBroadcast()
https://github.com/MobileChromeApps/cordova-plugin-chrome-apps-sockets-udp https://github.com/MobileChromeApps/cordova-plugin-chrome-apps-sockets-udp
Have you installed the cordova plugin for chrome-sockets-udp? 你有没有为chrome-socket-udp安装cordova插件?
From the terminal at the root of your project, type in: 从项目根目录的终端输入:
cordova plugin add cordova-plugin-chrome-apps-sockets-udp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.