First bluetooth connection proof-of-concept

This commit is contained in:
GHOSCHT 2022-04-11 16:32:50 +02:00
parent db72525fd4
commit 31c864f140
No known key found for this signature in database
GPG key ID: A35BD466B8871994
4 changed files with 63 additions and 14 deletions

View file

@ -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"}
implementation "androidx.navigation:navigation-ui-ktx:2.4.2"
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0")
}

View file

@ -1,7 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ghoscht.heliox">
<uses-feature android:name="android.hardware.bluetooth" android:required="true" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"

View file

@ -8,6 +8,7 @@ import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.viewModels
import com.ghoscht.heliox.databinding.FragmentLightControlBinding
import kotlinx.coroutines.*
/**
* A simple [Fragment] subclass.
@ -18,6 +19,11 @@ class LightControlFragment : Fragment() {
private val viewModel: LightControlViewModel by viewModels()
private lateinit var binding: FragmentLightControlBinding
override fun onCreate(savedInstanceState: Bundle?) {
viewModel.connect()
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?

View file

@ -1,27 +1,65 @@
package com.ghoscht.heliox
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothSocket
import android.content.Intent
import android.util.Log
import androidx.core.app.ActivityCompat.startActivityForResult
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.BufferedWriter
import java.io.IOException
import java.util.*
class LightControlViewModel : ViewModel() {
private val _status = MutableLiveData<String>("0,0,0,0")
private val _status = MutableLiveData<String>("Disconnected")
public val status: LiveData<String>
get() = _status
private lateinit var connectedDevice: BluetoothDevice
private lateinit var btSocket: BluetoothSocket
val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
var pairedDevices: Set<BluetoothDevice>? = 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<BluetoothDevice>? = 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())
}
}