Pra-24 Bluetooth Connectivity
Pra-24 Bluetooth Connectivity
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 androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Set;
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: