Saturday, 16 August 2014

Resistance Calculator App

This is the java file for a resistance calculator. I simply used 4 spinners to select the color values. It'll calculate the resistance value on clicking submit. I'll upload the XML file on request.

package com.example.resistancecalcu;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {
String band1,band2,band3,band4;
Spinner spinner1,spinner2,spinner3,spinner4;
TextView tv;
int a,b,c,d,temp,x,p=0;
String let;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner1=(Spinner) findViewById(R.id.spinner1);
        tv=(TextView)findViewById(R.id.textView1);
        spinner2=(Spinner) findViewById(R.id.spinner2);
        spinner3=(Spinner) findViewById(R.id.spinner3);
        spinner4=(Spinner) findViewById(R.id.spinner4);
  ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.colors, android.R.layout.simple_spinner_item);
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this,
       R.array.fourth, android.R.layout.simple_spinner_item);
 adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner2.setAdapter(adapter);
spinner3.setAdapter(adapter);
spinner4.setAdapter(adapter1);

    }
    public void value(View v)
    {
    band1=spinner1.getSelectedItem().toString();
    band2=spinner2.getSelectedItem().toString();
    band3=spinner3.getSelectedItem().toString();
    band4=spinner4.getSelectedItem().toString();
    if(band1.equals("Black"))
    a=0;
    else if(band1.equals("Brown"))
    a=1;
    else if(band1.equals("Red"))
    a=2;
    else if(band1.equals("Orange"))
    a=3;
    else if(band1.equals("Yellow"))
    a=4;
    else if(band1.equals("Green"))
    a=5;
    else if(band1.equals("Blue"))
    a=6;
    else if(band1.equals("Violet"))
    a=7;
    else if(band1.equals("Grey"))
    a=8;
    else
    a=9;
    if(band2.equals("Black"))
    b=0;
    else if(band2.equals("Brown"))
    b=1;
    else if(band2.equals("Red"))
    b=2;
    else if(band2.equals("Orange"))
    b=3;
    else if(band2.equals("Yellow"))
    b=4;
    else if(band2.equals("Green"))
    b=5;
    else if(band2.equals("Blue"))
    b=6;
    else if(band2.equals("Violet"))
    b=7;
    else if(band2.equals("Grey"))
    b=8;
    else
    b=9;
 
    temp=a*10+b;
    if(band3.equals("Black"))
    {
    c=0;
    x=1;
    let="";
   
    }
    else if(band3.equals("Brown"))
    {
    c=1;
    x=10;
    let="";
   
    }
    else if(band3.equals("Red"))
    {
    c=2;
    x=100;
    let="";
    }
    else if(band3.equals("Orange"))
    {
    c=3;
    x=1;
    let="K";
    }
    else if(band3.equals("Yellow"))
    {
    c=4;
    x=10;
    let="K";
    }
    else if(band3.equals("Green"))
    {
    c=5;
    x=100;
    let="K";
    }
    else if(band3.equals("Blue"))
    {
    c=6;
    x=1;
    let="M";
    }
    else if(band3.equals("Violet"))
    {
    c=7;
    x=10;
    let="M";
    }
    else if(band3.equals("Grey"))
    {
    c=8;
    x=100;
    let="M";
    }
    else
    {
    c=9;
    x=1;
    let="M";
    }
    if(band4.equals("Silver"))
    {
    p=10;
    }
    else if(band4.equals("Gold"))
    {
    p=5;
    }
 
 
    if(band4.equals("No color"))
    tv.setText(""+temp*x+" "+let+" ohm");
    else
    tv.setText(""+temp*x+" "+let+" ohm"+" "+p+"%");
 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
 
}

Friday, 8 August 2014

Telephony Manager (Blacklist/Whitelist/Picks up call automatically/Emergency Call) for Android

This is the main java code for implementing a blacklist/whitelist telephone manager for android. Additional features include picking up the call automatically as well as sending emergency calls.. If any doubts please do comment. I'll upload the XML scripts on request.
JAVA CODE
package com.example.phone;

import java.lang.reflect.Method;



