Forked from icastell/NetworkServiceDiscovery.java
Last active
August 29, 2015 14:18
-
-
Save rameshakulapc/2f20574d6d3ae8462adc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.solarfighter.activities; | |
import java.io.IOException; | |
import java.net.InetAddress; | |
import javax.jmdns.JmDNS; | |
import javax.jmdns.ServiceEvent; | |
import javax.jmdns.ServiceListener; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.net.nsd.NsdManager; | |
import android.net.nsd.NsdServiceInfo; | |
import android.net.wifi.WifiManager; | |
import android.net.wifi.WifiManager.MulticastLock; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import com.actionbarsherlock.app.SherlockFragmentActivity; | |
import com.solarfighter.R; | |
import com.solarfighter.tools.Log; | |
public class NewInstallationActivity extends SherlockFragmentActivity { | |
// Bonjour Pre JellyBean | |
private MulticastLock lock; | |
private static final String DNS_TYPE = "_http._tcp.local."; | |
private static final String DNS_NAME = "Solarfighter Gateway"; | |
private JmDNS jmdns = null; | |
private ServiceListener listener; | |
// Bonjour JellyBean | |
private NsdManager.DiscoveryListener discoveryListener; | |
private NsdManager mNsdManager; | |
private Button doneButton; | |
private View backButton; | |
/** | |
* @see android.app.Activity#onCreate(Bundle) | |
*/ | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_new_installation); | |
doneButton = (Button) findViewById(R.id.done_button); | |
doneButton.setOnClickListener(clickListener); | |
// Back button | |
backButton = findViewById(R.id.back_button); | |
backButton.setClickable(true); | |
backButton.setOnClickListener(clickListener); | |
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { | |
// setUpDiscoInfo(); | |
// } else { | |
// Set up Bonjour | |
Thread t = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
setUpDiscoInfoPreJellyBean(); | |
} | |
}); | |
t.start(); | |
// } | |
} | |
protected void onResume() { | |
super.onResume(); | |
if (jmdns != null) { | |
jmdns.addServiceListener(DNS_TYPE, listener); | |
} | |
if(mNsdManager!=null){ | |
mNsdManager.discoverServices(DNS_TYPE, NsdManager.PROTOCOL_DNS_SD, discoveryListener); | |
} | |
}; | |
protected void onStop() { | |
super.onStop(); | |
if (jmdns != null) { | |
jmdns.removeServiceListener(DNS_TYPE, listener); | |
} | |
if(mNsdManager!=null){ | |
mNsdManager.stopServiceDiscovery(discoveryListener); | |
} | |
}; | |
protected void onDestroy() { | |
super.onDestroy(); | |
if (jmdns != null) { | |
try { | |
jmdns.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
if (lock != null) { | |
lock.release(); | |
} | |
}; | |
OnClickListener clickListener = new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
switch (v.getId()) { | |
case R.id.back_button: | |
finish(); | |
break; | |
case R.id.done_button: | |
startActivity(new Intent(NewInstallationActivity.this, ConfigurationStepOneActivity.class)); | |
break; | |
default: | |
break; | |
} | |
} | |
}; | |
private void setUpDiscoInfo() { | |
Log.d("Setting up Bonjour"); | |
discoveryListener = new NsdManager.DiscoveryListener() { | |
@Override | |
public void onStopDiscoveryFailed(String serviceType, int errorCode) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onStartDiscoveryFailed(String serviceType, int errorCode) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onServiceLost(NsdServiceInfo serviceInfo) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onServiceFound(NsdServiceInfo serviceInfo) { | |
Log.d("Service resolved: " + serviceInfo.getServiceName() + " host:" + serviceInfo.getHost() + " port:" | |
+ serviceInfo.getPort() + " type:" + serviceInfo.getServiceType()); | |
} | |
@Override | |
public void onDiscoveryStopped(String serviceType) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onDiscoveryStarted(String serviceType) { | |
// TODO Auto-generated method stub | |
} | |
}; | |
mNsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE); | |
mNsdManager.discoverServices(DNS_TYPE, NsdManager.PROTOCOL_DNS_SD, discoveryListener); | |
} | |
private void setUpDiscoInfoPreJellyBean() { | |
Log.d("Setting up Bonjour Pre-JellyBean"); | |
listener = new ServiceListener() { | |
@Override | |
public void serviceResolved(ServiceEvent event) { | |
Log.d("Service resolved: " + event.getInfo().getQualifiedName() + " port:" + event.getInfo().getPort() | |
+ " domain:" + event.getInfo().getDomain()); | |
} | |
@Override | |
public void serviceRemoved(ServiceEvent event) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void serviceAdded(ServiceEvent event) { | |
// TODO Auto-generated method stub | |
} | |
}; | |
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); | |
lock = wifi.createMulticastLock("SolarFighterBonjour"); | |
lock.setReferenceCounted(true); | |
lock.acquire(); | |
try { | |
// Bug http://stackoverflow.com/a/13677686/1143172 | |
int intaddr = wifi.getConnectionInfo().getIpAddress(); | |
byte[] byteaddr = new byte[] { (byte) (intaddr & 0xff), (byte) (intaddr >> 8 & 0xff), | |
(byte) (intaddr >> 16 & 0xff), (byte) (intaddr >> 24 & 0xff) }; | |
InetAddress addr = InetAddress.getByAddress(byteaddr); // Need to | |
// process | |
// UnknownHostException | |
jmdns = JmDNS.create(addr); | |
jmdns.addServiceListener(DNS_TYPE, listener); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment