Skip to content

Commit fdfbb2c

Browse files
Merge pull request #187 from Semper-Viventem/master
* Improve deep analysis mode
2 parents 00f5269 + 000c49e commit fdfbb2c

File tree

8 files changed

+263
-87
lines changed

8 files changed

+263
-87
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ android {
2626
minSdk = 29
2727
targetSdk = 35
2828

29-
versionCode = 1708536371
30-
versionName = "0.29.1-beta"
29+
versionCode = 1708536372
30+
versionName = "0.29.2-beta"
3131

3232
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
3333

app/src/main/java/f/cking/software/data/helpers/BleScannerHelper.kt

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,11 @@ class BleScannerHelper(
9393
return callbackFlow {
9494
val services = mutableSetOf<BluetoothGattService>()
9595
val device = requireAdapter().getRemoteDevice(address)
96+
var gatt: BluetoothGatt? = null
9697

9798
val callback = object : BluetoothGattCallback() {
9899
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
99100
super.onServicesDiscovered(gatt, status)
100-
connections.put(gatt.device.address, gatt)
101-
102101
if (status == BluetoothGatt.GATT_SUCCESS) {
103102
Timber.tag(TAG_CONNECT).d("Services discovered. ${gatt.services.size} services for device $address")
104103
services.addAll(gatt.services.orEmpty())
@@ -129,49 +128,81 @@ class BleScannerHelper(
129128
}
130129
}
131130

132-
override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) {
131+
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
133132
super.onConnectionStateChange(gatt, status, newState)
134133
checkStatus(newState, gatt, status)
135134
}
136135

137-
private fun checkStatus(newState: Int, gatt: BluetoothGatt?, status: Int) {
136+
private fun checkStatus(newState: Int, gatt: BluetoothGatt, status: Int) {
137+
connections[address] = gatt
138138
when (newState) {
139139
BluetoothProfile.STATE_CONNECTING -> {
140140
Timber.tag(TAG_CONNECT).d("Connecting to device $address")
141141
trySend(DeviceConnectResult.Connecting)
142142
}
143143
BluetoothProfile.STATE_CONNECTED -> {
144144
Timber.tag(TAG_CONNECT).d("Connected to device $address")
145-
trySend(DeviceConnectResult.Connected(gatt!!))
145+
trySend(DeviceConnectResult.Connected(gatt))
146146
}
147147
BluetoothProfile.STATE_DISCONNECTING -> {
148148
Timber.tag(TAG_CONNECT).d("Disconnecting from device $address")
149149
trySend(DeviceConnectResult.Disconnecting)
150+
gatt.close()
150151
}
151152
BluetoothProfile.STATE_DISCONNECTED -> {
152153
Timber.tag(TAG_CONNECT).d("Disconnected from device $address")
153-
if (status == 0x85) {
154-
Timber.tag(TAG_CONNECT).e("Error while connecting to device $address. Error code: $status")
155-
trySend(DeviceConnectResult.MaxGattConnectionsReached)
156-
} else {
157-
trySend(DeviceConnectResult.Disconnected)
158-
}
154+
handleDisconnect(status, gatt)
155+
}
156+
else -> {
157+
Timber.tag(TAG_CONNECT).e("Error while connecting to device $address. Error code: $status")
158+
trySend(DeviceConnectResult.DisconnectedWithError.UnspecifiedConnectionError(gatt, status))
159+
}
160+
}
161+
}
162+
163+
private fun handleDisconnect(status: Int, gatt: BluetoothGatt) {
164+
when (status) {
165+
BluetoothGatt.GATT_SUCCESS -> {
166+
trySend(DeviceConnectResult.Disconnected)
167+
}
168+
CONNECTION_FAILED_TO_ESTABLISH -> {
169+
Timber.tag(TAG_CONNECT).e("Error while connecting to device $address. Error code: $status")
170+
trySend(DeviceConnectResult.DisconnectedWithError.ConnectionFailedToEstablish(gatt, status))
171+
}
172+
CONNECTION_FAILED_BEFORE_INITIALIZING -> {
173+
Timber.tag(TAG_CONNECT).e("Error while connecting to device $address. Error code: $status")
174+
trySend(DeviceConnectResult.DisconnectedWithError.ConnectionFailedBeforeInitializing(gatt, status))
175+
}
176+
CONNECTION_TERMINATED -> {
177+
Timber.tag(TAG_CONNECT).e("Error while connecting to device $address. Error code: $status")
178+
trySend(DeviceConnectResult.DisconnectedWithError.ConnectionTerminated(gatt, status))
179+
}
180+
BluetoothGatt.GATT_CONNECTION_TIMEOUT -> {
181+
Timber.tag(TAG_CONNECT).e("Error while connecting to device $address. Error code: $status")
182+
trySend(DeviceConnectResult.DisconnectedWithError.ConnectionTimeout(gatt, status))
183+
}
184+
BluetoothGatt.GATT_FAILURE -> {
185+
Timber.tag(TAG_CONNECT).e("Error while connecting to device $address. Error code: $status")
186+
trySend(DeviceConnectResult.DisconnectedWithError.ConnectionFailedTooManyClients(gatt, status))
159187
}
160188
else -> {
161189
Timber.tag(TAG_CONNECT).e("Error while connecting to device $address. Error code: $status")
162-
trySend(DeviceConnectResult.DisconnectedWithError(status))
163-
false
190+
trySend(DeviceConnectResult.DisconnectedWithError.UnspecifiedConnectionError(gatt, status))
164191
}
165192
}
166193
}
167194
}
168195

169196
Timber.tag(TAG_CONNECT).d("Connecting to device $address")
170-
connections[address] = device.connectGatt(appContext, false, callback)
197+
gatt = device.connectGatt(appContext, false, callback, BluetoothDevice.TRANSPORT_LE)
171198

172199
awaitClose {
173200
Timber.tag(TAG_CONNECT).d("Closing connection to device $address")
174-
closeDeviceConnection(address)
201+
if (requireBluetoothManager().getConnectionState(device, BluetoothProfile.GATT) != BluetoothProfile.STATE_DISCONNECTED) {
202+
gatt.disconnect()
203+
} else {
204+
gatt.close()
205+
}
175206
}
176207
}
177208
}
@@ -229,8 +260,17 @@ class BleScannerHelper(
229260
data class Connected(val gatt: BluetoothGatt) : DeviceConnectResult
230261
data object Disconnecting : DeviceConnectResult
231262
data object Disconnected : DeviceConnectResult
232-
data class DisconnectedWithError(val errorCode: Int) : DeviceConnectResult
233-
data object MaxGattConnectionsReached : DeviceConnectResult
263+
sealed interface DisconnectedWithError : DeviceConnectResult {
264+
val errorCode: Int
265+
val gatt: BluetoothGatt
266+
267+
class UnspecifiedConnectionError(override val gatt: BluetoothGatt, override val errorCode: Int) : DisconnectedWithError
268+
class ConnectionTimeout(override val gatt: BluetoothGatt, override val errorCode: Int) : DisconnectedWithError
269+
class ConnectionTerminated(override val gatt: BluetoothGatt, override val errorCode: Int) : DisconnectedWithError
270+
class ConnectionFailedToEstablish(override val gatt: BluetoothGatt, override val errorCode: Int) : DisconnectedWithError
271+
class ConnectionFailedBeforeInitializing(override val gatt: BluetoothGatt, override val errorCode: Int) : DisconnectedWithError
272+
class ConnectionFailedTooManyClients(override val gatt: BluetoothGatt, override val errorCode: Int) : DisconnectedWithError
273+
}
234274
}
235275

236276
fun isBluetoothEnabled(): Boolean {
@@ -306,10 +346,14 @@ class BleScannerHelper(
306346
}
307347

308348
private fun tryToInitBluetoothScanner() {
309-
bluetoothAdapter = appContext.getSystemService(BluetoothManager::class.java).adapter
349+
bluetoothAdapter = requireBluetoothManager().adapter
310350
bluetoothScanner = bluetoothAdapter?.bluetoothLeScanner
311351
}
312352

353+
private fun requireBluetoothManager(): BluetoothManager {
354+
return appContext.getSystemService(BluetoothManager::class.java)
355+
}
356+
313357
private fun requireScanner(): BluetoothLeScanner {
314358
if (bluetoothScanner == null) {
315359
tryToInitBluetoothScanner()
@@ -346,5 +390,8 @@ class BleScannerHelper(
346390
companion object {
347391
private const val TAG = "BleScannerHelper"
348392
private const val TAG_CONNECT = "BleScannerHelperConnect"
393+
private const val CONNECTION_FAILED_BEFORE_INITIALIZING = 0x85
394+
private const val CONNECTION_FAILED_TO_ESTABLISH = 0x3E
395+
private const val CONNECTION_TERMINATED = 0x16
349396
}
350397
}

app/src/main/java/f/cking/software/domain/interactor/BuildDeviceClassFromSystemInfo.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,10 @@ object BuildDeviceClassFromSystemInfo {
181181
" TV" to DeviceClass.AudioVideo.VideoDisplayAndLoudspeaker,
182182
"TV " to DeviceClass.AudioVideo.VideoDisplayAndLoudspeaker,
183183
"MacBook" to DeviceClass.Computer.Laptop,
184-
"Mac" to DeviceClass.Computer.Desktop,
184+
"iMac" to DeviceClass.Computer.Desktop,
185+
"Macmini" to DeviceClass.Computer.Desktop,
186+
"Mac1" to DeviceClass.Computer.Desktop,
187+
"Mac2" to DeviceClass.Computer.Desktop,
185188
"iPad" to DeviceClass.Phone.Smartphone,
186189
)
187190
}

0 commit comments

Comments
 (0)