Android App for detecting Bluetooth Devices (with Source Code)

Download APK Download Source Code Contact Us

The Android platform includes support for the Bluetooth network stack, which allows a device to wirelessly exchange data with other Bluetooth devices. The application framework provides access to the Bluetooth functionality through the Android Bluetooth APIs. These APIs let applications wirelessly connect to other Bluetooth devices, enabling point-to-point and multipoint wireless features.

About Android App:

Android operating system is the fast growing mobile operating system in the market. Android provides solution for users to develop their own apps and upload to app store and help users to download for free of cost.

Layout Code: activity_main. Xml
When application is executed this is the first layout design that will run which will load width, height, load back ground details..etc.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Text"
        android:textAppearance="?android:attr/textAppearanceLarge"></textview>

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:orientation="horizontal">

        <Button android:id="@+id/turnOn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/on"></Button>

        <Button android:id="@+id/visible"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/visible"></Button>
    </LinearLayout>

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:orientation="vertical">

        <Button android:id="@+id/paired"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/List"></Button>

        <Button android:id="@+id/search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Find"></Button>

        <ListView android:id="@+id/listView1"
            android:layout_width="fill_parent"
            android:layout_height="200dp">
        </ListView>
    </LinearLayout>

</RelativeLayout>

Layout Code: string. Xml
this xml file inside values folder under RES. It’s contains all text resources.

< ?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Bluetoothtwo</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="on">Turn On</string>
    <string name="List">List paired Devices</string>
    <string name="Find">Search new Devices</string>
    <string name="Text">Status: -</string>
    <string name="visible">Visible</string>
</resources>

Java File : MainActivity.java
MainActivity.java file in package com.mobilemerit.wifi.
Main activity file will load design form like buttons, text view, on click functionality, display text.

package com.mobilemerit.bluetooth;

import java.util.Set;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.mobilemerit.bluetooth.R;

public class MainActivity extends Activity implements OnClickListener {
	private static final int REQUEST_ENABLE_BT = 1;
	private Button onOffBtn;
	private Button listBtn;
	private Button findBtn;
	private Button visibleBtn;
	private TextView text;
	private BluetoothAdapter myBluetoothAdapter;
	private Set<bluetoothdevice> pairedDevices;
	private ListView myListView;
	private ArrayAdapter<string> BTArrayAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// take an instance of BluetoothAdapter - Bluetooth radio
		myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if (myBluetoothAdapter == null) {
			onOffBtn.setEnabled(false);
			listBtn.setEnabled(false);
			findBtn.setEnabled(false);
			visibleBtn.setEnabled(false);
			text.setText("Status: not supported");
			Toast.makeText(getApplicationContext(),
					"Your device does not support Bluetooth", Toast.LENGTH_LONG)
					.show();
		} else {
			text = (TextView) findViewById(R.id.text);
			onOffBtn = (Button) findViewById(R.id.turnOn);
			onOffBtn.setOnClickListener(this);
			visibleBtn = (Button) findViewById(R.id.visible);
			visibleBtn.setOnClickListener(this);
			listBtn = (Button) findViewById(R.id.paired);
			listBtn.setOnClickListener(this);
			findBtn = (Button) findViewById(R.id.search);
			findBtn.setOnClickListener(this);
			myListView = (ListView) findViewById(R.id.listView1);

			// create the arrayAdapter that contains the BTDevices, and set it
			// to the ListView
			BTArrayAdapter = new ArrayAdapter</string><string>(this,
					android.R.layout.simple_list_item_1);
			myListView.setAdapter(BTArrayAdapter);
			/*
			 * checking while app start bluetooth is On or Off if bluetooth is
			 * on then search devices and set button and status "ON" if
			 * bluetooth is off then set button and status "OFF"
			 */
			if (!myBluetoothAdapter.isEnabled()) {
				onOffBtn.setText("Turn OFF");
				text.setText("Status: Disabled");
			} else {
				onOffBtn.setText("Turn ON");
				text.setText("Status: Enabled");
				find();
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.turnOn:
			bluetoothOnOff();
			break;
		case R.id.visible:
			visible();
			break;
		case R.id.paired:
			list();
			break;
		case R.id.search:
			find();
			break;
		default:
			break;
		}
	}

