First bluetooth connection proof-of-concept
This commit is contained in:
parent
db72525fd4
commit
31c864f140
4 changed files with 63 additions and 14 deletions
|
@ -47,4 +47,6 @@ dependencies {
|
||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
||||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
||||||
implementation "androidx.navigation:navigation-fragment-ktx:2.4.2"
|
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")
|
||||||
|
}
|
|
@ -1,7 +1,10 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="com.ghoscht.heliox">
|
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
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:icon="@mipmap/ic_launcher"
|
||||||
|
|
|
@ -8,6 +8,7 @@ import android.view.ViewGroup
|
||||||
import androidx.databinding.DataBindingUtil
|
import androidx.databinding.DataBindingUtil
|
||||||
import androidx.fragment.app.viewModels
|
import androidx.fragment.app.viewModels
|
||||||
import com.ghoscht.heliox.databinding.FragmentLightControlBinding
|
import com.ghoscht.heliox.databinding.FragmentLightControlBinding
|
||||||
|
import kotlinx.coroutines.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple [Fragment] subclass.
|
* A simple [Fragment] subclass.
|
||||||
|
@ -18,6 +19,11 @@ class LightControlFragment : Fragment() {
|
||||||
private val viewModel: LightControlViewModel by viewModels()
|
private val viewModel: LightControlViewModel by viewModels()
|
||||||
private lateinit var binding: FragmentLightControlBinding
|
private lateinit var binding: FragmentLightControlBinding
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
viewModel.connect()
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
inflater: LayoutInflater, container: ViewGroup?,
|
inflater: LayoutInflater, container: ViewGroup?,
|
||||||
savedInstanceState: Bundle?
|
savedInstanceState: Bundle?
|
||||||
|
|
|
@ -1,27 +1,65 @@
|
||||||
package com.ghoscht.heliox
|
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.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
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() {
|
class LightControlViewModel : ViewModel() {
|
||||||
private val _status = MutableLiveData<String>("0,0,0,0")
|
private val _status = MutableLiveData<String>("Disconnected")
|
||||||
public val status: LiveData<String>
|
public val status: LiveData<String>
|
||||||
get() = _status
|
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(){
|
public fun connect() {
|
||||||
_status.postValue("255,255,255,255")
|
GlobalScope.launch {
|
||||||
}
|
pairedDevices = bluetoothAdapter?.bondedDevices
|
||||||
public fun turnOff(){
|
|
||||||
_status.postValue("0,0,0,0")
|
|
||||||
}
|
|
||||||
public fun toggle(){
|
|
||||||
_status.postValue("420,0,0,0")
|
|
||||||
}
|
|
||||||
public fun inc(){
|
|
||||||
|
|
||||||
|
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(){
|
public fun dec(){
|
||||||
|
btSocket.outputStream.write("0d".toByteArray())
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in a new issue