0% found this document useful (0 votes)
6 views7 pages

Exp 1 - MC

The document outlines an experiment for a mobile computing lab focused on implementing Bluetooth functionality in an Android application to transfer files between devices. It includes objectives, outcomes, deliverables, and relevant code snippets for setting up Bluetooth features such as enabling, discovering devices, and listing paired devices. The conclusion states that the app successfully connects to a Bluetooth network and facilitates file transfer.

Uploaded by

amishav2004
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)
6 views7 pages

Exp 1 - MC

The document outlines an experiment for a mobile computing lab focused on implementing Bluetooth functionality in an Android application to transfer files between devices. It includes objectives, outcomes, deliverables, and relevant code snippets for setting up Bluetooth features such as enabling, discovering devices, and listing paired devices. The conclusion states that the app successfully connects to a Bluetooth network and facilitates file transfer.

Uploaded by

amishav2004
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/ 7

CSL603 Mobile Computing Lab Sem VI

EXPERIMENT 1
Name Amisha Verma
Roll No 66

Title Implementation a Bluetooth network with application as transfer of a file from one
device to another

Pre requisite Android Programming

Mapping with CO CSL603.1

Objective To implement Blutooth.

Outcome Students learned to implement bluetooth to transfer files

Deliverables • Android programming


• Bluetooth implementation.

Colab Link @RequiresApi(Build.VERSION_CODES.S)


fun(_: View?) {
if (ActivityCompat.checkSelfPermission(
this,
BLUETOOTH
) != PackageManager.PERMISSION_GRANTED
){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
permit.launch(arrayOf( BLUETOOTH))
}
return
}
if (mBlueAdapter != null) {
if (!mBlueAdapter.isEnabled) {
//intent to on bluetooth
showToast("Turning On Bluetooth...")
} else {
showToast("Bluetooth is already on")
}
}
})
//discover bluetooth btn click
mDiscoverBtn.setOnClickListener(
@RequiresApi(Build.VERSION_CODES.S)
fun(_: View?) {
if (ActivityCompat.checkSelfPermission(
this,
BLUETOOTH_SCAN
) != PackageManager.PERMISSION_GRANTED
){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
permit.launch(arrayOf( BLUETOOTH_SCAN))
}
return
}
if (mBlueAdapter != null) {
if (!mBlueAdapter.isDiscovering) {
showToast("Making Your Device Discoverable")
val intent =
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)
startActivityForResult(
intent,
REQUEST_DISCOVER_BT
)
}
}
})
//off btn click
mOffBtn.setOnClickListener(
fun(_: View?) {
if (mBlueAdapter != null) {
if (mBlueAdapter.isEnabled) {
mBlueAdapter.disable()
showToast("Turning Bluetooth Off")
mBlueIv.setImageResource(R.drawable.ic_action_off)
} else {
showToast("Bluetooth is already off")
}
}
})
//get paired devices btn click
mPairedBtn.setOnClickListener(
fun(_: View?) {
if (mBlueAdapter != null) {
if (mBlueAdapter.isEnabled) {
mPairedTv.text = "Paired Devices"
val devices: Set<BluetoothDevice> =
mBlueAdapter.bondedDevices
for (device in devices) {
mPairedTv.append(
"""
Device: ${device.name}, $device
""".trimIndent()
)
}
} else {
//bluetooth is off so can't get paired devices
showToast("Turn on bluetooth to get paired devices")
}
}
})
}
//toast message function
private fun showToast(msg: String) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
}
companion object {
private const val REQUEST_DISCOVER_BT = 1
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/statusBluetoothTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textAlignment="center"
android:textSize="20sp" />
<!--Bluetooth icon (on/off)-->
<ImageView
android:id="@+id/bluetoothIv"
android:layout_width="100dp"
android:layout_height="100dp" />
<!--On Button-->
<Button
android:id="@+id/onBtn"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="Turn On" />
<!--Off btn-->
<Button
android:id="@+id/offBtn"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="Turn Off" />
<!--Discoverable button-->
<Button
android:id="@+id/discoverableBtn"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="Discoverable" />
<!--Get list of paired devices button-->
<Button
android:id="@+id/pairedBtn"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="Get Paired Devices" />
<!--Show paired devices here-->
<TextView
android:id="@+id/pairedTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.mybluetoothapplication"
tools:ignore="CoarseFineLocation">
<!-- Request legacy Bluetooth permissions on older devices. -->
<uses-permission android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
<!-- Needed only if your app looks for Bluetooth devices.
If your app doesn't use Bluetooth scan results to derive physical
location information, you can strongly assert that your app
doesn't derive physical location. -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"
/>
<!-- Needed only if your app makes the device discoverable to Bluetooth
devices. -->
<uses-permission
android:name="android.permission.BLUETOOTH_ADVERTISE" />
<!-- Needed only if your app communicates with already-paired Bluetooth
devices. -->
<uses-permission
android:name="android.permission.BLUETOOTH_CONNECT" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyBluetoothApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
build.gradle
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
android {
compileSdk 32
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.mybluetoothapplication"
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-
optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
Conclusion Successfully implemented an app that can connect to a bluetooth network
and show the paired devices. As a result we can transfer a file from one
device to another
References https://www.scribd.com/document/642175639/Untitled

You might also like