From ad13a7e958658cf3687372af8aafb7fef4490fc1 Mon Sep 17 00:00:00 2001 From: gogacoder Date: Sat, 30 Sep 2023 22:08:51 +0700 Subject: [PATCH] Now app can create connection and talk with the device --- .../main/java/com/helible/pilot/BluetoothController.kt | 6 +++--- app/src/main/java/com/helible/pilot/BluetoothViewModel.kt | 8 ++++++++ app/src/main/java/com/helible/pilot/MainActivity.kt | 3 ++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/helible/pilot/BluetoothController.kt b/app/src/main/java/com/helible/pilot/BluetoothController.kt index cc3d916..3e9e4ef 100644 --- a/app/src/main/java/com/helible/pilot/BluetoothController.kt +++ b/app/src/main/java/com/helible/pilot/BluetoothController.kt @@ -28,7 +28,6 @@ import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onCompletion -import kotlinx.coroutines.flow.toSet import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import java.io.IOException @@ -147,7 +146,8 @@ class AndroidBluetoothController(private val context: Context) : BluetoothContro ) companion object { - const val SERVICE_UUID = "af7cc14b-cffa-4a3d-b677-01b0ff0a93d7" + // SPP service UUID + const val SERVICE_UUID = "00001101-0000-1000-8000-00805F9B34FB" } init { @@ -241,7 +241,7 @@ class AndroidBluetoothController(private val context: Context) : BluetoothContro if(dataTransferService == null) { return null } - dataTransferService?.sendMessage(message.toByteArray()) + dataTransferService?.sendMessage("R1399\n\r".toByteArray()) return message } diff --git a/app/src/main/java/com/helible/pilot/BluetoothViewModel.kt b/app/src/main/java/com/helible/pilot/BluetoothViewModel.kt index 158d4bf..74ddfff 100644 --- a/app/src/main/java/com/helible/pilot/BluetoothViewModel.kt +++ b/app/src/main/java/com/helible/pilot/BluetoothViewModel.kt @@ -15,6 +15,8 @@ import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch + class BluetoothViewModel( private val bluetoothController: BluetoothController ) : ViewModel() { @@ -130,4 +132,10 @@ class BluetoothViewModel( fun cancelScan() { bluetoothController.cancelDiscovery() } + + fun sendMessage(message: KMessage) { + viewModelScope.launch { + bluetoothController.trySendMessage(message) + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/helible/pilot/MainActivity.kt b/app/src/main/java/com/helible/pilot/MainActivity.kt index eed566c..6bcf758 100644 --- a/app/src/main/java/com/helible/pilot/MainActivity.kt +++ b/app/src/main/java/com/helible/pilot/MainActivity.kt @@ -98,6 +98,7 @@ class MainActivity : ComponentActivity() { if (bluetoothState.isConnected) { Toast.makeText(applicationContext, "Подключение завершено", Toast.LENGTH_LONG) .show() + bluetoothViewModel.sendMessage(KMessage(1u,2u,3u,false, false)) } } @@ -105,7 +106,7 @@ class MainActivity : ComponentActivity() { LaunchedEffect(key1 = bluetoothState.errorMessage) { bluetoothState.errorMessage?.let { message -> - Toast.makeText(applicationContext, message, Toast.LENGTH_LONG).show() + Toast.makeText(applicationContext, "Ошибка: $message", Toast.LENGTH_LONG).show() navController.navigate("scanner") } }