import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
int i=0;
RelativeLayout back;
ArrayAdapter<String> listAdapter;
ListView listView;
ArrayAdapter<String> listAdapter1;
ListView listView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
TelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
listView=(ListView) findViewById(R.id.black);
listView1=(ListView) findViewById(R.id.ListView01);
listAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,0);
listAdapter1= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,0);
listView.setAdapter(listAdapter);
listView1.setAdapter(listAdapter1);
back=(RelativeLayout) findViewById(R.id.back1);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void wl(View v)
{
EditText ed=(EditText) findViewById(R.id.editText1);
String s= ed.getText().toString();
listAdapter.add(s);


}
public void bl(View v)
{
EditText ed=(EditText) findViewById(R.id.editText1);
String s= ed.getText().toString();
listAdapter1.add(s);


}
public void accept(View v)
{
TextView tv=(TextView) findViewById(R.id.tv);
tv.setText("ACCEPTING CALLS\nOnly Numbers on the\nBlackList will not be\nautomatically accepted");
i=2;
back.setBackgroundResource(R.drawable.accept);
}
public void reject(View v)
{
i=1;
TextView tv=(TextView) findViewById(R.id.tv);
tv.setText("REJECTING CALLS\nOnly Numbers on the\nWhiteList will be Accepted");
back.setBackgroundResource(R.drawable.reject);
}
public void emer(View v)
{
Uri number = Uri.parse("tel:5551234");
Intent callIntent = new Intent(Intent.ACTION_CALL, number);

startActivity(callIntent);
}
// public class PhoneCallReceiver extends BroadcastReceiver {
// private ITelephony telephonyService;
//}

class TeleListener extends PhoneStateListener {
 public void onCallStateChanged(int state, String incomingNumber) {
  super.onCallStateChanged(state, incomingNumber);
  switch (state) {
  case TelephonyManager.CALL_STATE_IDLE:
   // CALL_STATE_IDLE;
   Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
     Toast.LENGTH_LONG).show();
   break;
  case TelephonyManager.CALL_STATE_OFFHOOK:
   // CALL_STATE_OFFHOOK;
   Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
     Toast.LENGTH_LONG).show();
   break;
  case TelephonyManager.CALL_STATE_RINGING:
   // CALL_STATE_RINGING
  if(i==1)
  {


  if(listAdapter.getCount()>=0)
  {
  Toast.makeText(getApplicationContext(), ""+listAdapter.getPosition(incomingNumber), Toast.LENGTH_SHORT).show();
  if(listAdapter.getPosition(incomingNumber)!=-1)
  Toast.makeText(getApplicationContext(), "Call is on WhiteList", Toast.LENGTH_SHORT).show();

  else
  endcal();
  }
else

  endcal();

  }
  else if(i==2)
  {

  if(listAdapter1.getCount()>=0)
  {
  Toast.makeText(getApplicationContext(), ""+listAdapter1.getPosition(incomingNumber), Toast.LENGTH_SHORT).show();
  if(listAdapter1.getPosition(incomingNumber)!=-1)
  Toast.makeText(getApplicationContext(), "Call is on BlackList", Toast.LENGTH_SHORT).show();
  else
  acceptcall();
  }
else


  acceptcall();
  }
   Toast.makeText(getApplicationContext(), incomingNumber,
     Toast.LENGTH_LONG).show();
   Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING",
     Toast.LENGTH_LONG).show();
   break;
  default:
   break;
  }
 }

private void endcal() {
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
try{

Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
Object telephonyService = m.invoke(tm); // Get the internal ITelephony object
c = Class.forName(telephonyService.getClass().getName()); // Get its class
m = c.getDeclaredMethod("endCall"); // Get the "endCall()" method
m.setAccessible(true); // Make it accessible
m.invoke(telephonyService); // invoke endCall()
Toast.makeText(getApplicationContext(), "CALL IS REJECTED", Toast.LENGTH_SHORT).show();
}
catch (Exception e){}// TODO Auto-generated method stub

}
private void acceptcall() {
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

try{
Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
   buttonUp.putExtra(Intent.EXTRA_KEY_EVENT,
     new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
   getApplicationContext().sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
Toast.makeText(getApplicationContext(), "CALL IS accepted", Toast.LENGTH_SHORT).show();
}
catch (Exception e){}// TODO Auto-generated method stub

}
}
}

