Android Bluetooth Sample Code
Android Bluetooth Sample Code
This is a simple and powerful bluetooth app.Using this you can turn_on
bluetooth,start discoverable with a simple touch,even turn_off your
bluetooth.This will not work on emulator since bluetooth support is not added.
Here the class name is MainActivity. The data fields used are
(out,button,button1,button2,mbluetooth bluetoothAdapter).Usually view occupies a
rectangular space or area on the screen and used for event handling purpose. For
widgets view is the base class used to create interactive UI components findViewById
is the override for the method (R.id.out).BluetoothAdapter.getDefaultAdapter() is
the method used to perfom bluetooth tasks like device discovery,get query
list,instantiate a bluetoothdevice using MAC address also to create a bluetooth
serversocket.
if (mBluetoothAdapter == null) {
out.append("device not supported"); this conditions checks for bluetooth device.If no
hardware devices are founding in the device . It displays device not found [Note:This
especially happens in android emulator, because there is no support for bluetooth
hardware in android emulator]. if (!mBluetoothAdapter.isEnabled()) { when button
turn_on is clicked this method will request the bluetooth adapter to enable bluetooth
then the activity gets started .
if (!mBluetoothAdapter.isDiscovering()) {
Context context = getApplicationContext();This returns context for the entire
application. We can also use this instead of the current Activitycontext. If user needs
a context tied to the lifecycle of the entire application,not just for the current activity.
context.Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(enableBtIntent, REQUEST_DISCOVERABLE_BT); This
method instantiate bluetooth discoverable .We can also set the time for turning on
discoverable.
mBluetoothAdapter.disable();This method will turn off the bluetooth and exists the
application.
Xml code
<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"
android:background="@drawable/ic_launcher"
tools:context=".MainActivity" >
The Android compiler considers everything inside the relative layout tag to be a
parameter or a customization option until it reaches a closing tag(/>).
android:layout_width="wrap_content"
The wrap_content parameter tiles the content. The layout used for this program is
relative layout.
Manifest.xml
In manifest file these to permissions has to be added in order to start the activity
without any crash.
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
package com.example.bluetooth;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int REQUEST_ENABLE_BT = 0;
private static final int REQUEST_DISCOVERABLE_BT = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView out=(TextView)findViewById(R.id.out);
final Button button = (Button) findViewById(R.id.button1);
final Button button1 = (Button) findViewById(R.id.button2);
final Button button2 = (Button) findViewById(R.id.button3);
Xml code
<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"
android:background="@drawable/ic_launcher"
tools:context=".MainActivity" >
<TextView android:text="" android:id="@+id/out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="49dp"
android:text="TURN_ON" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="27dp"
android:text="DISCOVERABLE" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="28dp"
android:text="TURN_OFF" />
</RelativeLayout>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.bluetooth"
android:versionCode="1"
android:versionName="1.0"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.bluetooth.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>