Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: MPC Aim: Develop an application that identifies the Bluetooth devices in the
(01CT0716) wireless range.
Practical No: 08 Date: 24-10-2024 Enrollment No: 92100133056
CODE :
[Link] :
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
class MainActivity : AppCompatActivity() {
private lateinit var bluetoothAdapter: BluetoothAdapter
private val deviceList = mutableListOf<BluetoothDevice>()
private lateinit var deviceListAdapter: ArrayAdapter<String>
private lateinit var buttonSearch: Button
private lateinit var listViewDevices: ListView
private val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when([Link]) {
BluetoothDevice.ACTION_FOUND -> {
val device: BluetoothDevice? = if ([Link].SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
[Link](BluetoothDevice.EXTRA_DEVICE, BluetoothDevice::[Link])
} else {
30
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: MPC Aim: Develop an application that identifies the Bluetooth devices in the
(01CT0716) wireless range.
Practical No: 08 Date: 24-10-2024 Enrollment No: 92100133056
@Suppress("DEPRECATION")
[Link](BluetoothDevice.EXTRA_DEVICE)
}
device?.let {
if (checkBluetoothConnectPermission()) {
if () {
[Link](it)
val deviceName = getDeviceName(it)
[Link](deviceName)
}
}
}
}
BluetoothAdapter.ACTION_DISCOVERY_FINISHED -> {
[Link] = true
}
}
}
}
private fun getDeviceName(device: BluetoothDevice): String {
return if (checkBluetoothConnectPermission()) {
[Link] ?: [Link] ?: "Unknown Device"
} else {
"Unknown Device (Permission Required)"
}
}
private val bluetoothPermissionLauncher = registerForActivityResult(
[Link]()
) { permissions ->
if ([Link] { [Link] }) {
startDeviceDiscovery()
} else {
[Link](this, "Bluetooth permissions denied", Toast.LENGTH_SHORT).show()
}
}
private val enableBluetoothLauncher = registerForActivityResult(
31
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: MPC Aim: Develop an application that identifies the Bluetooth devices in the
(01CT0716) wireless range.
Practical No: 08 Date: 24-10-2024 Enrollment No: 92100133056
[Link]()
) { result ->
if ([Link] == RESULT_OK) {
startDeviceDiscovery()
} else {
[Link](this, "Bluetooth must be enabled to discover devices", Toast.LENGTH_SHORT).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
buttonSearch = findViewById([Link])
listViewDevices = findViewById([Link])
val bluetoothManager: BluetoothManager = getSystemService(BluetoothManager::[Link])
bluetoothAdapter = [Link]
if (bluetoothAdapter == null) {
[Link](this, "Bluetooth is not supported on this device", Toast.LENGTH_LONG).show()
finish()
return
}
deviceListAdapter = ArrayAdapter(this, [Link].simple_list_item_1, mutableListOf())
[Link] = deviceListAdapter
[Link] { searchDevices() }
[Link] { _, _, position, _ ->
val device = deviceList[position]
pairDevice(device)
}
// Register for broadcasts when a device is discovered
val filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
[Link](BluetoothAdapter.ACTION_DISCOVERY_FINISHED)
registerReceiver(receiver, filter)
}
32
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: MPC Aim: Develop an application that identifies the Bluetooth devices in the
(01CT0716) wireless range.
Practical No: 08 Date: 24-10-2024 Enrollment No: 92100133056
private fun searchDevices() {
if (!checkBluetoothPermissions()) {
requestBluetoothPermissions()
} else {
startDeviceDiscovery()
}
}
private fun startDeviceDiscovery() {
if (!checkBluetoothPermissions()) {
requestBluetoothPermissions()
return
}
[Link] = false
[Link]()
[Link]()
if (![Link]) {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
[Link](enableBtIntent)
} else {
if ([Link](
this,
[Link].BLUETOOTH_SCAN
) != PackageManager.PERMISSION_GRANTED
){
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return
}
if ([Link]) {
[Link]()
33
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: MPC Aim: Develop an application that identifies the Bluetooth devices in the
(01CT0716) wireless range.
Practical No: 08 Date: 24-10-2024 Enrollment No: 92100133056
}
[Link]()
}
}
private fun pairDevice(device: BluetoothDevice) {
if (!checkBluetoothConnectPermission()) {
requestBluetoothPermissions()
return
}
try {
[Link]()
[Link](this, "Pairing with ${getDeviceName(device)}", Toast.LENGTH_SHORT).show()
} catch (e: Exception) {
[Link](this, "Failed to pair with ${getDeviceName(device)}", Toast.LENGTH_SHORT).show()
}
}
private fun checkBluetoothPermissions(): Boolean {
return if ([Link].SDK_INT >= Build.VERSION_CODES.S) {
checkBluetoothScanPermission() && checkBluetoothConnectPermission() && checkLocationPermission()
} else {
checkLocationPermission()
}
}
private fun checkBluetoothScanPermission(): Boolean {
return if ([Link].SDK_INT >= Build.VERSION_CODES.S) {
[Link](
this,
[Link].BLUETOOTH_SCAN
) == PackageManager.PERMISSION_GRANTED
} else {
true
}
}
private fun checkBluetoothConnectPermission(): Boolean {
return if ([Link].SDK_INT >= Build.VERSION_CODES.S) {
34
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: MPC Aim: Develop an application that identifies the Bluetooth devices in the
(01CT0716) wireless range.
Practical No: 08 Date: 24-10-2024 Enrollment No: 92100133056
[Link](
this,
[Link].BLUETOOTH_CONNECT
) == PackageManager.PERMISSION_GRANTED
} else {
true
}
}
private fun checkLocationPermission(): Boolean {
return [Link](
this,
[Link].ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
}
private fun requestBluetoothPermissions() {
val permissions = if ([Link].SDK_INT >= Build.VERSION_CODES.S) {
arrayOf(
[Link].BLUETOOTH_SCAN,
[Link].BLUETOOTH_CONNECT,
[Link].ACCESS_FINE_LOCATION
)
} else {
arrayOf(
[Link].ACCESS_FINE_LOCATION
)
}
[Link](permissions)
}
override fun onDestroy() {
[Link]()
unregisterReceiver(receiver)
if (checkBluetoothScanPermission() && [Link]) {
[Link]()
}
}
}
35
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: MPC Aim: Develop an application that identifies the Bluetooth devices in the
(01CT0716) wireless range.
Practical No: 08 Date: 24-10-2024 Enrollment No: 92100133056
Activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="24dp"
android:background="#121212"> <!-- Dark background color -->
<Button
android:id="@+id/buttonSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search for Devices"
android:padding="16dp"
android:background="@drawable/button_background"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:layout_gravity="center"
android:elevation="4dp"
android:layout_marginBottom="24dp"/>
<ListView
android:id="@+id/listViewDevices"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="16dp"
android:dividerHeight="1dp"
android:divider="@android:color/white"
android:padding="8dp"
android:background="#1E1E1E"
android:elevation="4dp"
android:layout_margin="8dp"
android:textColor="#FFFFFF" />
</LinearLayout>
36
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: MPC Aim: Develop an application that identifies the Bluetooth devices in the
(01CT0716) wireless range.
Practical No: 08 Date: 24-10-2024 Enrollment No: 92100133056
OUTPUT :
37