Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration to BleManager 2.6 #78

Merged
merged 1 commit into from Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -86,10 +86,6 @@ private class BlinkyManagerImpl(
_ledState.value = state _ledState.value = state
} }


override fun getGattCallback(): BleManagerGattCallback {
return BlinkyManagerGattCallback()
}

override fun log(priority: Int, message: String) { override fun log(priority: Int, message: String) {
Timber.log(priority, message) Timber.log(priority, message)
} }
Expand All @@ -100,76 +96,73 @@ private class BlinkyManagerImpl(
return Log.VERBOSE return Log.VERBOSE
} }


private inner class BlinkyManagerGattCallback: BleManagerGattCallback() { private val buttonCallback by lazy {

object : ButtonCallback() {
private val buttonCallback by lazy { override fun onButtonStateChanged(device: BluetoothDevice, state: Boolean) {
object : ButtonCallback() { _buttonState.tryEmit(state)
override fun onButtonStateChanged(device: BluetoothDevice, state: Boolean) {
_buttonState.tryEmit(state)
}
} }
} }
}


private val ledCallback by lazy { private val ledCallback by lazy {
object : LedCallback() { object : LedCallback() {
override fun onLedStateChanged(device: BluetoothDevice, state: Boolean) { override fun onLedStateChanged(device: BluetoothDevice, state: Boolean) {
_ledState.tryEmit(state) _ledState.tryEmit(state)
}
} }
} }
}


override fun isRequiredServiceSupported(gatt: BluetoothGatt): Boolean { override fun isRequiredServiceSupported(gatt: BluetoothGatt): Boolean {
// Get the LBS Service from the gatt object. // Get the LBS Service from the gatt object.
gatt.getService(BlinkySpec.BLINKY_SERVICE_UUID)?.apply { gatt.getService(BlinkySpec.BLINKY_SERVICE_UUID)?.apply {
// Get the LED characteristic. // Get the LED characteristic.
ledCharacteristic = getCharacteristic( ledCharacteristic = getCharacteristic(
BlinkySpec.BLINKY_LED_CHARACTERISTIC_UUID, BlinkySpec.BLINKY_LED_CHARACTERISTIC_UUID,
// Mind, that below we pass required properties. // Mind, that below we pass required properties.
// If your implementation supports only WRITE_NO_RESPONSE, // If your implementation supports only WRITE_NO_RESPONSE,
// change the property to BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE. // change the property to BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE.
BluetoothGattCharacteristic.PROPERTY_WRITE BluetoothGattCharacteristic.PROPERTY_WRITE
) )
// Get the Button characteristic. // Get the Button characteristic.
buttonCharacteristic = getCharacteristic( buttonCharacteristic = getCharacteristic(
BlinkySpec.BLINKY_BUTTON_CHARACTERISTIC_UUID, BlinkySpec.BLINKY_BUTTON_CHARACTERISTIC_UUID,
BluetoothGattCharacteristic.PROPERTY_NOTIFY BluetoothGattCharacteristic.PROPERTY_NOTIFY
) )


// Return true if all required characteristics are supported. // Return true if all required characteristics are supported.
return ledCharacteristic != null && buttonCharacteristic != null return ledCharacteristic != null && buttonCharacteristic != null
}
return false
} }
return false
}


@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
override fun initialize() { override fun initialize() {
// Enable notifications for the button characteristic. // Enable notifications for the button characteristic.
val flow: Flow<ButtonState> = setNotificationCallback(buttonCharacteristic) val flow: Flow<ButtonState> = setNotificationCallback(buttonCharacteristic)
.asValidResponseFlow() .asValidResponseFlow()


// Forward the button state to the buttonState flow. // Forward the button state to the buttonState flow.
scope.launch { scope.launch {
flow.map { it.state }.collect { _buttonState.tryEmit(it) } flow.map { it.state }.collect { _buttonState.tryEmit(it) }
} }


enableNotifications(buttonCharacteristic) enableNotifications(buttonCharacteristic)
.enqueue() .enqueue()


// Read the initial value of the button characteristic. // Read the initial value of the button characteristic.
readCharacteristic(buttonCharacteristic) readCharacteristic(buttonCharacteristic)
.with(buttonCallback) .with(buttonCallback)
.enqueue() .enqueue()


// Read the initial value of the LED characteristic. // Read the initial value of the LED characteristic.
readCharacteristic(ledCharacteristic) readCharacteristic(ledCharacteristic)
.with(ledCallback) .with(ledCallback)
.enqueue() .enqueue()
} }


override fun onServicesInvalidated() { override fun onServicesInvalidated() {
ledCharacteristic = null ledCharacteristic = null
buttonCharacteristic = null buttonCharacteristic = null
}
} }


} }
2 changes: 1 addition & 1 deletion settings.gradle.kts
Expand Up @@ -16,7 +16,7 @@ dependencyResolutionManagement {
} }
versionCatalogs { versionCatalogs {
create("libs") { create("libs") {
from("no.nordicsemi.android.gradle:version-catalog:1.1.3") from("no.nordicsemi.android.gradle:version-catalog:1.2.1")
} }
} }
} }
Expand Down