From 5d39ace3f04918ded826abd8c3f4452a23021cc5 Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 10 Jun 2021 14:26:56 +0800 Subject: [PATCH] Use recommended API for getting bluetooth adapter --- .../vpnhotspot/manage/BluetoothTethering.kt | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt index e76c04dc..3e49495c 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt @@ -2,6 +2,7 @@ package be.mygod.vpnhotspot.manage import android.annotation.TargetApi import android.bluetooth.BluetoothAdapter +import android.bluetooth.BluetoothManager import android.bluetooth.BluetoothProfile import android.content.BroadcastReceiver import android.content.Context @@ -9,6 +10,7 @@ import android.content.Intent import android.content.IntentFilter import android.os.Build import androidx.annotation.RequiresApi +import androidx.core.content.getSystemService import androidx.core.os.BuildCompat import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.net.TetheringManager @@ -38,12 +40,12 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) : } private val isTetheringOn by lazy { clazz.getDeclaredMethod("isTetheringOn") } - fun pan(context: Context, serviceListener: BluetoothProfile.ServiceListener, adapter: BluetoothAdapter) = - (if (BuildCompat.isAtLeastS()) { - constructor.newInstance(context, serviceListener, adapter) - } else constructor.newInstance(context, serviceListener)) as BluetoothProfile + private val adapter = app.getSystemService()?.adapter + fun pan(context: Context, serviceListener: BluetoothProfile.ServiceListener) = (if (BuildCompat.isAtLeastS()) { + constructor.newInstance(context, serviceListener, adapter) + } else constructor.newInstance(context, serviceListener)) as BluetoothProfile val BluetoothProfile.isTetheringOn get() = isTetheringOn(this) as Boolean - fun BluetoothProfile.closePan() = BluetoothAdapter.getDefaultAdapter()!!.closeProfileProxy(PAN, this) + fun BluetoothProfile.closePan() = adapter!!.closeProfileProxy(PAN, this) private fun registerBluetoothStateListener(receiver: BroadcastReceiver) = app.registerReceiver(receiver, IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)) @@ -72,7 +74,6 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) : @RequiresApi(24) fun start(callback: TetheringManager.StartTetheringCallback) { if (pendingCallback != null) return - val adapter = BluetoothAdapter.getDefaultAdapter() try { if (adapter?.state == BluetoothAdapter.STATE_OFF) { registerBluetoothStateListener(this) @@ -95,7 +96,7 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) : val pan = pan ?: return null if (!connected) return null activeFailureCause = null - return BluetoothAdapter.getDefaultAdapter()?.state == BluetoothAdapter.STATE_ON && try { + return adapter?.state == BluetoothAdapter.STATE_ON && try { pan.isTetheringOn } catch (e: InvocationTargetException) { activeFailureCause = e.cause ?: e @@ -109,7 +110,7 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) : fun ensureInit(context: Context) { if (pan == null) try { - pan = pan(context, this, BluetoothAdapter.getDefaultAdapter() ?: return) + pan = pan(context, this) } catch (e: ReflectiveOperationException) { if (e.cause is SecurityException && BuildCompat.isAtLeastS()) Timber.d(e.readableMessage) else Timber.w(e)