0% found this document useful (0 votes)
26 views4 pages

Mad 26

This document contains code for a mobile application that demonstrates Bluetooth functionality. It includes XML layout code for the main activity screen containing buttons to turn Bluetooth on/off, list paired devices, and get visibility. The Java code handles the button clicks by enabling, disabling, and getting bonded/visible devices from the Bluetooth adapter. The app also displays paired devices in a list view. The Android manifest declares the required Bluetooth permissions.

Uploaded by

Ganesh Ekambe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

Mad 26

This document contains code for a mobile application that demonstrates Bluetooth functionality. It includes XML layout code for the main activity screen containing buttons to turn Bluetooth on/off, list paired devices, and get visibility. The Java code handles the button clicks by enabling, disabling, and getting bonded/visible devices from the Bluetooth adapter. The app also displays paired devices in a list view. The Android manifest declares the required Bluetooth permissions.

Uploaded by

Ganesh Ekambe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Mobile Application Development(22617)

Name: Ekambe Ganesh Dattatraya

Roll No. :53

Practical No. : 26

Exercise

Activity_main.xml

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


<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.c
om/a pk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:transitionGroup="true" tools:context=".MainActivity">

<TextView android:id="@+id/textView" android:layout_width="wrap_content"


android:layout_height="wrap_content" android:layout_marginStart="56dp"
android:layout_marginTop="72dp" android:text="Bluetooth_example"
android:textSize="35dp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
<Button android:id="@+id/button"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="54dp" android:text="Turn On"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" android:clickable="true"
android:onClick="on"
/>
<Button android:id="@+id/button2"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="54dp" android:text="ListDevices"
app:layout_constraintEnd_toStartOf="@+id/button3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/textView" android:onClick="list"/>
<Button android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_marginTop="54dp" android:text="Turn Off"


app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/textView" android:onClick="off"/>
Mobile Application Development(22617)
<Button android:id="@+id/button4"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="136dp" android:layout_marginTop="40dp" android:text="Get
Visisble"

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" android:onClick="visible"/>
<ListView android:id="@+id/listView" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginTop="84dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button4" />
<TextView android:id="@+id/textView2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginStart="11dp"
android:layout_marginTop="27dp" android:text="Paired Device"
android:textColor="#ff34ff06" android:textSize="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button4" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.exp24;
import androidx.appcompat.app.AppCompatActivity; 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 AppCompatActivity


{Button b1,b2,b3,b4;
private BluetoothAdapter BA;
private Set<BluetoothDevice>pairedDevices; ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
b1 = findViewById(R.id.button); b2 = findViewById(R.id.button2); b3 =
findViewById(R.id.button3); b4 = findViewById(R.id.button4);
BA = BluetoothAdapter.getDefaultAdapter(); lv = findViewById(R.id.listView);
}
public void on(View v){if(!BA.isEnabled()
){
Mobile Application Development(22617)
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){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(BluetoothDevicebt : pairedDevices) list.add(bt.getName());
Toast.makeText(getApplicationContext(),"Show Paired Devices",
Toast.LENGTH_LONG).show();
final ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1,list); lv.setAdapter(adapter);
}
}

Android_Manifest.xml

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exp24">
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
Maharashtra State Board of Technical Education 166

<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.Exp24">
<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>
Mobile Application Development(22617)
Output

You might also like