gpt4 book ai didi

android - 无法从 Android 连接到托管在 iMac 上的 bonjour 服务

转载 作者:行者123 更新时间:2023-11-29 02:13:10 25 4
gpt4 key购买 nike

我的目标是找到WIFI网络中的所有bonjour服务,找到它们的ip/端口,并通过HTTP进行通信。

托管在 iMac 上的 Bonjour 服务发现不正确 - 找到服务名称,但端口 = 0,InetAddress[] = null。与 Windows PC 上托管的相同服务的连接工作正常(来自一家供应商的相同版本的软件)- 名称、URL 和端口已正确发现。Bonjour 服务浏览器可以正确看到这两种服务。

JmDNS 3.4.0使用lib,见代码示例(部分代码省略):

MulticastLock lock = wifi.createMulticastLock(MULTICAST_LOCK_TAG);
lock.setReferenceCounted(true);
lock.acquire();

final InetAddress inetAddress = getLocalIpAddress();
jmdns = JmDNS.create(inetAddress, JMDNS_NAME);

ServiceInfo[] infos = jmdns.list(CAMERA_SERVERS);

if (infos != null && infos.length > 0) {
for (int i = 0; i < infos.length; i++) {
name = infos[i].getName();
InetAddress[] addresses = infos[i].getInetAddresses();
url = addresses[0].getHostAddress();
port = infos[i].getPort();
}

附言。尝试了 Apple 的 dns_sd.jar,但它依赖于 native 代码,无法在 Android 中使用。

有什么想法吗?

谢谢。

最佳答案

使用下面的代码。已经过测试和实现,工作正常

android.net.wifi.WifiManager.MulticastLock lock;
android.os.Handler handler = new android.os.Handler();

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

handler.postDelayed(new Runnable() {
public void run() {
setUp1();
}
}, 1000);

}

private void setUp1()
{
android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) getSystemService(android.content.Context.WIFI_SERVICE);
lock = wifi.createMulticastLock("mylockthereturn");
lock.setReferenceCounted(true);
lock.acquire();

try {
//http://stackoverflow.com/questions/13677199/jmdns-doesnt-work-properly-on-android-4-1


WifiInfo wifiInfo = wifi.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
String ipAddress = Formatter.formatIpAddress(ip);

InetAddress _bindingAddress = InetAddress.getByName(ipAddress);
jmdns = JmDNS.create(_bindingAddress);

ServiceInfo[] infos = jmdns.list("_afpovertcp._tcp.local.");
for (int i=0; i < infos.length; i++) {
notifyUser("\nServic : "+infos[i].getName()+"");

}
System.out.println();


} catch (IOException e) {
e.printStackTrace();
}

}

private void notifyUser(final String msg) {
handler.postDelayed(new Runnable() {
public void run() {

TextView t = (TextView)findViewById(R.id.text);
t.setText(msg+"\n=== "+t.getText());
}
}, 1);

}

关于android - 无法从 Android 连接到托管在 iMac 上的 bonjour 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6048442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com