Android working with Bluetooth using services

This program helps you to get the available Bluetooth devices and will list it on your screen.

public class InterfaceActivity extends Activity
{
BluetoothAdapter adapter;
ListView list;
ArrayAdapter<String> blue;

BTBondReceiver btr;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Receiver to intimate when a new device is found
IntentFilter filter = new IntentFilter(
“android.bluetooth.device.action.FOUND”);
btr=new BTBondReceiver();
this.getApplicationContext().registerReceiver(
btr, filter);

//Receiver to intimate when the scan process is completed
filter= new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(btr, filter);

adapter=BluetoothAdapter.getDefaultAdapter();
list=(ListView)findViewById(R.id.l);
blue= new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_dropdown_item_1line);
}

public void doDis(View v)
{
// TODO Auto-generated method stub
Log.d(“Clicked”,”click”);
try
{
if(!adapter.isDiscovering())
adapter.startDiscovery(); //start searching
else
Log.d(“already available”,”click”);
blue.clear(); //clear the adapter for showing new list of devices
}
catch(Exception e)
{
e.printStackTrace();
}
}

private class BTBondReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

try
{
String action=intent.getAction();
if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
{
Log.d(“Receiver”, “Discovery finished”);
if(blue.getCount()>0)
{
list.setAdapter(blue);
Log.d(“Listttttttt”, “Items”);
}
}
else if(BluetoothDevice.ACTION_FOUND.equals(action))
{

//one new device is found
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(device.getName(), “BOND_STATED = ” + device.getAddress());
blue.add(device.getName());
}
}
catch(Exception e)
{
e.printStackTrace();
}

}

protected void onDestroy()
{
super.onDestroy();
unregisterReceiver(btr);
}
}

xml:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical” >

<Button
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”scan”
android:onClick=”doDis”/>
<ListView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/l”
android:background=”@android:color/white”/>

</LinearLayout>

*Dont forget to give permissions

ManifestFile:

<uses-permission android:name=”android.permission.BLUETOOTH” />
<uses-permission android:name=”android.permission.BLUETOOTH_ADMIN”/>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s