Skip to content

Instantly share code, notes, and snippets.

@tdcolvin
Last active January 5, 2024 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdcolvin/0529f8f7b5e8507784dcbc1f06bfd4ad to your computer and use it in GitHub Desktop.
Save tdcolvin/0529f8f7b5e8507784dcbc1f06bfd4ad to your computer and use it in GitHub Desktop.
//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