Now app can create connection and talk with the device
This commit is contained in:
@@ -28,7 +28,6 @@ import kotlinx.coroutines.flow.flow
|
|||||||
import kotlinx.coroutines.flow.flowOn
|
import kotlinx.coroutines.flow.flowOn
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.flow.onCompletion
|
import kotlinx.coroutines.flow.onCompletion
|
||||||
import kotlinx.coroutines.flow.toSet
|
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
@@ -147,7 +146,8 @@ class AndroidBluetoothController(private val context: Context) : BluetoothContro
|
|||||||
)
|
)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val SERVICE_UUID = "af7cc14b-cffa-4a3d-b677-01b0ff0a93d7"
|
// SPP service UUID
|
||||||
|
const val SERVICE_UUID = "00001101-0000-1000-8000-00805F9B34FB"
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
@@ -241,7 +241,7 @@ class AndroidBluetoothController(private val context: Context) : BluetoothContro
|
|||||||
if(dataTransferService == null) {
|
if(dataTransferService == null) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
dataTransferService?.sendMessage(message.toByteArray())
|
dataTransferService?.sendMessage("R1399\n\r".toByteArray())
|
||||||
return message
|
return message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ import kotlinx.coroutines.flow.launchIn
|
|||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
class BluetoothViewModel(
|
class BluetoothViewModel(
|
||||||
private val bluetoothController: BluetoothController
|
private val bluetoothController: BluetoothController
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
@@ -130,4 +132,10 @@ class BluetoothViewModel(
|
|||||||
fun cancelScan() {
|
fun cancelScan() {
|
||||||
bluetoothController.cancelDiscovery()
|
bluetoothController.cancelDiscovery()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun sendMessage(message: KMessage) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
bluetoothController.trySendMessage(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -98,6 +98,7 @@ class MainActivity : ComponentActivity() {
|
|||||||
if (bluetoothState.isConnected) {
|
if (bluetoothState.isConnected) {
|
||||||
Toast.makeText(applicationContext, "Подключение завершено", Toast.LENGTH_LONG)
|
Toast.makeText(applicationContext, "Подключение завершено", Toast.LENGTH_LONG)
|
||||||
.show()
|
.show()
|
||||||
|
bluetoothViewModel.sendMessage(KMessage(1u,2u,3u,false, false))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,7 +106,7 @@ class MainActivity : ComponentActivity() {
|
|||||||
|
|
||||||
LaunchedEffect(key1 = bluetoothState.errorMessage) {
|
LaunchedEffect(key1 = bluetoothState.errorMessage) {
|
||||||
bluetoothState.errorMessage?.let { message ->
|
bluetoothState.errorMessage?.let { message ->
|
||||||
Toast.makeText(applicationContext, message, Toast.LENGTH_LONG).show()
|
Toast.makeText(applicationContext, "Ошибка: $message", Toast.LENGTH_LONG).show()
|
||||||
navController.navigate("scanner")
|
navController.navigate("scanner")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user