diff --git a/app/src/main/java/de/ccc/events/badge/card10/common/ConnectionException.kt b/app/src/main/java/de/ccc/events/badge/card10/common/ConnectionException.kt new file mode 100644 index 0000000000000000000000000000000000000000..1afe2808499cb8986f1a5c4d314e51c37650427e --- /dev/null +++ b/app/src/main/java/de/ccc/events/badge/card10/common/ConnectionException.kt @@ -0,0 +1,30 @@ +/* + * Copyright by the original author or authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package de.ccc.events.badge.card10.common + +import android.content.res.Resources +import de.ccc.events.badge.card10.R +import java.lang.Exception + +class ConnectionException(message: String = Resources.getSystem().getString(R.string.connection_error_generic)) : + Exception(message) \ No newline at end of file diff --git a/app/src/main/java/de/ccc/events/badge/card10/common/ConnectionService.kt b/app/src/main/java/de/ccc/events/badge/card10/common/ConnectionService.kt index 60f0e4018912f19e6a2675a7c622835d729a078c..ad8e184e8d35a9f1d3ec9ab149a58fb36a2e812e 100644 --- a/app/src/main/java/de/ccc/events/badge/card10/common/ConnectionService.kt +++ b/app/src/main/java/de/ccc/events/badge/card10/common/ConnectionService.kt @@ -22,12 +22,11 @@ package de.ccc.events.badge.card10.common -import android.bluetooth.BluetoothDevice -import android.bluetooth.BluetoothGatt -import android.bluetooth.BluetoothGattCallback -import android.bluetooth.BluetoothGattCharacteristic +import android.bluetooth.* import android.content.Context import android.util.Log +import de.ccc.events.badge.card10.CARD10_BLUETOOTH_MAC_PREFIX +import de.ccc.events.badge.card10.R import de.ccc.events.badge.card10.filetransfer.LowEffortService import java.lang.IllegalStateException import java.lang.NullPointerException @@ -68,6 +67,20 @@ object ConnectionService { throw IllegalStateException() } + // Use first BLE devices that is bonded + val bondedDevices = BluetoothAdapter.getDefaultAdapter().bondedDevices.filter { + it.address.startsWith( + CARD10_BLUETOOTH_MAC_PREFIX, + true + ) + } + + if (bondedDevices.isEmpty()) { + throw ConnectionException(context.getString(R.string.connection_error_no_bonded_device)) + } + + device = bondedDevices[0] + // 1. Connect // 2. Discover services // 3. Change MTU diff --git a/app/src/main/java/de/ccc/events/badge/card10/filetransfer/FileTransferFragment.kt b/app/src/main/java/de/ccc/events/badge/card10/filetransfer/FileTransferFragment.kt index b9ecbaef2a10ceb099eae0a49de9fcbe471e4787..de8f68642367f0caa5fc0015f09e07fde84c0bb7 100644 --- a/app/src/main/java/de/ccc/events/badge/card10/filetransfer/FileTransferFragment.kt +++ b/app/src/main/java/de/ccc/events/badge/card10/filetransfer/FileTransferFragment.kt @@ -34,6 +34,7 @@ import android.widget.ProgressBar import android.widget.TextView import androidx.annotation.UiThread import androidx.fragment.app.Fragment +import de.ccc.events.badge.card10.CARD10_BLUETOOTH_MAC_PREFIX import de.ccc.events.badge.card10.R import de.ccc.events.badge.card10.common.ConnectionService import de.ccc.events.badge.card10.common.GattListener @@ -72,15 +73,11 @@ class FileTransferFragment : Fragment(), GattListener, FileTransferListener{ buttonStartStop = view.findViewById(R.id.button_start_stop_transfer) - connect() + initConnection() toggleControls() } - private fun connect() { - // Test function that connects to specific card10 - val remoteDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:05:8B:44:42:42") - ConnectionService.device = remoteDevice - + private fun initConnection() { val ctx = context ?: throw IllegalStateException() ConnectionService.connect(ctx) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ac827b1a52fe6f327cbc69176a6ce0cf2e1ec7fd..deb1806b01cf25391b840e2adcb3327d8ec545c4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -24,4 +24,7 @@ <string name="app_detail_downloads">Downloads: %1$d</string> <string name="app_detail_content_size">Size: %1$d</string> <string name="app_detail_button_download">Download</string> + + <string name="connection_error_generic">Connection error</string> + <string name="connection_error_no_bonded_device">No bonded card10s available</string> </resources>