0

我已经搜索了一些网络,但我没有找到正确的答案。JmDNS 侦听器不起作用。为什么?

    protected void onStart() {
            super.onStart();
            try {
                Log.v(TAG, "onStart()");

                if(android.os.Build.VERSION.SDK_INT > 9){
                    StrictMode.ThreadPolicy.Builder builder = new Builder();
                    StrictMode.ThreadPolicy policy = builder.permitAll().build(); 
                    StrictMode.setThreadPolicy(policy);
                }

                WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);

                mMulticastLock = wifi.createMulticastLock("mDNSLock");
                mMulticastLock.setReferenceCounted(true);
                mMulticastLock.acquire();

                String ip = Formatter.formatIpAddress(wifi.getConnectionInfo().getIpAddress());
                mJmDNSManager = JmDNS.create(ip);
                mJmDNSManager.addServiceTypeListener(mJmDNSServiceTypeListener);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
private ServiceTypeListener mJmDNSServiceTypeListener = new ServiceTypeListener() {

        @Override
        public void subTypeForServiceTypeAdded(ServiceEvent event) {
            // TODO Auto-generated method stub
            Log.v(TAG, "== subTypeForServiceTypeAdded: "+ event.getType());

        }

        @Override
        public void serviceTypeAdded(ServiceEvent event) {
            // TODO Auto-generated method stub
            Log.v(TAG, "== serviceTypeAdded: " + event.getType());
            mJmDNSManager.addServiceListener(event.getType(), mJmDNSServiceListener);
        }
    };

    protected ServiceListener mJmDNSServiceListener = new ServiceListener(){
        @Override
        public void serviceAdded(ServiceEvent event) {
            // TODO Auto-generated method stub
            Looper.prepare();
            Log.v(TAG, "== serviceAdded : " + event.getName());
            mServiceEvents = new ArrayList<ServiceEvent>();

            //mJmDNSManager.requestServiceInfo(event.getType(), event.getName(), 1);

            ServiceInfo info = event.getInfo();
            Toast.makeText(MainActivity.this, "onServiceAdded : " + info.getName() + "/" 
            + info.getType() + "/" + info.getPort(), Toast.LENGTH_LONG).show();

            mServiceEvents.add(event);

            Log.v(TAG, "==mServiceEvents.size() : " + mServiceEvents.size());
            if(mServiceEvents.size() == 1) {
                autoPairing();
            }
            else {
                progressDialog.dismiss();

                if (mDialog != null && !mDialog.isRemoving()) mDialog.dismiss();

                mDialog = new SelectDeviceDialog();
                mDialog.show(getFragmentManager(), "SelectDeviceDialog");
            }
            Looper.loop();
        }

        @Override
        public void serviceRemoved(ServiceEvent event) {
            // TODO Auto-generated method stub
            Log.v(TAG, "== serviceRemoved: " + event.getName());
        }

        @Override
        public void serviceResolved(ServiceEvent event) {
            // TODO Auto-generated method stub
            Log.v(TAG, "== serviceResolved: " + event.getInfo().getQualifiedName() + " port:" + event.getInfo().getPort());
        }

    };

我在 JmDNS 上使用了 addServiceTypeListener,但监听器不起作用。用在Gingerbread上 我想知道addServiceTypeListener和addserviceListener的使用方法。感谢任何回复。

4

0 回答 0