java - Connect Multiple Device using NsdManager -
i have working on android , need little bit socket programing in application.i want create own network using nsdmanager.i succefully register service in network , done connection other device.i have transfer data between devices.
problem
- everthing working fine transfer message between 2 devices problem occur while connect more 2 device.
requirement
- i want connect more 2 device , share data in connected device.
brief description of code
here have 2 class chatconnection , nsdhalper. chatconnection transfer data between 2 device , nsdhalper provice utility of nsdmanger.
first register service in network.
public void registerservice(int port) { nsdserviceinfo serviceinfo = new nsdserviceinfo(); serviceinfo.setport(port); serviceinfo.setservicename(mservicename); serviceinfo.setservicetype(service_type); mnsdmanager.registerservice( serviceinfo, nsdmanager.protocol_dns_sd, mregistrationlistener); }
than discover service
public void discoverservices() { mnsdmanager.discoverservices( service_type, nsdmanager.protocol_dns_sd, mdiscoverylistener); }
and making 1 custome adapter listing available device in network.
class devicelistadpater extends arrayadapter<nsdserviceinfo> { private list<nsdserviceinfo> items; private context context; public devicelistadpater(context context, list<nsdserviceinfo> objects) { super(context, 0, 0, objects); this.context = context; items = objects; } @override public view getview(int position, view convertview, viewgroup parent) { view v = convertview; if (v == null) { layoutinflater vi = (layoutinflater) context.getsystemservice( context.layout_inflater_service); v = vi.inflate(r.layout.row_device_list, parent, false); } nsdserviceinfo device = items.get(position); if (device != null) { textview top = (textview) v.findviewbyid(r.id.txt_devicename); if (top != null) { top.settext(device.getservicename()); } } return v; } }
and have done following connect properly
public void initializediscoverylistener() { mdiscoverylistener = new nsdmanager.discoverylistener() { @override public void ondiscoverystarted(string regtype) { log.d(tag, "service discovery started"); } @override public void onservicefound(nsdserviceinfo service) { log.d(tag, "service discovery success" + service); if (!service.getservicetype().equals(service_type)) { log.d(tag, "unknown service type: " + service.getservicetype()); } else if (service.getservicename().equals(mservicename)) { log.d(tag, "same machine: " + mservicename); } else //if (service.getservicename().contains(mservicename)) { getcallback().deviceupdate(service); } } @override public void onservicelost(nsdserviceinfo service) { log.e(tag, "service lost" + service); getcallback().connectiondone(service,false); if (mservice == service) { mservice = null; } } @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); mnsdmanager.stopservicediscovery(this); } @override public void onstopdiscoveryfailed(string servicetype, int errorcode) { log.e(tag, "discovery failed: error code:" + errorcode); mnsdmanager.stopservicediscovery(this); } }; } public void initializeresolvelistener() { mresolvelistener = new nsdmanager.resolvelistener() { @override public void onresolvefailed(nsdserviceinfo serviceinfo, int errorcode) { log.e(tag, "resolve failed" + errorcode); getcallback().connectiondone(serviceinfo,false); } @override public void onserviceresolved(nsdserviceinfo serviceinfo) { log.e(tag, "resolve succeeded. " + serviceinfo); if (serviceinfo.getservicename().equals(mservicename)) { log.d(tag, "same ip."); return; } mservice = serviceinfo; getcallback().connectiondone(serviceinfo,true); } }; } public void initializeregistrationlistener() { mregistrationlistener = new nsdmanager.registrationlistener() { @override public void onserviceregistered(nsdserviceinfo nsdserviceinfo) { mservicename = nsdserviceinfo.getservicename(); discoverservices(); } @override public void onregistrationfailed(nsdserviceinfo arg0, int arg1) { } @override public void onserviceunregistered(nsdserviceinfo arg0) { } @override public void onunregistrationfailed(nsdserviceinfo serviceinfo, int errorcode) { } }; }
i hope clear code.
and need solve problem. please if connect multiple device using nsdmanager.
i have making application using this code referance.
thanks,
onik answer.
- suppose have 3 device a1 a2 , a3.while device successfull register in network , display devices each other.while a1 request connect a2 a2 pop accept connection or decline if a2 tapped on connect 2 device connect successfull.durinng session a3 want connect a2 a3 request connect a2 again a2 pop connection. problem occurs while a2 tapped on connect button previous connection losses. requirement in previous connection still remain while a3 connect a2.
i hope onik clear problem. , in logcat nothing happen.while create new connection logcat display same while connect @ previous connection.
Comments
Post a Comment