3

我遇到了与网络服务发现相关的问题。

当我在连接了 wifi 的情况下启动应用程序时,NSD 可以很好地发现服务并顺利解决它们。但是,当我们连接 wifi,禁用 wifi 或从飞行模式切换 wifi 后,就会出现问题。

它只是卡在上面DiscoveryStarted,永远不会从那里继续,尽管它在关闭飞行模式后建立了与 wifi 路由器的连接。

在代码中,我还确保只有在确保 wifi 连接时才会开始发现,但运气不好。

现在我必须杀死应用程序才能让 NSD 正常工作。

我正在使用来自 Google Gist 的 NSD Helper:

https://android.googlesource.com/platform/development/+/master/samples/training/NsdChat/src/com/example/android/nsdchat/NsdHelper.java

NsdHelper helper;
    BroadcastReceiver wifiReciever = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
                if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)) {
                    //do stuff
                    helper.stopDiscovery();
                    helper = new NsdHelper(context);
                    helper.discoverServices();
                } else {
                    // wifi connection was lost
                    helper.stopDiscovery();
                }
            }
        }
    };




    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //startDiscovery();
//        helper = new NsdHelper(this);
//        helper.discoverServices();

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
        registerReceiver(wifiReciever, intentFilter);




        Toast.makeText(this,"Service Started",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
//        if(service!= null)
//        {
//            service.stop();

//        helper.stopDiscovery();
//        }
        unregisterReceiver(wifiReciever);
//
       Toast.makeText(this,"Service destroyed",Toast.LENGTH_SHORT).show();
        super.onDestroy();
    }
4

0 回答 0