From 31c864f140ac9f6b721b1711e53ee22e060108e3 Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Mon, 11 Apr 2022 16:32:50 +0200 Subject: [PATCH] First bluetooth connection proof-of-concept --- modules/android/app/build.gradle | 4 +- .../android/app/src/main/AndroidManifest.xml | 5 +- .../ghoscht/heliox/LightControlFragment.kt | 6 ++ .../ghoscht/heliox/LightControlViewModel.kt | 62 +++++++++++++++---- 4 files changed, 63 insertions(+), 14 deletions(-) diff --git a/modules/android/app/build.gradle b/modules/android/app/build.gradle index e49f215..f5e0e6c 100644 --- a/modules/android/app/build.gradle +++ b/modules/android/app/build.gradle @@ -47,4 +47,6 @@ dependencies { androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation "androidx.navigation:navigation-fragment-ktx:2.4.2" - implementation "androidx.navigation:navigation-ui-ktx:2.4.2"} \ No newline at end of file + implementation "androidx.navigation:navigation-ui-ktx:2.4.2" + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0") +} \ No newline at end of file diff --git a/modules/android/app/src/main/AndroidManifest.xml b/modules/android/app/src/main/AndroidManifest.xml index 7ec4593..9332c73 100644 --- a/modules/android/app/src/main/AndroidManifest.xml +++ b/modules/android/app/src/main/AndroidManifest.xml @@ -1,7 +1,10 @@ - + + + + ("0,0,0,0") + private val _status = MutableLiveData("Disconnected") public val status: LiveData get() = _status + private lateinit var connectedDevice: BluetoothDevice + private lateinit var btSocket: BluetoothSocket + val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter() + var pairedDevices: Set? = null - public fun turnOn(){ - _status.postValue("255,255,255,255") - } - public fun turnOff(){ - _status.postValue("0,0,0,0") - } - public fun toggle(){ - _status.postValue("420,0,0,0") - } - public fun inc(){ + public fun connect() { + GlobalScope.launch { + pairedDevices = bluetoothAdapter?.bondedDevices + val pairedDevices: Set? = bluetoothAdapter?.bondedDevices + pairedDevices?.forEach { device -> + if (device.name == "Heliox") { + connectedDevice = device + btSocket = + device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")) + btSocket.connect() + while (true) { + _status.postValue(btSocket.inputStream.bufferedReader().readLine()) + } + } + } + } } + + public fun turnOn() { + btSocket.outputStream.write("on".toByteArray()) + } + + public fun turnOff() { + btSocket.outputStream.write("off".toByteArray()) + } + + public fun toggle() { + btSocket.outputStream.write("0t".toByteArray()) + } + + public fun inc() { + btSocket.outputStream.write("0i".toByteArray()) + } + public fun dec(){ - + btSocket.outputStream.write("0d".toByteArray()) } } \ No newline at end of file