Professional Documents
Culture Documents
24
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.
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);
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();
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
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;
Button b1,b2,b3,b4;
private Set<BluetoothDevice>pairedDevices;
ListView lv;
@Override
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);
if (!BA.isEnabled()) {
startActivityForResult(turnOn, 0);
else {
BA.disable();
startActivityForResult(getVisible, 0);
pairedDevices = BA.getBondedDevices();
for(BluetoothDevice bt : pairedDevices)
list.add(bt.getName());
lv.setAdapter(adapter);
//=====================================================================
//activity_main.xml
<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: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"
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
<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>
</intent-filter>
</activity>
</application>
</manifest>