Skip to content

Instantly share code, notes, and snippets.

@kirmartuk
Created December 27, 2020 11:21
Show Gist options
  • Save kirmartuk/0fc268f5b25b1b9f43f7ae7b30bc5748 to your computer and use it in GitHub Desktop.
Save kirmartuk/0fc268f5b25b1b9f43f7ae7b30bc5748 to your computer and use it in GitHub Desktop.
Bluetooth Repository for Android (Kotlin)
/**
* Simple Bluetooth repository for android on kotlin
*
* @author Kirill Martyuk
* @version 1.0
*/
class BluetoothRepository(private val context: Context) {
/**
* Get current connected bluetooth devices
* @param liveData with ArrayList of BluetoothDevice
* @return update lifeData after receiving data from ServiceListener
*/
fun getConnectedBluetoothDevices(liveData: MutableLiveData<ArrayList<BluetoothDevice>>) {
var list: ArrayList<BluetoothDevice>
val serviceListener: BluetoothProfile.ServiceListener = object :
BluetoothProfile.ServiceListener {
override fun onServiceDisconnected(profile: Int) {
TODO("Not yet implemented")
}
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile?) {
list = proxy?.connectedDevices as ArrayList<BluetoothDevice>
liveData.postValue(list)
Log.e("connected", list.toString())
BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy)
}
}
BluetoothAdapter.getDefaultAdapter()
.getProfileProxy(
context,
serviceListener, BluetoothProfile.STATE_CONNECTED
)
}
/**
* Connect to bluetooth device where BluetoothProfile == HEADSET && A2DP
* @param device - BluetoothDevice
* @return void
*/
fun connect(device: BluetoothDevice) {
val serviceListener: BluetoothProfile.ServiceListener = object :
BluetoothProfile.ServiceListener {
override fun onServiceDisconnected(profile: Int) {}
@SuppressLint("DiscouragedPrivateApi")
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
val connect = proxy.javaClass.getDeclaredMethod(
"connect",
BluetoothDevice::class.java
)
connect.isAccessible = true
connect.invoke(proxy, device)
BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy)
}
}
BluetoothAdapter.getDefaultAdapter().apply {
getProfileProxy(context, serviceListener, BluetoothProfile.HEADSET)
getProfileProxy(context, serviceListener, BluetoothProfile.A2DP)
}
}
/**
* Disconnect connected bluetooth device where BluetoothProfile == HEADSET && A2DP
* @param device - BluetoothDevice
* @return void
*/
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 disconnectMethod = proxy.javaClass.getDeclaredMethod(
"disconnect",
BluetoothDevice::class.java
)
disconnectMethod.isAccessible = true
disconnectMethod.invoke(proxy, device)
BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy)
}
}
BluetoothAdapter.getDefaultAdapter().apply {
getProfileProxy(context, serviceListener, BluetoothProfile.HEADSET)
getProfileProxy(context, serviceListener, BluetoothProfile.A2DP)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment