@@ -10,6 +10,7 @@ import android.util.Log
1010import expo.modules.kotlin.Promise
1111import no.nordicsemi.android.ble.BleManager
1212import no.nordicsemi.android.ble.TimeoutableRequest
13+ import no.nordicsemi.android.ble.observer.ConnectionObserver
1314import java.util.Locale
1415import java.util.UUID
1516import kotlin.math.min
@@ -28,6 +29,41 @@ class RNBleManager(
2829 private var gatt: BluetoothGatt ? = null
2930 private var mtu: Int? = null
3031
32+ init {
33+ connectionObserver = object : ConnectionObserver {
34+ override fun onDeviceConnecting (device : BluetoothDevice ) {
35+ log(Log .INFO , " device connecting" )
36+ emitConnectionStateChange(ConnectionState .CONNECTING_TO_DEVICE , 0 )
37+ }
38+
39+ override fun onDeviceConnected (device : BluetoothDevice ) {
40+ emitConnectionStateChange(ConnectionState .DISCOVERING_SERVICES , 0 )
41+ }
42+
43+ override fun onDeviceFailedToConnect (
44+ device : BluetoothDevice ,
45+ reason : Int
46+ ) {
47+ emitConnectionStateChange(ConnectionState .DISCONNECTED , reason)
48+ }
49+
50+ override fun onDeviceReady (device : BluetoothDevice ) {
51+ emitConnectionStateChange(ConnectionState .CONNECTED , 0 )
52+ }
53+
54+ override fun onDeviceDisconnecting (device : BluetoothDevice ) {
55+ emitConnectionStateChange(ConnectionState .DISCONNECTING , 0 )
56+ }
57+
58+ override fun onDeviceDisconnected (
59+ device : BluetoothDevice ,
60+ reason : Int
61+ ) {
62+ emitConnectionStateChange(ConnectionState .DISCONNECTED , reason)
63+ }
64+ }
65+ }
66+
3167 override fun getMinLogPriority (): Int {
3268 return logLevel
3369 }
@@ -70,31 +106,21 @@ class RNBleManager(
70106 val mtu = mtu
71107 if (mtu != null ) {
72108 requestMtu(mtu)
73- .before {
74- emitConnectionStateChange(
75- ConnectionState . REQUESTING_MTU ,
76- BluetoothGatt . GATT_SUCCESS ,
77- )
109+ .fail { device, status ->
110+ log( Log . WARN , " Error requesting MTU: $status " )
111+ }
112+ .done {
113+ log( Log . INFO , " MTU correctly exchanged " )
78114 }
79115 .enqueue()
80116 }
81117 }
82118
83- override fun onDeviceReady () {
84- emitConnectionStateChange(ConnectionState .CONNECTED , 0 )
85- }
86-
87119 override fun onServicesInvalidated () {
88- emitConnectionStateChange(ConnectionState .DISCONNECTED , 0 )
89120 gatt = null
90121 }
91122
92123 override fun isRequiredServiceSupported (gatt : BluetoothGatt ): Boolean {
93- emitConnectionStateChange(
94- ConnectionState .DISCOVERING_SERVICES ,
95- BluetoothGatt .GATT_SUCCESS ,
96- )
97-
98124 this .gatt = gatt
99125
100126 return true
@@ -120,7 +146,6 @@ class RNBleManager(
120146 if (isConnected) {
121147 disconnect()
122148 .done { log(Log .INFO , " device disconnected" ) }
123- .before { emitConnectionStateChange(ConnectionState .DISCONNECTING , 0 ) }
124149 .fail { device, status ->
125150 Log .w(
126151 LOG_TAG ,
@@ -135,16 +160,12 @@ class RNBleManager(
135160 .retry(3 )
136161 .timeout(TIMEOUT_MS )
137162 .useAutoConnect(false )
138- .before {
139- emitConnectionStateChange(ConnectionState .CONNECTING_TO_DEVICE , BluetoothGatt .GATT_SUCCESS )
140- }
141163 .done {
142164 log(Log .INFO , " device connected" )
143165 promise.resolve()
144166 }
145167 .fail { device, status ->
146168 log(Log .WARN , " error connecting device: $status " )
147- emitConnectionStateChange(ConnectionState .DISCONNECTED , status)
148169 promise.reject(
149170 BleError .ERROR_NOT_CONNECTED .name,
150171 " error connecting to device status: $status " ,
@@ -158,10 +179,8 @@ class RNBleManager(
158179 if (isConnected) {
159180 disconnect()
160181 .timeout(5_000 )
161- .before { emitConnectionStateChange(ConnectionState .DISCONNECTING , 0 ) }
162182 .done {
163183 log(Log .INFO , " Device disconnected" )
164- emitConnectionStateChange(ConnectionState .DISCONNECTED , 0 )
165184 promise.resolve()
166185 }
167186 .fail { device, status ->
0 commit comments