You are on page 1of 9

Mobile Application Development (22617) Practical No.

24

Practical No. 24: Develop a program for providing Bluetooth connectivity

I. Practical Significance
Bluetooth is a way to send or receive data between two different devices. Android platform
includes support for the Bluetooth framework that allows a device to wirelessly exchange data
with other Bluetooth devices.

II. Relevant Program Outcomes (POs)


PO2- Discipline knowledge
PO3- Experiments and practice
PO4- Engineering tools.
PO7- Ethics

III. Competency and Practical Skills


“Create simple Android applications.”
This practical is expected to develop the following skills
1. Able to communicate with Bluetooth hardware.
2. Able to integrate Bluetooth in any application.

IV. Relevant Course Outcome(s)


1. Configure Android environment and development tools.
2. Develop rich user Interfaces by using layouts and controls.
3. Use User Interface components for android application development.

V. Practical Outcome (PrOs)


Develop a program for Bluetooth.

VI. Relevant Affective Domain Related Outcome(s)


1. Work collaboratively in team.
2. Follow ethical Practices.

VII. Minimum Theoretical Background


Android provides Bluetooth API to perform these different operations.
1. Scan for other Bluetooth devices
2. Get a list of paired devices.
3. Connect to other devices through service discovery.
Android provides Bluetooth Adapter class to communicate with Bluetooth. Create an object
of this calling by calling the static method getDefaultAdapter(). Its syntax is given below.
private BluetoothAdapter BA;
BA = BluetoothAdapter.getDefaultAdapter();

In order to enable the Bluetooth of your device, call the intent with the following Bluetooth
constant ACTION_REQUEST_ENABLE. Its syntax is.
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);

Maharashtra State Board of Technical Education 1


Mobile Application Development (22617) Practical No. 24

Once you enable the Bluetooth, you can get a list of paired devices by calling
getBondedDevices() method. It returns a set of Bluetooth devices. Its syntax is.
private
Set<BluetoothDevice>pairedDevi
ces; pairedDevices = BA.getBondedDevices();

VIII. Resources required (Additional)

Sr. Instrument /Object Specification Quantity Remarks


No.
Android enabled 2 GB RAM 1 Data cable is
smartphone / Android mandatory
1
version supporting emulator for
emulators

IX. Practical related Questions


Note: Below given are few sample questions for reference. Teachers must design more
such questions to ensure the achievement of identified CO.
1. Name the methods which are used to enable and disable Bluetooth adapter.
2. Explain the purpose of ACTION_REQUEST_DISCOVERABLE Constant.
3. List the uses of setName(String name)method.

(Space for answers)


1.
To enable:
boolean enable()
To disable:
boolean disable()

2.
ACTION_REQUEST_DISCOVERABLE
Activity Action: Show a system activity that requests discoverable mode. This activity will also
request the user to turn on Bluetooth if it is not currently enabled.

3.
public boolean setName (String name)
Set the friendly Bluetooth name of the local Bluetooth adapter. This name is visible to remote
Bluetooth devices.

X. Exercise
Note: Faculty must ensure that every group of students use different input value.
(Use blank space for answers or attach more pages if needed)
1. Write a program to turn on, get visible, list devices and turnoff Bluetooth with the help of
following GUI

Maharashtra State Board of Technical Education 2


Mobile Application Development (22617) Practical No. 24

Answer:

//MainActivity.java

package com.jamiapolytechnic.experiment241;

import android.app.Activity;

import android.bluetooth.BluetoothAdapter;

import android.bluetooth.BluetoothDevice;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.ListView;

import android.widget.Toast;

import java.util.ArrayList;

import java.util.Set;

public class MainActivity extends Activity {

Button b1,b2,b3,b4;

Maharashtra State Board of Technical Education 3


Mobile Application Development (22617) Practical No. 24

private BluetoothAdapter BA;

private Set<BluetoothDevice>pairedDevices;

ListView lv;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

b1 = (Button) findViewById(R.id.button1);

b2=(Button)findViewById(R.id.button2);

b3=(Button)findViewById(R.id.button3);

b4=(Button)findViewById(R.id.button4);

BA = BluetoothAdapter.getDefaultAdapter();

lv = (ListView)findViewById(R.id.listView);

public void on(View v){

if (!BA.isEnabled()) {

Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(turnOn, 0);

Toast.makeText(getApplicationContext(), "Turned on",Toast.LENGTH_LONG).show();

else {

Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_LONG).show();

public void off(View v){

Maharashtra State Board of Technical Education 4


Mobile Application Development (22617) Practical No. 24

BA.disable();

Toast.makeText(getApplicationContext(), "Turned off" ,Toast.LENGTH_LONG).show();

public void visible(View v){

Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

startActivityForResult(getVisible, 0);

public void list(View v){

pairedDevices = BA.getBondedDevices();

ArrayList list = new ArrayList();

for(BluetoothDevice bt : pairedDevices)

list.add(bt.getName());

Toast.makeText(getApplicationContext(), "Showing Paired


Devices",Toast.LENGTH_SHORT).show();

final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,


list);

lv.setAdapter(adapter);

//=====================================================================

//activity_main.xml

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

<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"

Maharashtra State Board of Technical Education 5


Mobile Application Development (22617) Practical No. 24

android:padding="10dp"

tools:context=".MainActivity" >

<TextView android:text="Bluetooth"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/textview"

android:textSize="30dp" />

<Button

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Turn On"

android:layout_below="@id/textview"

android:onClick="on" />

<Button

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Get visible"

android:onClick="visible"

android:layout_below="@id/button1" />

<Button

android:id="@+id/button3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="List devices"

android:onClick="list"

Maharashtra State Board of Technical Education 6


Mobile Application Development (22617) Practical No. 24

android:layout_below="@id/button2" />

<Button

android:id="@+id/button4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Turn Off"

android:onClick="off"

android:layout_below="@id/button3" />

<TextView

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Paired devices:"

android:textColor="#ff34ff06"

android:textSize="25dp"

android:layout_below="@id/button4" />

<ListView

android:id="@+id/listView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/textView2" />

</RelativeLayout>

//=====================================================================

//AndroidManifest.xml

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

Maharashtra State Board of Technical Education 7


Mobile Application Development (22617) Practical No. 24

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.jamiapolytechnic.experiment241">

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

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

<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/Theme.Experiment241">

<activity android:name=".MainActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

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

</intent-filter>

</activity>

</application>

</manifest>

Maharashtra State Board of Technical Education 8


Mobile Application Development (22617) Practical No. 24

XI. References / Suggestions for further Reading


1. https://www.tutorialspoint.com/android
2. https://stuff.mit.edu
3. https://www.tutorialspoint.com/android/android_advanced_tutorial.pdf
4. https://developer.android.com

XII. Assessment Scheme

Performance indicators Weightage

Process related (10 Marks) 30%

1. Logic Formation 10%


2. Debugging ability 15%
3. Follow ethical practices 5%
Product related (15 Marks) 70%

4. Interactive GUI 20%


5. Answer to Practical related questions 20%
6. Expected Output 20%
7. Timely Submission 10%
Total (25 Marks) 100%

List of student Team Members


1
2
3
4

Marks Obtained Dated signature of


Teacher
Process Product Total
Related(10) Related(15) (25)

Maharashtra State Board of Technical Education 9

You might also like