Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When we call the bluetooth BLE manager for connection was success but it is getting unpair and disconnected frequently on android phone.

What I have tried:

fun isDevicePaired(name: String) : Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            if (ActivityCompat.checkSelfPermission(
                    context,
                    Manifest.permission.BLUETOOTH_CONNECT
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                // TODO: Consider calling
                
                return false
            }
        }
        lateinit var mBluetoothAdapter: BluetoothAdapter

        try {
            mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
        } catch (e: Exception) {
            return false
        }

        var pairedDevices: Set<BluetoothDevice>? = mBluetoothAdapter.bondedDevices

        for (device in pairedDevices?.toList() ?: listOf()) {
            if (!device.name.isNullOrEmpty())
                if (device.name.contentEquals(name))
                    return true
        }

        return false
    }
Posted
Comments
[no name] 15-Feb-23 18:24pm    
Nothing happens "randomly". You need to find a pattern and a cause. Android only? (apparently, by your post). So, one size doesn't fit all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900