0% found this document useful (0 votes)
15 views5 pages

Pra-24 Bluetooth Connectivity

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

Pra-24 Bluetooth Connectivity

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

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"
tools:context=".MainActivity"
android:layout_margin="14dp"
android:orientation="vertical">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bluetooth"
android:textSize="20dp"
android:textStyle="bold"/>
<Button
android:id="@+id/on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn On" />

<Button
android:id="@+id/getvisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Visible" />

<Button
android:id="@+id/listdevice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List Devices" />

<Button
android:id="@+id/off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn OFF" />

<ListView
android:id="@+id/lv"
android:layout_width="279dp"
android:layout_height="284dp"/>

</LinearLayout>
MainActivity.java
package com.example.bluetoothpr24;

import static android.bluetooth.BluetoothAdapter.ACTION_REQUEST_ENABLE;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

Button on,off,getvisible,listdevice;
final BluetoothAdapter bAdapter = BluetoothAdapter.getDefaultAdapter();
private Set<BluetoothDevice> pairedDevices;
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
on = (Button) findViewById(R.id.on);
off = (Button) findViewById(R.id.off);
getvisible = (Button) findViewById(R.id.getvisible);
listdevice = (Button) findViewById(R.id.listdevice);
lv = (ListView) findViewById(R.id.lv);

on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bAdapter.enable();
}
});
off.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bAdapter.disable();
}
});
getvisible.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent getVisible = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
}
});
listdevice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pairedDevices = bAdapter.getBondedDevices();
ArrayList list = new ArrayList();
for(BluetoothDevice bt : pairedDevices)
list.add(bt.getName());
Toast.makeText(getApplicationContext(), "Showing Paired
Devices", Toast.LENGTH_SHORT).show();
ArrayAdapter adapter = new
ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
}
});
}
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetoothpr24">

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE"
/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

<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.BluetoothPr24">
<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>
Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy