Basic Bluetooth example causing problems

37 Views Asked by At

I am currently trying to further my knowledge in using Bluetooth in Android studio, but for some reason I cannot seem to get the App I am working on to acknowlege or request permissions for Bluetooth. I cant help but think theres something I am missing, but from what I have seen in other forums, Bluetooth works out of the box.

What I would like to happen is when the app opens to add Bluetooth devices found into a listbox at the bottom of the screen

The App compiles ok, gives me a message to tell me Bluetooth is on or off but then when accessing Bluetooth feature just crashes out. For purposes of Brevity I have omitted the permissions in this code sample. I have refined the issue to

  val pairedDevices = bluetoothAdapter!!.bondedDevices

But cannot understand what I am doing wrong, can someone help please? Here is My Manifest

    <?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.adamvi">
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-feature android:name="android.hardware.bluetooth" android:required="true"/>

    <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.AdamVI"
        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>

Here is my MainActivity

import android.annotation.SuppressLint
import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ListView
import android.widget.Toast
import java.util.Arrays


class MainActivity : Activity() {

    @SuppressLint("MissingPermission")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val bluetoothManager: BluetoothManager = getSystemService(BluetoothManager::class.java)
        val bluetoothAdapter: BluetoothAdapter? = bluetoothManager.adapter

        if (bluetoothAdapter != null) {
            if (bluetoothAdapter != null && bluetoothAdapter.isEnabled) {
                Toast.makeText(
                    applicationContext,
                    "Bluetooth is switched on",
                    Toast.LENGTH_LONG
                ).show()

                val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
                val listView = ArrayList<String>()
                val pairedDevices = bluetoothAdapter!!.bondedDevices
                pairedDevices.forEach { device ->
                    listView.add(device.name.toString())
                }
            }
        } else {

            Toast.makeText(
                applicationContext,
                "Bluetooth is switched off",
                Toast.LENGTH_LONG
            ).show()


        }
    }
}
1

There are 1 best solutions below

0
Michael Kotzjan On BEST ANSWER

Some Bluetooth permissions are so called runtime permissions. You need to request access at runtime as well as declare the usage in your manifest. According to Android Developer Guide for targeting Android 12 and above:

The BLUETOOTH_ADVERTISE, BLUETOOTH_CONNECT, and BLUETOOTH_SCAN permissions are runtime permissions. Therefore, you must explicitly request user approval in your app before you can look for Bluetooth devices, make a device discoverable to other devices, or communicate with already-paired Bluetooth devices.

and for targeting Android 11 and below:

Because location permissions are runtime permissions, you must request these permissions at runtime along with declaring them in your manifest.