Skip to content

Commit

Permalink
Merge pull request #78 from NordicSemiconductor/migration/ble-manager
Browse files Browse the repository at this point in the history
Migration to BleManager 2.6
  • Loading branch information
philips77 authored Jan 11, 2023
2 parents 8352e92 + c6e5f36 commit 1e3130b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ private class BlinkyManagerImpl(
_ledState.value = state
}

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

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

private inner class BlinkyManagerGattCallback: BleManagerGattCallback() {

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

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

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

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

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

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

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

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

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

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

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

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

0 comments on commit 1e3130b

Please sign in to comment.