(Arduino) Room Automation -Bluetooth + NFC for Android

This is the main java code for implementing room automation with bluetooth (Arduino Board). We've also implement NFC in this code. If any doubts please do comment. I'll upload the XML scripts on request.
The Arduino code is at the bottom. Modify as required.
JAVA FILE

package com.example.arduino3;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;

import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.tech.NfcV;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.app.Activity;
import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

ArrayAdapter<String> listAdapter;
ListView listView;
ImageView iv;
BluetoothAdapter btAdapter;
ImageView b1;
int c = 2;
int z = 10;
int x = 5;
int n = 0;
boolean a = false;
boolean b = false;
int successconnection = 0;

// NFC CODE
boolean con = false;
NfcAdapter myNfcAdapter;
TextView view;
TextView v2;
TextView v3;

StringBuilder myText = new StringBuilder();
IntentFilter[] intentFiltersArray;
PendingIntent pendingIntent;
String[][] techListsArray;
String[] ElementList = null;
// ends here

public static String address = "20:13:09:12:26:11";
Set<BluetoothDevice> devicesArray;
ArrayList<String> pairedDevices;
ArrayList<BluetoothDevice> devices;
public static final UUID MY_UUID = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");
protected static final int SUCCESS_CONNECT = 0;
protected static final int MESSAGE_READ = 1;
protected static final int bdli = 2;
protected static final int acli = 3;
IntentFilter filter;
RelativeLayout k;
public ConnectedThread mConnectedThread;
TextView bd;
BroadcastReceiver receiver = null;
String tag = "debugging";
protected static final int AC = 0;
BluetoothSocket msock;
// Handler- to control the flow of messages between the main thread and ConnectThread
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Log.i(tag, "in handler");
super.handleMessage(msg);
//A case statement is used to determine which elements are to be turned on or off
switch (msg.what) {
case SUCCESS_CONNECT:

// DO something
msock = (BluetoothSocket) msg.obj;
ConnectedThread connectedThread = new ConnectedThread(
(BluetoothSocket) msg.obj);
// ((BluetoothSocket)msg.obj)
Toast.makeText(getApplicationContext(),
"CONNECTION IS ESTABLISHED", Toast.LENGTH_SHORT).show();

// String s = "1";
if (n == 0)
setContentView(R.layout.third);
Log.i(tag, "connected");
/*
* if (n == 1) { int numn = 0; connectedThread.write(numn); }
*/
break;

// next();
// finish();

case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
String string = new String(readBuf);
Toast.makeText(getApplicationContext(), string,
Toast.LENGTH_SHORT).show();
break;

case bdli:
// Toast.makeText(getApplicationContext(),
// "HI",Toast.LENGTH_SHORT).show();
ConnectedThread connectedThread1 = new ConnectedThread(
(BluetoothSocket) msg.obj);
if (a == true)
a = false;
else
a = true;
if (a == true) {
int num = 0;
Toast.makeText(getApplicationContext(), "Element is on",
Toast.LENGTH_SHORT).show();
connectedThread1.write(num);
} else if (a == false) {
int num = 2;
Toast.makeText(getApplicationContext(), "Element is off",
Toast.LENGTH_SHORT).show();
connectedThread1.write(num);
}

break;
case acli:// Toast.makeText(getApplicationContext(),
// "HI",Toast.LENGTH_SHORT).show();
ConnectedThread connectedThread2 = new ConnectedThread(
(BluetoothSocket) msg.obj);
if (b == true)
b = false;
else
b = true;
if (b == true) {
int nume = 1;
Toast.makeText(getApplicationContext(), "Element is on",
Toast.LENGTH_SHORT).show();
connectedThread2.write(nume);
} else if (b == false) {
int nume = 3;
Toast.makeText(getApplicationContext(), "Element is off",
Toast.LENGTH_SHORT).show();
connectedThread2.write(nume);
}

break;

}
}

// public void next() {
//
// Intent nextScreen= new
// Intent(getApplicationContext(),SecondScreenActivity.class);
// startActivity(nextScreen);
// TODO Auto-generated method stub

