@@ -133,123 +133,12 @@ internal class BeckonBleManager(
133133 }
134134 }
135135
136- private val gattCallback = object : BleManagerGattCallback () {
137-
138- override fun initialize () {
139- Timber .d(" initialize" )
140- if (bluetoothGatt != null ) {
141- val services = bluetoothGatt!! .services.map { it.uuid }
142- val characteristics = allCharacteristics(bluetoothGatt!! )
143- val detail = DeviceDetail (services, characteristics)
144-
145- // TODO Fix disposable
146- val disposable =
147- Observable .just(Unit ).delay(1600 , TimeUnit .MILLISECONDS ).flatMapCompletable {
148- subscribeBla(descriptor.subscribes, detail)
149- }
150- .andThen(readBla(descriptor.reads, detail).ignoreElements())
151- .subscribe(
152- {
153- devicesSubject.onSuccess(detail.right())
154- },
155- {
156- devicesSubject.onSuccess(
157- ConnectionError .GeneralError (
158- device.address,
159- it
160- ).left()
161- )
162- }
163- )
164- } else {
165- devicesSubject.onSuccess(ConnectionError .BluetoothGattNull (device.address).left())
166- }
167- }
168-
169- override fun onDeviceDisconnected () {
170- Timber .d(" onDeviceDisconnected gattCallback" )
171- }
172-
173- override fun isRequiredServiceSupported (gatt : BluetoothGatt ): Boolean {
174- Timber .d(" isRequiredServiceSupported $gatt " )
175- val services = gatt.services.map { it.uuid }
176- Timber .d(" All discovered services $services " )
177- bluetoothGatt = gatt
178-
179- return true
180- }
181-
182- private fun allCharacteristics (gatt : BluetoothGatt ): List <CharacteristicSuccess > {
183- return gatt.services.flatMap { allCharacteristics(it) }
184- }
185-
186- private fun allCharacteristics (service : BluetoothGattService ): List <CharacteristicSuccess > {
187- return service.characteristics.flatMap {
188- allCharacteristics(service, it)
189- }
190- }
191-
192- fun allCharacteristics (
193- service : BluetoothGattService ,
194- char : BluetoothGattCharacteristic
195- ): List <CharacteristicSuccess > {
196- return Property .values().toList()
197- .map { findCharacteristic(service, char, it) }
198- .filterOption()
199- }
200-
201- private fun findCharacteristic (
202- service : BluetoothGattService ,
203- char : BluetoothGattCharacteristic ,
204- type : Property
205- ): Option <CharacteristicSuccess > {
206- return when (type) {
207- Property .WRITE -> writeCharacteristic(service, char)
208- Property .READ -> readCharacteristic(service, char)
209- Property .NOTIFY -> notifyCharacteristic(service, char)
210- }
211- }
212-
213- private fun notifyCharacteristic (
214- service : BluetoothGattService ,
215- char : BluetoothGattCharacteristic
216- ): Option <CharacteristicSuccess .Notify > {
217- return if (char.properties and BluetoothGattCharacteristic .PROPERTY_NOTIFY > 0 &&
218- char.properties and BluetoothGattCharacteristic .PROPERTY_READ > 0
219- ) {
220- Option (CharacteristicSuccess .Notify (char.uuid, service.uuid, char))
221- } else {
222- None
223- }
224- }
225-
226- private fun readCharacteristic (
227- service : BluetoothGattService ,
228- char : BluetoothGattCharacteristic
229- ): Option <CharacteristicSuccess .Read > {
230- return if (char.properties and BluetoothGattCharacteristic .PROPERTY_READ > 0 ) {
231- Option (CharacteristicSuccess .Read (char.uuid, service.uuid, char))
232- } else {
233- None
234- }
235- }
236-
237- private fun writeCharacteristic (
238- service : BluetoothGattService ,
239- char : BluetoothGattCharacteristic
240- ): Option <CharacteristicSuccess .Write > {
241- return if (char.properties and BluetoothGattCharacteristic .PERMISSION_WRITE > 0 ) {
242- Option (CharacteristicSuccess .Write (char.uuid, service.uuid, char))
243- } else {
244- None
245- }
246- }
247- }
136+ // private val gattCallback =
248137
249138 fun doCreateBond (): Completable {
250139 bondSubject.onNext(BondState .CreatingBond )
251140 return Completable .create { emitter ->
252- ensureBond ()
141+ createBondInsecure ()
253142 .done { emitter.onComplete() }
254143 .fail { device, status ->
255144 emitter.onError(
@@ -281,7 +170,118 @@ internal class BeckonBleManager(
281170 }
282171
283172 override fun getGattCallback (): BleManagerGattCallback {
284- return gattCallback
173+ return object : BleManagerGattCallback () {
174+
175+ override fun initialize () {
176+ Timber .d(" initialize" )
177+ if (bluetoothGatt != null ) {
178+ val services = bluetoothGatt!! .services.map { it.uuid }
179+ val characteristics = allCharacteristics(bluetoothGatt!! )
180+ val detail = DeviceDetail (services, characteristics)
181+
182+ // TODO Fix disposable
183+ val disposable =
184+ Observable .just(Unit ).delay(1600 , TimeUnit .MILLISECONDS ).flatMapCompletable {
185+ subscribeBla(descriptor.subscribes, detail)
186+ }
187+ .andThen(readBla(descriptor.reads, detail).ignoreElements())
188+ .subscribe(
189+ {
190+ devicesSubject.onSuccess(detail.right())
191+ },
192+ {
193+ devicesSubject.onSuccess(
194+ ConnectionError .GeneralError (
195+ device.address,
196+ it
197+ ).left()
198+ )
199+ }
200+ )
201+ } else {
202+ devicesSubject.onSuccess(ConnectionError .BluetoothGattNull (device.address).left())
203+ }
204+ }
205+
206+ override fun onDeviceDisconnected () {
207+ Timber .d(" onDeviceDisconnected gattCallback" )
208+ }
209+
210+ override fun isRequiredServiceSupported (gatt : BluetoothGatt ): Boolean {
211+ Timber .d(" isRequiredServiceSupported $gatt " )
212+ val services = gatt.services.map { it.uuid }
213+ Timber .d(" All discovered services $services " )
214+ bluetoothGatt = gatt
215+
216+ return true
217+ }
218+
219+ private fun allCharacteristics (gatt : BluetoothGatt ): List <CharacteristicSuccess > {
220+ return gatt.services.flatMap { allCharacteristics(it) }
221+ }
222+
223+ private fun allCharacteristics (service : BluetoothGattService ): List <CharacteristicSuccess > {
224+ return service.characteristics.flatMap {
225+ allCharacteristics(service, it)
226+ }
227+ }
228+
229+ fun allCharacteristics (
230+ service : BluetoothGattService ,
231+ char : BluetoothGattCharacteristic
232+ ): List <CharacteristicSuccess > {
233+ return Property .values().toList()
234+ .map { findCharacteristic(service, char, it) }
235+ .filterOption()
236+ }
237+
238+ private fun findCharacteristic (
239+ service : BluetoothGattService ,
240+ char : BluetoothGattCharacteristic ,
241+ type : Property
242+ ): Option <CharacteristicSuccess > {
243+ return when (type) {
244+ Property .WRITE -> writeCharacteristic(service, char)
245+ Property .READ -> readCharacteristic(service, char)
246+ Property .NOTIFY -> notifyCharacteristic(service, char)
247+ }
248+ }
249+
250+ private fun notifyCharacteristic (
251+ service : BluetoothGattService ,
252+ char : BluetoothGattCharacteristic
253+ ): Option <CharacteristicSuccess .Notify > {
254+ return if (char.properties and BluetoothGattCharacteristic .PROPERTY_NOTIFY > 0 &&
255+ char.properties and BluetoothGattCharacteristic .PROPERTY_READ > 0
256+ ) {
257+ Option (CharacteristicSuccess .Notify (char.uuid, service.uuid, char))
258+ } else {
259+ None
260+ }
261+ }
262+
263+ private fun readCharacteristic (
264+ service : BluetoothGattService ,
265+ char : BluetoothGattCharacteristic
266+ ): Option <CharacteristicSuccess .Read > {
267+ return if (char.properties and BluetoothGattCharacteristic .PROPERTY_READ > 0 ) {
268+ Option (CharacteristicSuccess .Read (char.uuid, service.uuid, char))
269+ } else {
270+ None
271+ }
272+ }
273+
274+ private fun writeCharacteristic (
275+ service : BluetoothGattService ,
276+ char : BluetoothGattCharacteristic
277+ ): Option <CharacteristicSuccess .Write > {
278+ return if (char.properties and BluetoothGattCharacteristic .PERMISSION_WRITE > 0 ) {
279+ Option (CharacteristicSuccess .Write (char.uuid, service.uuid, char))
280+ } else {
281+ None
282+ }
283+ }
284+ }
285285 }
286286
287287 fun connectionState (): Observable <ConnectionState > {
0 commit comments