@@ -86,10 +86,6 @@ private class BlinkyManagerImpl(
8686 _ledState .value = state
8787 }
8888
89- override fun getGattCallback (): BleManagerGattCallback {
90- return BlinkyManagerGattCallback ()
91- }
92-
9389 override fun log (priority : Int , message : String ) {
9490 Timber .log(priority, message)
9591 }
@@ -100,76 +96,73 @@ private class BlinkyManagerImpl(
10096 return Log .VERBOSE
10197 }
10298
103- private inner class BlinkyManagerGattCallback : BleManagerGattCallback () {
104-
105- private val buttonCallback by lazy {
106- object : ButtonCallback () {
107- override fun onButtonStateChanged (device : BluetoothDevice , state : Boolean ) {
108- _buttonState .tryEmit(state)
109- }
99+ private val buttonCallback by lazy {
100+ object : ButtonCallback () {
101+ override fun onButtonStateChanged (device : BluetoothDevice , state : Boolean ) {
102+ _buttonState .tryEmit(state)
110103 }
111104 }
105+ }
112106
113- private val ledCallback by lazy {
114- object : LedCallback () {
115- override fun onLedStateChanged (device : BluetoothDevice , state : Boolean ) {
116- _ledState .tryEmit(state)
117- }
107+ private val ledCallback by lazy {
108+ object : LedCallback () {
109+ override fun onLedStateChanged (device : BluetoothDevice , state : Boolean ) {
110+ _ledState .tryEmit(state)
118111 }
119112 }
113+ }
120114
121- override fun isRequiredServiceSupported (gatt : BluetoothGatt ): Boolean {
122- // Get the LBS Service from the gatt object.
123- gatt.getService(BlinkySpec .BLINKY_SERVICE_UUID )?.apply {
124- // Get the LED characteristic.
125- ledCharacteristic = getCharacteristic(
126- BlinkySpec .BLINKY_LED_CHARACTERISTIC_UUID ,
127- // Mind, that below we pass required properties.
128- // If your implementation supports only WRITE_NO_RESPONSE,
129- // change the property to BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE.
130- BluetoothGattCharacteristic .PROPERTY_WRITE
131- )
132- // Get the Button characteristic.
133- buttonCharacteristic = getCharacteristic(
134- BlinkySpec .BLINKY_BUTTON_CHARACTERISTIC_UUID ,
135- BluetoothGattCharacteristic .PROPERTY_NOTIFY
136- )
137-
138- // Return true if all required characteristics are supported.
139- return ledCharacteristic != null && buttonCharacteristic != null
140- }
141- return false
115+ override fun isRequiredServiceSupported (gatt : BluetoothGatt ): Boolean {
116+ // Get the LBS Service from the gatt object.
117+ gatt.getService(BlinkySpec .BLINKY_SERVICE_UUID )?.apply {
118+ // Get the LED characteristic.
119+ ledCharacteristic = getCharacteristic(
120+ BlinkySpec .BLINKY_LED_CHARACTERISTIC_UUID ,
121+ // Mind, that below we pass required properties.
122+ // If your implementation supports only WRITE_NO_RESPONSE,
123+ // change the property to BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE.
124+ BluetoothGattCharacteristic .PROPERTY_WRITE
125+ )
126+ // Get the Button characteristic.
127+ buttonCharacteristic = getCharacteristic(
128+ BlinkySpec .BLINKY_BUTTON_CHARACTERISTIC_UUID ,
129+ BluetoothGattCharacteristic .PROPERTY_NOTIFY
130+ )
131+
132+ // Return true if all required characteristics are supported.
133+ return ledCharacteristic != null && buttonCharacteristic != null
142134 }
135+ return false
136+ }
143137
144- @OptIn(ExperimentalCoroutinesApi ::class )
145- override fun initialize () {
146- // Enable notifications for the button characteristic.
147- val flow: Flow <ButtonState > = setNotificationCallback(buttonCharacteristic)
148- .asValidResponseFlow()
138+ @OptIn(ExperimentalCoroutinesApi ::class )
139+ override fun initialize () {
140+ // Enable notifications for the button characteristic.
141+ val flow: Flow <ButtonState > = setNotificationCallback(buttonCharacteristic)
142+ .asValidResponseFlow()
149143
150- // Forward the button state to the buttonState flow.
151- scope.launch {
152- flow.map { it.state }.collect { _buttonState .tryEmit(it) }
153- }
144+ // Forward the button state to the buttonState flow.
145+ scope.launch {
146+ flow.map { it.state }.collect { _buttonState .tryEmit(it) }
147+ }
154148
155- enableNotifications(buttonCharacteristic)
156- .enqueue()
149+ enableNotifications(buttonCharacteristic)
150+ .enqueue()
157151
158- // Read the initial value of the button characteristic.
159- readCharacteristic(buttonCharacteristic)
160- .with (buttonCallback)
161- .enqueue()
152+ // Read the initial value of the button characteristic.
153+ readCharacteristic(buttonCharacteristic)
154+ .with (buttonCallback)
155+ .enqueue()
162156
163- // Read the initial value of the LED characteristic.
164- readCharacteristic(ledCharacteristic)
165- .with (ledCallback)
166- .enqueue()
167- }
157+ // Read the initial value of the LED characteristic.
158+ readCharacteristic(ledCharacteristic)
159+ .with (ledCallback)
160+ .enqueue()
161+ }
168162
169- override fun onServicesInvalidated () {
170- ledCharacteristic = null
171- buttonCharacteristic = null
172- }
163+ override fun onServicesInvalidated () {
164+ ledCharacteristic = null
165+ buttonCharacteristic = null
173166 }
174167
175168}
0 commit comments