JmDNS监听器无法正常工作

时间:2013-09-12 01:36:31

标签: android jmdns

我已经搜索了一些网页,但我找不到正确的答案。 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,但是监听器没有用。它用于姜饼 我想知道addServiceTypeListener和addserviceListener的使用方法。 谢谢你的回复。

0 个答案:

没有答案