// }
};

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
// Intent nextScreen=new
// Intent(getApplicationContext(),Main1Activity.class);
// startActivity(nextScreen);

setContentView(R.layout.activity_main);

getActionBar().hide();
/*
* getWindow().getDecorView().setSystemUiVisibility(
* View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
* View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
* View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
* View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN
* | View.SYSTEM_UI_FLAG_IMMERSIVE);
*/
Toast.makeText(getApplicationContext(),
"Welcome to the Arduino Interface", Toast.LENGTH_SHORT).show();
// here we initialise the components and check whether a bluetooth adapter is present and whether it is turned on or off
init();
if (btAdapter == null) {
Toast.makeText(getApplicationContext(), "No bluetooth detected",
Toast.LENGTH_SHORT).show();
finish();
} else {
if (btAdapter.isEnabled()) {
} else {

turnOnBT();
}

}

// nfc
initialise();
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
//Intent filter filters NDEF tags
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("text/plain"); /*
* Handles all MIME based
* dispatches. You should specify
* only the ones that you need.
*/
} catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
intentFiltersArray = new IntentFilter[] { ndef, };
techListsArray = new String[][] { new String[] { NfcV.class.getName() } };

}

//Initialises NFC and checks for NFC tag intent

private void initialise() {
myNfcAdapter = NfcAdapter.getDefaultAdapter(this);

//checks for NFC device
if (myNfcAdapter == null) {
Toast.makeText(getApplicationContext(), "NOT FOUND",
Toast.LENGTH_SHORT).show();
} else {
// Toast.makeText(getApplicationContext(), "FOUND",
// Toast.LENGTH_SHORT).show();

}

if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
resolveIntent(getIntent());
}
}

//Payload in NDEF data from tag is used for identifying
   //Room and corresponding xml is set
private void resolveIntent(Intent intent) {
// TODO Auto-generated method stub
String payload = new String();
String text = new String();
NdefMessage[] messages = getNdefMessages(intent);

for (int i = 0; i < messages.length; i++) {
myText.append("Message " + (i + 1) + "\n");
for (int j = 0; j < messages[0].getRecords().length; j++) {
NdefRecord record = messages[i].getRecords()[j];
myText.append((j + 1) + "th. Record Tnf: " + record.getTnf()
+ "\n");
myText.append((j + 1) + "th. Record type: "
+ new String(record.getType()) + "\n");
myText.append((j + 1) + "th. Record id: "
+ new String(record.getId()) + "\n");
payload = new String(record.getPayload());
text = payload;
myText.append((j + 1) + "th. Record payload: " + payload + "\n");
}

}

// setContentView(R.layout.nfcjoe);
ElementList = text.split(" ");

//Room selection based on NFC data
if (ElementList[0].equals("R1") == true) {
n = 1;
setContentView(R.layout.activity_main);
init();
func();

} else if (ElementList[0].equals("R2") == true) {
n = 2;
setContentView(R.layout.activity_main);
init();
func();
}


}
//NDEF message is read from Tag and is stored in variable
//message of type NdefMessage
NdefMessage[] getNdefMessages(Intent intent) {
NdefMessage[] message = null;

if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Parcelable[] rawMessages = intent
.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMessages != null) {
message = new NdefMessage[rawMessages.length];
for (int i = 0; i < rawMessages.length; i++) {
message[i] = (NdefMessage) rawMessages[i];
}
} else {
byte[] empty = new byte[] {};
NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN,
empty, empty, empty);
NdefMessage msg = new NdefMessage(new NdefRecord[] { record });
message = new NdefMessage[] { msg };
}
} else {
Log.d("", "Unknown intent.");
finish();
}
return message;

}

public void accli(View v) {// Toast.makeText(getApplicationContext(),
// "AIRCONDITIONING IS TURNING ON",
// Toast.LENGTH_SHORT).show();

mHandler.obtainMessage(acli, msock).sendToTarget();

}

