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
@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, |
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
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") |
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
@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) |
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
private val callback = object: BluetoothGattCallback() { | |
... | |
override fun onCharacteristicChanged( | |
gatt: BluetoothGatt?, | |
characteristic: BluetoothGattCharacteristic | |
) { | |
super.onCharacteristicChanged(gatt, characteristic) | |
Log.v("bluetooth", characteristic.value.contentToString()) | |
} |
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
private val callback = object: BluetoothGattCallback() { | |
... | |
override fun onCharacteristicWrite( | |
gatt: BluetoothGatt, | |
characteristic: BluetoothGattCharacteristic, | |
status: Int | |
) { | |
super.onCharacteristicWrite(gatt, characteristic, status) |
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
private val callback = object: BluetoothGattCallback() { | |
... | |
@Deprecated("Deprecated in Java") | |
override fun onCharacteristicRead( | |
gatt: BluetoothGatt, | |
characteristic: BluetoothGattCharacteristic, | |
status: Int | |
) { |
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
// 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() { |
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 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
fun haveAllPermissions(context: Context) = | |
ALL_BLE_PERMISSIONS | |
.all { context.checkSelfPermission(it) == PackageManager.PERMISSION_GRANTED } |
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
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, |