@@ -0,0 +1,51 @@
|
||||
package be.mygod.vpnhotspot.manage
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.bluetooth.BluetoothAdapter
|
||||
import android.bluetooth.BluetoothProfile
|
||||
import android.content.Context
|
||||
import be.mygod.vpnhotspot.widget.SmartSnackbar
|
||||
import timber.log.Timber
|
||||
|
||||
class BluetoothTethering(context: Context) : BluetoothProfile.ServiceListener, AutoCloseable {
|
||||
companion object {
|
||||
/**
|
||||
* PAN Profile
|
||||
* From BluetoothProfile.java.
|
||||
*/
|
||||
private const val PAN = 5
|
||||
private val isTetheringOn by lazy @SuppressLint("PrivateApi") {
|
||||
Class.forName("android.bluetooth.BluetoothPan").getDeclaredMethod("isTetheringOn")
|
||||
}
|
||||
}
|
||||
|
||||
private val adapter = BluetoothAdapter.getDefaultAdapter()
|
||||
private var pan: BluetoothProfile? = null
|
||||
/**
|
||||
* Based on: https://android.googlesource.com/platform/packages/apps/Settings/+/78d5efd/src/com/android/settings/TetherSettings.java
|
||||
*/
|
||||
val active: Boolean get() {
|
||||
val pan = pan
|
||||
return adapter?.state == BluetoothAdapter.STATE_ON && pan != null && isTetheringOn.invoke(pan) as Boolean
|
||||
}
|
||||
|
||||
init {
|
||||
try {
|
||||
adapter?.getProfileProxy(context, this, PAN)
|
||||
} catch (e: SecurityException) {
|
||||
Timber.w(e)
|
||||
SmartSnackbar.make(e).show()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onServiceDisconnected(profile: Int) {
|
||||
pan = null
|
||||
}
|
||||
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
|
||||
pan = proxy
|
||||
}
|
||||
override fun close() {
|
||||
adapter?.closeProfileProxy(PAN, pan)
|
||||
pan = null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user