public void Bdlight(View v) {

// Toast.makeText(getApplicationContext(),
// "BEDROOM LIGHT IS TURNING ON", Toast.LENGTH_SHORT).show();
bd = (TextView) findViewById(R.id.textView1);

mHandler.obtainMessage(bdli, msock).sendToTarget();

// sendData("0");

// if(c%2==0)
// bd.setText("STATE ON");
// else
// bd.setText("STATE OFF");
// c++;
}

public void context(int i) {
// TODO Auto-generated method stub

}

public void turnon(View v) {
btAdapter.enable();
k.setBackgroundResource(R.drawable.on);
Toast.makeText(getApplicationContext(), "YOU TURNED ON THE BT",
Toast.LENGTH_SHORT).show();
}

public void four(View v) {
setContentView(R.layout.four);
}

public void back(View v) {
setContentView(R.layout.third);
}

public void fourth(View v) {
setContentView(R.layout.fourth);
}

public void turnoff(View v) {
btAdapter.disable();
k.setBackgroundResource(R.drawable.dead);
Toast.makeText(getApplicationContext(), "YOU TURNED OFF THE BT",
Toast.LENGTH_SHORT).show();
finish();
}

public void can(View v) {
btAdapter.cancelDiscovery();
k.setBackgroundResource(R.drawable.off);

Toast.makeText(getApplicationContext(), "KILL THE DISCOVERY",
Toast.LENGTH_SHORT).show();
}

public void sup(View v) {
// we enter into this function on clicking the bluetooth icon
// this method starts the discovery for bluetooth devices
// TODO Auto-generated method stub
btAdapter.cancelDiscovery();
View b = findViewById(R.id.imageButton1);

View tnih1 = findViewById(R.id.textView1);
View tnih2 = findViewById(R.id.textView2);
tnih1.setVisibility(View.GONE);
tnih2.setVisibility(View.GONE);
b.setVisibility(View.GONE);
k.setBackgroundResource(R.drawable.s3);

Toast.makeText(getApplicationContext(), "SEARCHING", Toast.LENGTH_SHORT)
.show();

func();

btAdapter.startDiscovery();
getPairedDevices();

}

private void func() {
// TODO Auto-generated method stub
// The broadcast receiver picks up any bluetooth devices found based on the filter set to "Action found"
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();

if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

if (device.getName().toString().equals("HC-06")) {
if (btAdapter.isDiscovering()) {
btAdapter.cancelDiscovery();
}

BluetoothDevice selectedDevice = device;
ConnectThread connect = new ConnectThread(
selectedDevice);
connect.start();
Toast.makeText(getApplicationContext(),
"ATTEMPTING TO CONNECT", Toast.LENGTH_SHORT)
.show();
Log.i(tag, "in click listener");
} else {
Toast.makeText(getApplicationContext(),
"Device ISNT PAIRED", Toast.LENGTH_SHORT)
.show();
}

if (n == 1)
setContentView(R.layout.four);
else if (n == 2)
setContentView(R.layout.fourth);
//Here we display the bluetooth devices nearby which are paired into our listview
devices.add(device);
String s = "";
for (int a = 0; a < pairedDevices.size(); a++) {
if (device.getName().equals(pairedDevices.get(a))) {
// append
s = "(Paired)";
break;
}
}

listAdapter.add(device.getName() + " " + s + " " + "\n");
}

// else
// if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
// Toast.makeText(getApplicationContext(),
// "DISCOVERY STARTED AGAIN", Toast.LENGTH_SHORT).show();
// }
// else
// if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
// Toast.makeText(getApplicationContext(),
// "DISCOVERY IS FINISHED",Toast.LENGTH_SHORT).show();

// }

}
};
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
// filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
// registerReceiver(receiver, filter);
// filter = new
// IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
// registerReceiver(receiver, filter);
// filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
// registerReceiver(receiver, filter);

}

private void turnOnBT() {
// to turn on the bluetooth
// TODO Auto-generated method stub
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, 0);


}

private void getPairedDevices() {
// TODO Auto-generated method stub
//to get a list of the paired bluetooth devices

Set<BluetoothDevice> devicesArray = btAdapter.getBondedDevices();
if (devicesArray.size() > 0) {
for (BluetoothDevice device : devicesArray) {
pairedDevices.add(device.getName());
}



}

}

