0% found this document useful (0 votes)
13 views3 pages

Exp 24

The document contains an AndroidManifest.xml file and a MainActivity.java file for a Bluetooth application. The manifest includes permissions for Bluetooth and location access, while the MainActivity manages Bluetooth functionality such as turning it on/off and discovering devices. The layout features buttons for controlling Bluetooth and a ListView for displaying paired devices.

Uploaded by

sawantkrish562
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)
13 views3 pages

Exp 24

The document contains an AndroidManifest.xml file and a MainActivity.java file for a Bluetooth application. The manifest includes permissions for Bluetooth and location access, while the MainActivity manages Bluetooth functionality such as turning it on/off and discovering devices. The layout features buttons for controlling Bluetooth and a ListView for displaying paired devices.

Uploaded by

sawantkrish562
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/ 3

EXPl 24.

AndroidManifest.xml

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


<?xml version="1.0" encoding="utf-8"?>
<manifest
<LinearLayout
xmlns:android="http://schemas.android.com/ap
xmlns:android="http://schemas.android.com/ap
k/res/android"
k/res/android"
package="com.example.bluetoothpracs">
android:layout_width="match_parent"
<uses-permission
android:layout_height="match_parent"
android:name="android.permission.BLUETOOTH"
android:paddingLeft="10dp"
/>
android:orientation="vertical"
<uses-permission
android:paddingRight="10dp">
android:name="android.permission.BLUETOOTH_
<TextView
ADMIN"/>
android:layout_width="wrap_content"
<uses-permission
android:layout_height="wrap_content"
android:name="android.permission.ACCESS_COA
android:text="Bluetooth"
RSE_LOCATION"/>
android:textSize="25dp"
<application
android:textStyle="bold"
android:allowBackup="true"
/>
android:icon="@mipmap/ic_launcher"
<Button
android:label="@string/app_name"
android:id="@+id/btnOn"
android:layout_width="wrap_content"
android:roundIcon="@mipmap/ic_launcher_roun
android:layout_height="wrap_content"
d"
android:text="Turn On" />
android:supportsRtl="true"
<Button
android:theme="@style/AppTheme">
android:id="@+id/btnDiscoverable"
<activity android:name=".MainActivity">
android:layout_width="wrap_content"
<intent-filter>
android:layout_height="wrap_content"
<action
android:name="android.intent.action.MAIN" />
android:text="Get Visible" />
<category
<Button
android:name="android.intent.category.LAUNCH
android:layout_width="wrap_content"
ER" />
android:layout_height="wrap_content"
</intent-filter>
android:id="@+id/btnGet"
</activity>
android:text="List Devices"
</application>
/>
<Button
</manifest>
android:id="@+id/btnOFF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
MainActivity.java
android:text="Turn OFF" />
<ListView package com.example.bluetoothpracs;
android:id="@+id/deviceList"
android:layout_width="match_parent" import
android:layout_height="wrap_content" > androidx.appcompat.app.AppCompatActivity;
</ListView>
</LinearLayout> import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle; Toast.makeText(getApplicationContext(),"Bluetoot
import android.view.View; h Turned OFF", Toast.LENGTH_SHORT).show();
import android.widget.ArrayAdapter; }
import android.widget.Button; });
import android.widget.ListView; btnDisc.setOnClickListener(new
import android.widget.Toast; View.OnClickListener() {
@Override
import java.util.ArrayList; public void onClick(View v) {
import java.util.Set; if(!bAdapter.isDiscovering()){
startActivityForResult(new
public class MainActivity extends Intent(BluetoothAdapter.ACTION_REQUEST_DISC
AppCompatActivity { OVERABLE),1);
private ListView lstvw;
private ArrayAdapter aAdapter; Toast.makeText(getApplicationContext(),"Making
private BluetoothAdapter bAdapter = Device
BluetoothAdapter.getDefaultAdapter(); Discoverable",Toast.LENGTH_SHORT).show();
@Override }
protected void onCreate(Bundle }
savedInstanceState) { });
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btn =
Button btntOn = (Button)findViewById(R.id.btnGet);
(Button)findViewById(R.id.btnOn); btn.setOnClickListener(new
Button btntOff = View.OnClickListener() {
(Button)findViewById(R.id.btnOFF); @Override
Button btnDisc = public void onClick(View v) {
(Button)findViewById(R.id.btnDiscoverable); if(bAdapter==null){
final BluetoothAdapter bAdapter =
BluetoothAdapter.getDefaultAdapter(); Toast.makeText(getApplicationContext(),"Bluetoot
btntOn.setOnClickListener(new h Not Supported",Toast.LENGTH_SHORT).show();
View.OnClickListener() { }
@Override else{
public void onClick(View v) { Set<BluetoothDevice> pairedDevices =
if(bAdapter == null) bAdapter.getBondedDevices();
{ ArrayList list = new ArrayList();
if(pairedDevices.size()>0){
Toast.makeText(getApplicationContext(),"Bluetoot for(BluetoothDevice device:
h Not Supported",Toast.LENGTH_SHORT).show(); pairedDevices){
} String devicename =
else{ device.getName();
if(!bAdapter.isEnabled()){ String macAddress =
startActivityForResult(new device.getAddress();
Intent(BluetoothAdapter.ACTION_REQUEST_ENAB list.add("Name:
LE),1); "+devicename+"MAC Address: "+macAddress);
}
Toast.makeText(getApplicationContext(),"Bluetoot lstvw = (ListView)
h Turned ON",Toast.LENGTH_SHORT).show(); findViewById(R.id.deviceList);
} aAdapter = new
} ArrayAdapter(getApplicationContext(),
} android.R.layout.simple_list_item_1, list);
}); lstvw.setAdapter(aAdapter);
btntOff.setOnClickListener(new }
View.OnClickListener() { }
@Override }
public void onClick(View v) { });
bAdapter.disable(); }
}

You might also like