Skip to content

Instantly share code, notes, and snippets.

View tdcolvin's full-sized avatar

Tom Colvin tdcolvin

  • Basingstoke, UK
  • 17:08 (UTC +01:00)
View GitHub Profile
@Composable
fun PlanetsScreen(...) {
val revealPIN by viewModel.isShowingPin.collectAsStateWithLifecycle()
val scope = rememberCoroutineScope()
Column {
Button(
onClick = {
scope.launch {
// Here we call a function which takes at least 10 seconds to run,
private val callback = object: BluetoothGattCallback() {
override fun onDescriptorWrite(
gatt: BluetoothGatt?,
descriptor: BluetoothGattDescriptor,
status: Int
) {
super.onDescriptorWrite(gatt, descriptor, status)
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.v("bluetooth", "Descriptor ${descriptor.uuid} of characteristic ${descriptor.characteristic.uuid}: write success")
@RequiresPermission(PERMISSION_BLUETOOTH_CONNECT)
fun stopReceivingPasswordUpdates() {
val service = gatt?.getService(CTF_SERVICE_UUID)
val characteristic = service?.getCharacteristic(PASSWORD_CHARACTERISTIC_UUID)
if (characteristic != null) {
val CLIENT_CONFIG_DESCRIPTOR = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")
val desc = characteristic.getDescriptor(CLIENT_CONFIG_DESCRIPTOR)
desc?.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE)
gatt?.writeDescriptor(desc)
private val callback = object: BluetoothGattCallback() {
...
override fun onCharacteristicChanged(
gatt: BluetoothGatt?,
characteristic: BluetoothGattCharacteristic
) {
super.onCharacteristicChanged(gatt, characteristic)
Log.v("bluetooth", characteristic.value.contentToString())
}
private val callback = object: BluetoothGattCallback() {
...
override fun onCharacteristicWrite(
gatt: BluetoothGatt,
characteristic: BluetoothGattCharacteristic,
status: Int
) {
super.onCharacteristicWrite(gatt, characteristic, status)
private val callback = object: BluetoothGattCallback() {
...
@Deprecated("Deprecated in Java")
override fun onCharacteristicRead(
gatt: BluetoothGatt,
characteristic: BluetoothGattCharacteristic,
status: Int
) {
// The saved list of services. The BluetoothGattService objects here can be used
// to list the characteristics in each service.
private var services: List<BluetoothGattService> = emptyList()
@RequiresPermission(PERMISSION_BLUETOOTH_CONNECT)
fun discoverServices() {
gatt?.discoverServices()
}
private val callback = object: BluetoothGattCallback() {
//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)
fun haveAllPermissions(context: Context) =
ALL_BLE_PERMISSIONS
.all { context.checkSelfPermission(it) == PackageManager.PERMISSION_GRANTED }
val ALL_BLE_PERMISSIONS = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
arrayOf(
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_SCAN
)
}
else {
arrayOf(
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH,