Now app can create connection and talk with the device

This commit is contained in:
2023-09-30 22:08:51 +07:00
parent 0c949a9bda
commit ad13a7e958
3 changed files with 13 additions and 4 deletions

View File

@@ -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
}

View File

@@ -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)
}
}
}

View File

@@ -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")
}
}