Android: Как отключить уже подключенное устройство bluetooth? - PullRequest
0 голосов
/ 12 июля 2020

У меня есть функция подключения, и она отлично работает

    fun connect(btDevice: BluetoothDevice?) {
        val id: UUID = btDevice?.uuids?.get(0)!!.uuid
        val bts = btDevice.createInsecureRfcommSocketToServiceRecord(id)
        bts?.connect()
    }

Но bts.close() не работает, если я назову эту

D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@518c676, channel: 2, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@d48a477, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@51478e4mSocket: null, mSocketState: CLOSED

1 Ответ

0 голосов
/ 26 июля 2020

Kotlin

private fun disconnect(device: BluetoothDevice) {
    val serviceListener: BluetoothProfile.ServiceListener = object :
        BluetoothProfile.ServiceListener {
        override fun onServiceDisconnected(profile: Int) {}

        @SuppressLint("DiscouragedPrivateApi")
        override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
            val disconnect = BluetoothA2dp::class.java.getDeclaredMethod(
                "disconnect",
                BluetoothDevice::class.java
            )
            disconnect.isAccessible = true
            disconnect.invoke(proxy, device)
            BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy)
        }
    }
    BluetoothAdapter.getDefaultAdapter()
        .getProfileProxy(this, serviceListener, BluetoothProfile.A2DP)
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...