@@ -24,7 +24,6 @@ import com.juul.kable.gatt.Response.OnCharacteristicRead
2424import com.juul.kable.gatt.Response.OnCharacteristicWrite
2525import com.juul.kable.gatt.Response.OnDescriptorRead
2626import com.juul.kable.gatt.Response.OnDescriptorWrite
27- import com.juul.kable.gatt.Response.OnMtuChanged
2827import com.juul.kable.gatt.Response.OnReadRemoteRssi
2928import com.juul.kable.gatt.Response.OnServicesDiscovered
3029import kotlinx.atomicfu.atomic
@@ -38,6 +37,7 @@ import kotlinx.coroutines.SupervisorJob
3837import kotlinx.coroutines.async
3938import kotlinx.coroutines.flow.Flow
4039import kotlinx.coroutines.flow.MutableStateFlow
40+ import kotlinx.coroutines.flow.StateFlow
4141import kotlinx.coroutines.flow.asStateFlow
4242import kotlinx.coroutines.flow.combine
4343import kotlinx.coroutines.flow.first
@@ -136,6 +136,15 @@ public class AndroidPeripheral internal constructor(
136136 private val _state = MutableStateFlow <State >(State .Disconnected ())
137137 public override val state: Flow <State > = _state .asStateFlow()
138138
139+ private val _mtu = MutableStateFlow <Int ?>(null )
140+
141+ /* *
142+ * [StateFlow] of the most recently negotiated MTU. The MTU will change upon a successful request to change the MTU
143+ * (via [requestMtu]), or if the peripheral initiates an MTU change. [StateFlow]'s `value` will be `null` until MTU
144+ * is negotiated.
145+ */
146+ public val mtu: StateFlow <Int ?> = _mtu .asStateFlow()
147+
139148 private val observers = Observers (this )
140149
141150 @Volatile
@@ -168,6 +177,7 @@ public class AndroidPeripheral internal constructor(
168177 transport,
169178 phy,
170179 _state ,
180+ _mtu ,
171181 invokeOnClose = { connectJob.value = null }
172182 ) ? : throw ConnectionRejectedException ()
173183
@@ -232,12 +242,16 @@ public class AndroidPeripheral internal constructor(
232242 .map { it.toPlatformService() }
233243 }
234244
235- /* * @throws NotReadyException if invoked without an established [connection][Peripheral.connect]. */
236- public suspend fun requestMtu (mtu : Int ) {
237- connection.execute<OnMtuChanged > {
238- this @execute.requestMtu(mtu)
239- }
240- }
245+ /* *
246+ * Requests that the current connection's MTU be changed. Suspends until the MTU changes, or failure occurs. The
247+ * negotiated MTU value is returned, which may not be [mtu] value requested if the remote peripheral negotiated an
248+ * alternate MTU.
249+ *
250+ * @throws NotReadyException if invoked without an established [connection][Peripheral.connect].
251+ * @throws GattRequestRejectedException if Android was unable to fulfill the MTU change request.
252+ * @throws GattStatusException if MTU change request failed.
253+ */
254+ public suspend fun requestMtu (mtu : Int ): Int = connection.requestMtu(mtu)
241255
242256 public override suspend fun write (
243257 characteristic : Characteristic ,
0 commit comments