我想用 Android 设备解析自定义 IOT 设备(esp8266)的 IP 地址。
经过一些研究,我认为有两个很好的选择可以做到这一点。第一个选项是 android NsdManager。第二个选项是Android mDNSResponder。
我更喜欢用 NsdManager 来做这件事。
我使用了 android 手册和其他一些帖子来获得一个很好的例子。当我尝试解析服务以获取 IP 地址和端口号时,我不断得到 0 作为结果。(内部故障错误)。
为了确保 ESP 正常工作,我安装了一个带有 mdns-sd 浏览器的应用程序来测试它。这工作正常。
有人可以给我一个提示,为什么我得到一个 0 结果?
ESP8266:
AsyncWebServer server(80);
DNSServer dns;
uint16_t GetDeviceId()
{
return ESP.getChipId();
}
String MakeMine(const char *NameTemplate)
{
uint16_t uChipId = GetDeviceId();
String Result = String(NameTemplate) + String(uChipId, HEX);
return Result;
}
void setup() {
Serial.begin(115200);
AsyncWiFiManager wifiManager(&server,&dns);
wifiManager.autoConnect("AutoConnectAP");
if (MDNS.begin("lego-test")) {
String MyName = MakeMine("LegoSensor");
MDNS.addService(MyName.c_str(), "tcp", 23);
}else{
Serial.println("Error setting up MDNS responder!");
}
}
void loop() {
MDNS.update();
}
安卓:
public class MainActivity extends BridgeActivity {
public String TAG = "test_mdns";
private NsdManager.DiscoveryListener discoveryListener;
private NsdManager mgr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initializes the Bridge
registerPlugin(ToiletLegoPlugin.class);
mgr = (NsdManager) getSystemService(Context.NSD_SERVICE);
initializeDiscoveryListener();
this.discoverServices();
}
public void discoverServices() {
mgr.discoverServices("_services._dns-sd._udp", NsdManager.PROTOCOL_DNS_SD, discoveryListener);
}
public void stopDiscovery() {
mgr.stopServiceDiscovery(discoveryListener);
}
public void initializeDiscoveryListener() {
discoveryListener = new NsdManager.DiscoveryListener() {
@Override
public void onDiscoveryStarted(String regType) {
Log.d(TAG, "Service discovery started");
}
@Override
public void onServiceFound(NsdServiceInfo service) {
if(service.getServiceName().contains("LegoSensor")){
Log.d(TAG, "Service discovery success" + service);
Log.d(TAG, "Type: " + service.getServiceType());
Log.d(TAG, "Service Name: " + service.getServiceName());
Log.d(TAG, "Service discovery success = " + service.toString());
mgr.resolveService(service, new InitializeResolveListener());
}
}
@Override
public void onServiceLost(NsdServiceInfo service) {
Log.e(TAG, "service lost: " + service);
}
@Override
public void onDiscoveryStopped(String serviceType) {
Log.i(TAG, "Discovery stopped: " + serviceType);
}
@Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
mgr.stopServiceDiscovery(this);
}
@Override
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
mgr.stopServiceDiscovery(this);
}
};
}
public class InitializeResolveListener implements NsdManager.ResolveListener {
@Override
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
Log.e(TAG, "Resolve failed" + errorCode);
}
@Override
public void onServiceResolved(NsdServiceInfo serviceInfo) {
Log.e(TAG, "Resolve Succeeded. " + serviceInfo);
int port = serviceInfo.getPort();
InetAddress host = serviceInfo.getHost(); // getHost() will work now
Log.d(TAG, "Host: " + host.getHostAddress());
}
};
}
日志猫:
2022-01-03 17:42:26.506 548-642/? E/NsdService: Failed to execute mdnssd [resolve, 152552, _LegoSensora15f, _tcp.local., local.]
com.android.server.NativeDaemonConnector$NativeDaemonArgumentException: command '152710 mdnssd resolve 152552 _LegoSensora15f _tcp.local. local.' failed with '501 152710 resolveService got an error from DNSServiceResolve'
at com.android.server.NativeDaemonConnector.executeForList(NativeDaemonConnector.java:512)
at com.android.server.NativeDaemonConnector.execute(NativeDaemonConnector.java:411)
at com.android.server.NativeDaemonConnector.execute(NativeDaemonConnector.java:406)
at com.android.server.NsdService$DaemonConnection.execute(NsdService.java:698)
at com.android.server.NsdService.resolveService(NsdService.java:749)
at com.android.server.NsdService.access$2300(NsdService.java:61)
at com.android.server.NsdService$NsdStateMachine$EnabledState.processMessage(NsdService.java:363)
at com.android.internal.util.StateMachine$SmHandler.processMsg(StateMachine.java:992)
at com.android.internal.util.StateMachine$SmHandler.handleMessage(StateMachine.java:809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.os.HandlerThread.run(HandlerThread.java:65)
2022-01-03 17:42:26.507 315-23443/? I/ADB_SERVICES: post waitpid (pid=23442) status=0000
2022-01-03 17:42:26.514 23388-23438/com.lego.frontend E/test_mdns: Resolve failed0