private void init() {
// TODO Auto-generated method stub
// the initialisation of our various components in the app
listView = (ListView) findViewById(R.id.listView);
// listView.setOnItemClickListener(this);
listAdapter = new ArrayAdapter<String>(this, R.layout.color, 0);
listView.setAdapter(listAdapter);
k = (RelativeLayout) findViewById(R.id.back);
btAdapter = BluetoothAdapter.getDefaultAdapter();
pairedDevices = new ArrayList<String>();
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
devices = new ArrayList<BluetoothDevice>();
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null;
}
myNfcAdapter.disableForegroundDispatch(this);
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();

myNfcAdapter.enableForegroundDispatch(this, pendingIntent,
intentFiltersArray, techListsArray);

}

protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
resolveIntent(intent);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(),
"Bluetooth must be enabled to continue", Toast.LENGTH_SHORT)
.show();
finish();
}
}

/*
* public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long
* arg3) {
*
* // TODO Auto-generated method stub // con=true;
*
* if (btAdapter.isDiscovering()) { btAdapter.cancelDiscovery(); } if
* (listAdapter.getItem(arg2).contains("Paired")) {
*
* BluetoothDevice selectedDevice = devices.get(arg2); ConnectThread connect
* = new ConnectThread(selectedDevice); connect.start();
* Toast.makeText(getApplicationContext(), "ATTEMPTING TO CONNECT",
* Toast.LENGTH_SHORT).show(); Log.i(tag, "in click listener"); } else {
* Toast.makeText(getApplicationContext(), "Device ISNT PAIRED",
* Toast.LENGTH_SHORT).show(); }
*
* if (n == 1) setContentView(R.layout.four); else if (n == 2)
* setContentView(R.layout.fourth);
*
*
* }
*/

private class ConnectThread extends Thread {
// Used to create a connection through bluetooth to the device we wish to connect to
private final BluetoothSocket mmSocket;

// private final BluetoothDevice mmDevice;

public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
// mmDevice = device;
Log.i(tag, "construct");
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server
// code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.i(tag, "get socket failed");

}
mmSocket = tmp;
}

public void run() {
// Cancel discovery because it will slow down the connection
btAdapter.cancelDiscovery();
Log.i(tag, "connect - run");
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
Log.i(tag, "connect - succeeded");
} catch (IOException connectException) {
Log.i(tag, "connect failed");
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) {
}
return;
}

// Do work to manage the connection (in a separate thread)
// The handler which can be used to pass messages between the main thread and the current thread
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();

}

/** Will cancel an in-progress connection, and close the socket */
// public void cancel() {
// try {
// mmSocket.close();
// } catch (IOException e) { }
// }
}

private class ConnectedThread extends Thread {
// Used the manage the bluetooth connection by sending the data to the bluetooth module
// private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;

public ConnectedThread(BluetoothSocket socket) {
// mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;

// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}

mmInStream = tmpIn;
mmOutStream = tmpOut;
}

public void run() {
byte[] buffer; // buffer store for the stream
int bytes; // bytes returned from read()

// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
buffer = new byte[1024];
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();

} catch (IOException e) {
break;
}
}
}

/* Call this from the main activity to send data to the remote device */
public void write(int bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) {
}
}
// public void sendData(String message)
// {
// byte[] msgBuffer=message.getBytes();
// try
// {
// mmOutStream.write(msgBuffer);
// }catch(IOException e){ }
// }

/* Call this from the main activity to shutdown the connection */
// public void cancel() {
// try {
// mmSocket.close();
// } catch (IOException e) { }
// }
}

}



ARDUINO CODE
int incomingByte = 0;   // for incoming serial data
int led=10;
int led1=12;
void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps

}


void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();
if(incomingByte==0)
{
  digitalWrite(led,HIGH);
 
 

}
else if(incomingByte==1)
{
    digitalWrite(led1,HIGH);
   

}
else if(incomingByte==2)
{
  digitalWrite(led,LOW);
 

}
else if(incomingByte==3)
{
  digitalWrite(led1,LOW);
 

}
                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
        }
}