让JmDNS与Android AVD配合使用时遇到了一些困难。我创建了3个应用程序。一个注册ServiceListener并记录任何活动的Android应用程序,一个与android应用程序相同的Java应用程序,以及另一个注册服务的Java应用程序。 Java监听器应用程序将接收其他Java应用程序,但Android应用程序不会。我也试过同时运行两个AVD,看看他们是否相互接受而他们没有。我还要提一下,我对INTERNET和CHANGE_WIFI_MULSTICAST_STATE的权限已启用。这是我的代码:
Android应用
public class BonjourActivity extends Activity {
// Multicast
private WifiManager wifi;
private MulticastLock lock;
private JmDNS jmdns;
private String type = "_im._tcp.local.";
private ServiceListener listener;
private ServiceInfo serviceInfo;
// On Create
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Multicast
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
lock = wifi.createMulticastLock("");
lock.setReferenceCounted(true);
lock.acquire();
// JmDNS
new AsyncTask<Object, Object, Object>(){
@Override
protected Object doInBackground(Object... params) {
// Create JmDNS
try {
jmdns = JmDNS.create();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Object result) {
// Add Listener
jmdns.addServiceListener(type, listener = new ServiceListener(){
@Override
public void serviceAdded(ServiceEvent ev) {
jmdns.requestServiceInfo(ev.getType(), ev.getName(), 1);
}
@Override
public void serviceRemoved(ServiceEvent ev) {
Log.d("Service", "Service Removed: " + ev.getName());
}
@Override
public void serviceResolved(ServiceEvent ev) {
Log.d("Service", "Service Resolved: " + ev.getInfo().getURL());
}
});
}
}.execute();
}
// On Destroy
public void onDestroy(){
// Release Lock
if (lock != null){
lock.release();
}
// Close JmDNS
if (jmdns != null){
jmdns.removeServiceListener(type, listener);
try {
jmdns.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
super.onDestroy();
}
}
Java侦听器应用程序:
public class Listener {
private static JmDNS jmdns;
private static String type = "_im._tcp.local.";
private static ServiceListener serviceListener;
private static ServiceInfo serviceInfo;
// Main
public static void main(String args[]){
try {
jmdns = JmDNS.create();
jmdns.addServiceListener(type, serviceListener = new ServiceListener(){
@Override
public void serviceAdded(ServiceEvent ev) {
System.out.println("Service Added: " + ev.getName());
jmdns.requestServiceInfo(ev.getType(), ev.getName(), 1);
}
@Override
public void serviceRemoved(ServiceEvent ev) {
System.out.println("Service Removed: " + ev.getName());
}
@Override
public void serviceResolved(ServiceEvent ev) {
System.out.println("Service Resolved: " + ev.getInfo().getURL());
}
});
System.out.println("Listener Added");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Java发件人应用程序:
public class Sender {
private static JmDNS jmdns;
private static String type = "_im._tcp.local.";
private static ServiceListener serviceListener;
private static ServiceInfo serviceInfo;
// Main
public static void main(String args[]){
try {
jmdns = JmDNS.create();
serviceInfo = ServiceInfo.create(type, "Test IM Service", 55555, "Instant messaging test service");
jmdns.registerService(serviceInfo);
System.out.println("Sender: Service Created");
new Timer().schedule(new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Closing..");
jmdns.unregisterAllServices();
try {
jmdns.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(0);
}
}, 10000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
另外,当我尝试运行应用程序时,LogCat会给我一些错误:
NetworkManagementSocketTagger setKernelCountSet(10009,0)以errno -2
失败WifiStateMachine错误!未处理的消息{what = 131157 when = -1ms}
有谁知道为什么android应用程序无法接收其他应用程序生成的JmDNS服务?
答案 0 :(得分:1)
稍微注意,模拟器中运行的虚拟设备与计算机不在同一网络中。服务发现可能不起作用。我的jmdns实现在模拟器中不起作用。在真实设备上试用它。 并转到http://home.heeere.com/tech-androidjmdns.html了解更多详情。