![](/img/trans.png)
[英]Possible error in the Android docs code example or am I missing something?
[英]Is Multicast broken for Android 2.0.1 (currently on the DROID) or am I missing something?
此代碼在Ubuntu,Windows和Mac OS X中運行良好。它也適用於運行Android 2.1.1的Nexus One。
我開始發送和收聽多播數據報,所有計算機和Nexus One都能完美地看到對方。 然后我在Droid (固件2.0.1) 上運行相同的代碼 , 每個人都會得到Droid發送的數據包,但機器人只會監聽自己的數據包 。
這是一個線程的run()
方法,該線程一直在組播組上偵聽發送到該組的傳入數據包。
我在本地網絡上運行測試,我在路由器中啟用了多播支持。 我的目標是讓設備通過向多播組廣播包而在網上相遇。
public void run() {
byte[] buffer = new byte[65535];
DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
try {
MulticastSocket ms = new MulticastSocket(_port);
ms.setNetworkInterface(_ni); //non loopback network interface passed
ms.joinGroup(_ia); //the multicast address, currently 224.0.1.16
Log.v(TAG,"Joined Group " + _ia);
while (true) {
ms.receive(dp);
String s = new String(dp.getData(),0,dp.getLength());
Log.v(TAG,"Received Package on "+ _ni.getName() +": " + s);
Message m = new Message();
Bundle b = new Bundle();
b.putString("event", "Listener ("+_ni.getName()+"): \"" + s + "\"");
m.setData(b);
dispatchMessage(m); //send to ui thread
}
} catch (SocketException se) {
System.err.println(se);
} catch (IOException ie) {
System.err.println(ie);
}
}
這是從每個可用的有效網絡接口(不是環回接口)發送多播數據報的代碼。
public void sendPing() {
MulticastSocket ms = null;
try {
ms = new MulticastSocket(_port);
ms.setTimeToLive(TTL_GLOBAL);
List<NetworkInterface> interfaces = getMulticastNonLoopbackNetworkInterfaces();
for (NetworkInterface iface : interfaces) {
//skip loopback
if (iface.getName().equals("lo"))
continue;
ms.setNetworkInterface(iface);
_buffer = ("FW-"+ _name +" PING ("+iface.getName()+":"+iface.getInetAddresses().nextElement()+")").getBytes();
DatagramPacket dp = new DatagramPacket(_buffer, _buffer.length,_ia,_port);
ms.send(dp);
Log.v(TAG,"Announcer: Sent packet - " + new String(_buffer) + " from " + iface.getDisplayName());
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e2) {
e2.printStackTrace();
}
}
更新(2010年4月2日)我找到了一種讓Droid的網絡接口使用Multicast進行通信的方法: WifiManager.MulticastLock 。
MulticastLock _wifiMulticastLock = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)).createMulticastLock("multicastLockNameHere");
_wifiMulticastLock.acquire();
那么當你完成了......
if (_wifiMulticastLock != null && _wifiMulticastLock.isHeld())
_wifiMulticastLock.release();
在我這樣做之后,Droid開始在多播組上發送和接收UDP數據報。
2010年7月6日更新
根據請求,這是我當前的代碼,下一個方法存在於可用於廣播和多播接收器的抽象類上。
public void run() {
onInit();
try {
byte[] data = new byte[65535];
while (isProcessing()) {
try {
DatagramPacket receivedDatagram = new DatagramPacket(data, data.length);
_socket.receive(receivedDatagram);
onDatagramReceived(receivedDatagram);
data = new byte[65535]; // This pattern is for saving memory allocation.
} catch (InterruptedIOException e) {
if (!isProcessing())
break;
}
} // while
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
} finally {
onStop();
_socket.close();
_socket.disconnect();
}
}
您的擴展類應該實現onInit()
和onDatagramReceived()
對於多播接收器, onInit()
看起來像這樣:
_socket = new MulticastSocket(PORT_MULTICAST);
InetAddress groupAddress = InetAddress.getByAddress(MULTICAST_GROUP_ADDRESS);
InetAddress groupInetAddress = FrostWireUtils.fastResolveAddress(groupAddress); //reflection hack to not resolve ips
try {
_socket.setSoTimeout(500);
_socket.setTimeToLive(MULTICAST_TTL_GLOBAL);
_socket.setReuseAddress(true);
_socket.setNetworkInterface(
WifiUtils.getWifiNetworkInterface());
_socket.joinGroup(groupInetAddress);
WifiUtils.lockMulticast();
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
我已經實現了另一個測試,這次是使用UDP Broadcast 。 有用。
結論:據我所知,固件2.0.1上的Motorola Droid手機不支持多播,但您總是可以在廣播地址上使用常規DatagramPackets。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.