Skip to content

Commit

Permalink
Update NRF libraries 2.2.4 (#9)
Browse files Browse the repository at this point in the history
*  NRF BLE 2.2.4

* BRF Scanner 1.5.0
  • Loading branch information
lenguyenthanh committed Jun 22, 2021
1 parent 53e66b6 commit 652df2d
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 7 deletions.
4 changes: 4 additions & 0 deletions beckon/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ android {
}
}
}
compileOptions {
targetCompatibility '1.8'
sourceCompatibility '1.8'
}
kotlinOptions {
jvmTarget = "1.8"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import arrow.core.Option
import arrow.core.filterOption
import arrow.core.left
import arrow.core.right
import com.lenguyenthanh.rxarrow.SingleZ
import com.technocreatives.beckon.BleConnectionState
import com.technocreatives.beckon.BondState
import com.technocreatives.beckon.Change
Expand Down Expand Up @@ -49,7 +50,7 @@ internal class BeckonBleManager(
context: Context,
val device: BluetoothDevice,
val descriptor: Descriptor
) : BleManager<BeckonManagerCallbacks>(context) {
) : BleManager(context) {

private var bluetoothGatt: BluetoothGatt? = null

Expand All @@ -75,7 +76,10 @@ internal class BeckonBleManager(
stateSubject.onNext(newState)
}

mCallbacks = BeckonManagerCallbacks(bondSubject, onStateChange)
// TODO
// mCallbacks = BeckonManagerCallbacks(bondSubject, onStateChange)
setConnectionObserver(BeckonConnectionObserver(onStateChange))
setBondingObserver(BeckonBondingObserver(bondSubject))
// TODO Fix disposable
val statesDisposable = states.subscribe { Timber.d("New states of $device $it") }
}
Expand All @@ -95,7 +99,10 @@ internal class BeckonBleManager(
return devicesSubject.hide()
}

fun connect(retryAttempts: Int = 3, retryDelay: Int = 100): Single<Either<ConnectionError, DeviceDetail>> {
fun connect(
retryAttempts: Int = 3,
retryDelay: Int = 100
): SingleZ<ConnectionError, DeviceDetail> {
val request = connect(device)
.retry(retryAttempts, retryDelay)
.useAutoConnect(true)
Expand Down Expand Up @@ -242,7 +249,7 @@ internal class BeckonBleManager(
fun doCreateBond(): Completable {
bondSubject.onNext(BondState.CreatingBond)
return Completable.create { emitter ->
createBond()
ensureBond()
.done { emitter.onComplete() }
.fail { device, status ->
emitter.onError(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.technocreatives.beckon.internal

import android.bluetooth.BluetoothDevice
import com.technocreatives.beckon.BondState
import com.technocreatives.beckon.util.debugInfo
import io.reactivex.subjects.BehaviorSubject
import no.nordicsemi.android.ble.observer.BondingObserver
import timber.log.Timber

internal class BeckonBondingObserver(
private val bondStateSubject: BehaviorSubject<BondState>,
) : BondingObserver {
override fun onBondingRequired(device: BluetoothDevice) {
// todo do something
Timber.i("onBondingRequired ${device.debugInfo()}")
}

override fun onBonded(device: BluetoothDevice) {
Timber.i("onBonded ${device.debugInfo()}")
bondStateSubject.onNext(BondState.Bonded)
}

override fun onBondingFailed(device: BluetoothDevice) {
Timber.i("onBondingFailed ${device.debugInfo()}")
bondStateSubject.onNext(BondState.NotBonded)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import arrow.core.Some
import arrow.core.flatMap
import arrow.core.left
import arrow.core.right
import com.lenguyenthanh.rxarrow.SingleZ
import com.lenguyenthanh.rxarrow.fix
import com.technocreatives.beckon.BeckonClient
import com.technocreatives.beckon.BeckonDevice
Expand Down Expand Up @@ -176,7 +177,7 @@ internal class BeckonClientImpl(
private fun connect(
device: BluetoothDevice,
descriptor: Descriptor
): Single<Either<ConnectionError, BeckonDevice>> {
): SingleZ<ConnectionError, BeckonDevice> {
Timber.d("Connect BluetoothDevice: $device")

val manager = BeckonBleManager(context, device, descriptor)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.technocreatives.beckon.internal

import android.bluetooth.BluetoothDevice
import com.technocreatives.beckon.BleConnectionState
import com.technocreatives.beckon.util.debugInfo
import no.nordicsemi.android.ble.observer.ConnectionObserver
import timber.log.Timber

internal class BeckonConnectionObserver(
private val stateCallback: (BleConnectionState) -> Unit
) : ConnectionObserver {
override fun onDeviceConnecting(device: BluetoothDevice) {
Timber.i("onDeviceConnecting ${device.debugInfo()}")
stateCallback(BleConnectionState.Connecting)
}

override fun onDeviceConnected(device: BluetoothDevice) {
Timber.i("onDeviceConnected $device, debugInfo: ${device.debugInfo()}")
stateCallback(BleConnectionState.Connected)
}

override fun onDeviceFailedToConnect(device: BluetoothDevice, reason: Int) {
Timber.w("onError Device: ${device.debugInfo()} Reason: $reason")
stateCallback(BleConnectionState.Failed("NO Message", reason))
}

override fun onDeviceReady(device: BluetoothDevice) {
Timber.i("onDeviceReady ${device.debugInfo()}")
stateCallback(BleConnectionState.Ready)
}

override fun onDeviceDisconnecting(device: BluetoothDevice) {
Timber.i("onDeviceDisconnecting $device")
stateCallback(BleConnectionState.Disconnecting)
}

override fun onDeviceDisconnected(device: BluetoothDevice, reason: Int) {
Timber.i("onDeviceDisconnected $device")
stateCallback(BleConnectionState.Disconnected)
}
}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ buildscript {
ext.timber = '4.7.1'
ext.sharedPreferences = '1.1.0'
ext.rxBindingVersion = '3.1.0'
ext.nsScannerVersion = '1.4.3'
ext.nsBleVersion = '2.1.1'
ext.nsScannerVersion = '1.5.0'
ext.nsBleVersion = '2.2.4'
ext.nsDfuVersion = '1.9.0'
ext.moshi = '1.12.0'

Expand Down

0 comments on commit 652df2d

Please sign in to comment.