	/* to enable and disable bluetooth */
	public void bluetoothOnOff() {
		if (!myBluetoothAdapter.isEnabled()) {
			Intent turnOnIntent = new Intent(
					BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT);

		} else {
			myBluetoothAdapter.disable();
			onOffBtn.setText("Turn OFF");
			BTArrayAdapter.clear();
			Toast.makeText(getApplicationContext(), "Bluetooth turned off",
					Toast.LENGTH_SHORT).show();
		}
	}

	/* to make visible your device to other devices */
	public void visible() {
		Intent getVisible = new Intent(
				BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
		startActivityForResult(getVisible, 0);
	}

	/* to search pair devices */
	public void list() {
		if (!myBluetoothAdapter.isEnabled()) {
			Toast.makeText(getApplicationContext(), "First Trun On Bluetooth",
					Toast.LENGTH_SHORT).show();
		} else {
			// get paired devices
			pairedDevices = myBluetoothAdapter.getBondedDevices();
			// put it's one to the adapter
			BTArrayAdapter.clear();
			for (BluetoothDevice device : pairedDevices)
				BTArrayAdapter.add(device.getName() + "\n"
						+ device.getAddress());
			Toast.makeText(getApplicationContext(), "Show Paired Devices",
					Toast.LENGTH_SHORT).show();
		}
	}

	/* to find all available devices */
	public void find() {
		if (!myBluetoothAdapter.isEnabled()) {
			Toast.makeText(getApplicationContext(), "First Trun On Bluetooth",
					Toast.LENGTH_SHORT).show();
		} else {
			BTArrayAdapter.clear();
			myBluetoothAdapter.startDiscovery();
			registerReceiver(bReceiver, new IntentFilter(
					BluetoothDevice.ACTION_FOUND));
		}
	}

	final BroadcastReceiver bReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			// When discovery finds a device
			if (BluetoothDevice.ACTION_FOUND.equals(action)) {
				// Get the BluetoothDevice object from the Intent
				BluetoothDevice device = intent
						.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				// add the name and the MAC address of the object to the
				// arrayAdapter
				BTArrayAdapter.add(device.getName() + "\n"
						+ device.getAddress());
				BTArrayAdapter.notifyDataSetChanged();
			}
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_ENABLE_BT) {
			if (myBluetoothAdapter.isEnabled()) {
				text.setText("Status: Enabled");
				onOffBtn.setText("Turn ON");
				Toast.makeText(getApplicationContext(), "Bluetooth turned on",
						Toast.LENGTH_SHORT).show();
				find();
			} else {
				text.setText("Status: Disabled");
				onOffBtn.setText("Turn OFF");
			}
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(bReceiver);
	}
}

Mainifest.xml
This is the main configuration file for Bluetooth android project. This xml code will load required permission and main layout by default.

< ?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mobilemerit.bluetooth"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses -sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19"></uses>
	<uses -permission android:name="android.permission.BLUETOOTH"></uses>
    <uses -permission android:name="android.permission.BLUETOOTH_ADMIN"></uses>
    <application android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="com.mobilemerit.bluetooth.MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="portrait">
            <intent -filter>
                <action android:name="android.intent.action.MAIN"></action>

                <category android:name="android.intent.category.LAUNCHER"></category>
            </intent>
        </activity>
    </application>

</manifest>
Android App for detecting Bluetooth Devices
Android App for detecting Bluetooth Devices
Android App for detecting Bluetooth Devices
Android App for detecting Bluetooth Devices

Download APK Download Source Code Contact Us

Related Posts