I am working on a project to control lights using arduino + ethernet shield + android app. I am using android studio for app development purpose. The issue is, try-catch block has been implemented inside the OnClickListener() which doesnt seem to work. I am new to android app development and cant think of a solution for the same. The app does get installed but the buttons do not perform their function. i.e. the server doesnt receive any package. Actually, initially the targetSdkVersion was set to 8, hence the holo theme and the buttons worked properly. Once i set it to 22 (lollipop) the material theme gets applied by default and the buttons no longer work. Thanking you in advance.
public void led(String s) throws Exception
{
byte[] b=(s.getBytes());
if(isOnline())
{
serverHostname1 = new String ("192.168.1.177");
ip = InetAddress.getByName(serverHostname1);
d1 = new DatagramSocket();//}
try{
send = new DatagramPacket(b,b.length, ip, 8032);
}catch(Exception e){
}
d1.send(send);
d1.setSoTimeout(10000);
d1.receive(rec);
modifiedSentence = new String(rec.getData());
InetAddress returnIPAddress = rec.getAddress();
Toast.makeText(getApplicationContext(),"Reply from Server:"+returnIPAddress,Toast.LENGTH_LONG).show();
d1.close();
}
else
{
Toast.makeText(getApplicationContext(),"No network",Toast.LENGTH_LONG).show();
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button on= (Button)findViewById(R.id.on);
Button off= (Button)findViewById(R.id.off);
on.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
//ArduinoActivity a=new ArduinoActivity();
led("1");
Toast.makeText(getApplicationContext(),"ON",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Error::"+e);
}
}
});
off.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
//ArduinoActivity b=new ArduinoActivity();
led("2");
Toast.makeText(getApplicationContext(), "OFF",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
//TODO Auto-generated catch block
System.out.println("Error::"+e);
}
}
});
}
}