Last active
January 5, 2024 09:02
-
-
Save tdcolvin/0529f8f7b5e8507784dcbc1f06bfd4ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Our connection to the selected device | |
private var gatt: BluetoothGatt? = null | |
//Whatever we do with our Bluetooth device connection, whether now or later, we will get the | |
//results in this callback object, which can become massive. | |
private val callback = object: BluetoothGattCallback() { | |
//We will override more methods here as we add functionality. | |
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) { | |
super.onConnectionStateChange(gatt, status, newState) | |
//This tells us when we're connected or disconnected from the peripheral. | |
if (status != BluetoothGatt.GATT_SUCCESS) { | |
//TODO: handle error | |
return | |
} | |
if (newState == BluetoothGatt.STATE_CONNECTED) { | |
//TODO: handle the fact that we've just connected | |
} | |
} | |
} | |
@RequiresPermission(PERMISSION_BLUETOOTH_CONNECT) | |
fun connect() { | |
gatt = bluetoothDevice.connectGatt(context, false, callback) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment