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

Prac 24

Uploaded by

kaverinagare39
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)
22 views4 pages

Prac 24

Uploaded by

kaverinagare39
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

Practical No:=24

Title:=Develop a program for Bluetooth Connectivity


Rollno:=26 Batch:=B
_____________________________________________________________________________________
<?xml version="1.0" encoding="utf-8"?> android:layout_width="match_parent"
<LinearLayout android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk android:layout_marginTop="20dp">
/res/android"
android:layout_width="match_parent" <TextView
android:layout_height="match_parent" android:id="@+id/textView"
android:orientation="vertical" android:layout_width="match_parent"
android:padding="20dp" android:layout_height="wrap_content"
android:gravity="center" android:padding="12dp"
android:background="#E0E0E0"> android:textSize="16sp"

<Button android:background="@android:color/darker_gr
android:id="@+id/btnOn" ay"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:textColor="@android:color/white"/>
android:text="Turn Bluetooth ON" </ScrollView>
android:padding="10dp"/>
</LinearLayout>
<Button package com.example.bluetooth_connectivity;
android:id="@+id/btnOff"
android:layout_width="match_parent" import android.bluetooth.BluetoothAdapter;
android:layout_height="wrap_content" import android.bluetooth.BluetoothDevice;
android:text="Turn Bluetooth OFF" import android.content.Intent;
android:padding="10dp"/> import android.content.pm.PackageManager;
import android.os.Bundle;
<Button import android.view.View;
android:id="@+id/btnDiscover" import android.widget.Button;
android:layout_width="match_parent" import android.widget.TextView;
android:layout_height="wrap_content" import android.widget.Toast;
android:text="Make Discoverable"
android:padding="10dp"/> import
androidx.appcompat.app.AppCompatActivity;
<Button import androidx.core.app.ActivityCompat;
android:id="@+id/btnShow"
android:layout_width="match_parent" import java.util.Set;
android:layout_height="wrap_content"
android:text="Show Paired Devices" public class MainActivity extends
android:padding="10dp"/> AppCompatActivity {
Button btnOn, btnOff, btnDiscover, btnShow;
<ScrollView TextView tvDevices;
BluetoothAdapter bluetoothAdapter; //
Set<BluetoothDevice> pairedDevices; ActivityCompat#requestPermissions
// here to request the missing
@Override permissions, and then overriding
protected void onCreate(Bundle // public void
savedInstanceState) { onRequestPermissionsResult(int requestCode,
super.onCreate(savedInstanceState); String[] permissions,
setContentView(R.layout.activity_main); // int[]
grantResults)
// Initialize UI elements // to handle the case where the user
tvDevices = findViewById(R.id.textView); grants the permission. See the documentation
btnOn = findViewById(R.id.btnOn); // for
btnOff = findViewById(R.id.btnOff); ActivityCompat#requestPermissions for more
btnDiscover = details.
findViewById(R.id.btnDiscover); return;
btnShow = findViewById(R.id.btnShow); }
bluetoothAdapter.disable();
// Get Bluetooth Adapter showToast("Bluetooth Turned OFF");
bluetoothAdapter = });
BluetoothAdapter.getDefaultAdapter();
// Discoverable Mode
// Turn Bluetooth ON btnDiscover.setOnClickListener(v -> {
btnOn.setOnClickListener(v -> { Intent discoverIntent = new
if (!bluetoothAdapter.isEnabled()) { Intent(BluetoothAdapter.ACTION_REQUEST_
Intent intent = new DISCOVERABLE);
Intent(BluetoothAdapter.ACTION_REQUEST_E if
NABLE); (ActivityCompat.checkSelfPermission(this,
startActivityForResult(intent, 0); android.Manifest.permission.BLUETOOTH_AD
showToast("Turning Bluetooth VERTISE) !=
ON..."); PackageManager.PERMISSION_GRANTED) {
} else { return;
showToast("Bluetooth is already }
ON"); startActivityForResult(discoverIntent, 0);
} showToast("Discovering Bluetooth
}); Devices...");
});
// Turn Bluetooth OFF
btnOff.setOnClickListener(v -> { // Show Paired Devices
if btnShow.setOnClickListener(v -> {
(ActivityCompat.checkSelfPermission(this, if (bluetoothAdapter.isEnabled()) {
android.Manifest.permission.BLUETOOTH_CO tvDevices.setText("Paired
NNECT) != Devices:\n");
PackageManager.PERMISSION_GRANTED) { if
// TODO: Consider calling (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.BLUETOOTH_CO TH"/>
NNECT) != <uses-permission
PackageManager.PERMISSION_GRANTED) { android:name="android.permission.BLUETOO
return; TH_ADMIN"/>
} <uses-permission
pairedDevices = android:name="android.permission.BLUETOO
bluetoothAdapter.getBondedDevices(); TH_ADVERTISE"/>
<uses-permission
if (!pairedDevices.isEmpty()) { android:name="android.permission.BLUETOO
for (BluetoothDevice device : TH_CONNECT"/>
pairedDevices) { <uses-permission
tvDevices.append("\nDevice: " + android:name="android.permission.BLUETOO
device.getName() + " (" + device.getAddress() + TH_SCAN"/>
")");
} <application
} else { android:allowBackup="true"
tvDevices.append("\nNo Paired android:icon="@mipmap/ic_launcher"
Devices Found"); android:label="@string/app_name"
}
} else { android:roundIcon="@mipmap/ic_launcher_rou
showToast("Please enable Bluetooth nd"
first!"); android:supportsRtl="true"
}
}); android:theme="@style/Theme.Bluetooth_Conn
} ectivity"
private void showToast(String message) { tools:targetApi="31">
Toast.makeText(this, message,
Toast.LENGTH_SHORT).show(); <activity
} android:name=".MainActivity"
} android:exported="true">
<?xml version="1.0" encoding="utf-8"?> <intent-filter>
<manifest <action
xmlns:android="http://schemas.android.com/apk android:name="android.intent.action.MAIN" />
/res/android" <category
android:name="android.intent.category.LAUNC
xmlns:tools="http://schemas.android.com/tools" HER" />
> </intent-filter>
</activity>
<!-- Bluetooth Permissions --> </application>
<uses-permission
android:name="android.permission.BLUETOO </manifest>
OUTPUT:=

You might also like