Mad PR 24
Mad PR 24
24
Code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout <Button
android:id="@+id/button2"
xmlns:android="http://schemas.android.co
m/apk/res/android" android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:text="List Devices"
android:padding="16dp"> android:onClick="list" />
</LinearLayout>
<TextView
android:id="@+id/textView" <LinearLayout
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Bluetooth example" android:orientation="horizontal"
android:textSize="35sp" android:gravity="center_horizontal"
android:layout_marginTop="10dp">
android:layout_gravity="center_horizontal
" /> <Button
android:id="@+id/button3"
<LinearLayout
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="wrap_content" android:text="Turn Off"
android:orientation="horizontal" android:onClick="off"
android:gravity="center_horizontal" android:layout_marginEnd="10dp"
android:layout_marginTop="20dp"> />
<Button <Button
android:id="@+id/button" android:id="@+id/button4"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Turn On" android:text="Get Visible"
android:onClick="on" android:onClick="visible" />
android:layout_marginEnd="10dp" </LinearLayout>
/>
<TextView public class MainActivity extends
android:id="@+id/textView2" AppCompatActivity {
Button b1, b2, b3, b4;
android:layout_width="wrap_content" private BluetoothAdapter BA;
private Set<BluetoothDevice>
android:layout_height="wrap_content" pairedDevices;
android:text="Paired Device" ListView lv;
android:textColor="#ff34ff06" private static final int
android:textSize="25sp" REQUEST_BLUETOOTH_PERMISSIO
android:layout_marginTop="20dp" /> NS = 1;
<ListView @Override
android:id="@+id/listView" protected void onCreate(Bundle
savedInstanceState) {
android:layout_width="match_parent" super.onCreate(savedInstanceState);
android:layout_height="wrap_content" setContentView(R.layout.activity_main);
android:layout_marginTop="10dp" />
</LinearLayout>
getSupportActionBar().setTitle("Bluetooth
Main example");
package com.example.pr24;
b1 = findViewById(R.id.button);
import b2 = findViewById(R.id.button2);
androidx.appcompat.app.AppCompatActiv b3 = findViewById(R.id.button3);
ity; b4 = findViewById(R.id.button4);
import androidx.core.app.ActivityCompat; BA =
import BluetoothAdapter.getDefaultAdapter();
android.bluetooth.BluetoothAdapter; lv = findViewById(R.id.listView);
import
android.bluetooth.BluetoothDevice; requestBluetoothPermissions();
import android.content.Intent; }
import
android.content.pm.PackageManager; private void
import android.os.Bundle; requestBluetoothPermissions() {
import android.view.View; if
import android.widget.ArrayAdapter; (ActivityCompat.checkSelfPermission(this
import android.widget.Button; ,
import android.widget.ListView; android.Manifest.permission.BLUETOOT
import android.widget.Toast; H_CONNECT) !=
import java.util.ArrayList; PackageManager.PERMISSION_GRANT
import java.util.Set; ED ||
ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.BLUETOOT }
H_ADVERTISE) != }
PackageManager.PERMISSION_GRANT
ED) { public void off(View v) {
requestBluetoothPermissions();
ActivityCompat.requestPermissions(this, if
new String[]{ (ActivityCompat.checkSelfPermission(this
,
android.Manifest.permission.BLUETOOT android.Manifest.permission.BLUETOOT
H_CONNECT, H_CONNECT) !=
PackageManager.PERMISSION_GRANT
android.Manifest.permission.BLUETOOT ED) {
H_ADVERTISE return;
}, }
REQUEST_BLUETOOTH_PERMISSIO BA.disable();
NS);
} Toast.makeText(getApplicationContext(),
} "Turned Off",
Toast.LENGTH_LONG).show();
public void on(View v) { }
if (!BA.isEnabled()) {
Intent turnOn = new public void visible(View v) {
Intent(BluetoothAdapter.ACTION_REQU Intent getVisible = new
EST_ENABLE); Intent(BluetoothAdapter.ACTION_REQU
requestBluetoothPermissions(); EST_DISCOVERABLE);
if requestBluetoothPermissions();
(ActivityCompat.checkSelfPermission(this if
, (ActivityCompat.checkSelfPermission(this
android.Manifest.permission.BLUETOOT ,
H_CONNECT) != android.Manifest.permission.BLUETOOT
PackageManager.PERMISSION_GRANT H_ADVERTISE) !=
ED) { PackageManager.PERMISSION_GRANT
return; ED) {
} return;
startActivityForResult(turnOn, 0); }
startActivityForResult(getVisible, 0);
Toast.makeText(getApplicationContext(), }
"Turned On",
Toast.LENGTH_LONG).show(); public void list(View v) {
} else { requestBluetoothPermissions();
if
Toast.makeText(getApplicationContext(), (ActivityCompat.checkSelfPermission(this
"Already On", ,
Toast.LENGTH_LONG).show(); android.Manifest.permission.BLUETOOT
H_CONNECT) != list.add(bt.getName());
PackageManager.PERMISSION_GRANT }
ED) {
return; Toast.makeText(getApplicationContext(),
} "Show Paired Devices",
pairedDevices = Toast.LENGTH_LONG).show();
BA.getBondedDevices(); ArrayAdapter<String> adapter = new
ArrayList<String> list = new ArrayAdapter<>(this,
ArrayList<>(); android.R.layout.simple_list_item_1, list);
for (BluetoothDevice bt : lv.setAdapter(adapter);
pairedDevices) { }
}
Output: