primary functions
This commit is contained in:
parent
1dc65adce4
commit
ccc3988c51
3
.idea/deploymentTargetSelector.xml
generated
3
.idea/deploymentTargetSelector.xml
generated
@ -5,6 +5,9 @@
|
|||||||
<SelectionState runConfigName="app">
|
<SelectionState runConfigName="app">
|
||||||
<option name="selectionMode" value="DROPDOWN" />
|
<option name="selectionMode" value="DROPDOWN" />
|
||||||
</SelectionState>
|
</SelectionState>
|
||||||
|
<SelectionState runConfigName="MainActivity">
|
||||||
|
<option name="selectionMode" value="DROPDOWN" />
|
||||||
|
</SelectionState>
|
||||||
</selectionStates>
|
</selectionStates>
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@ -1,24 +1,108 @@
|
|||||||
package ovh.smayzy.i2d_project_15_app
|
package ovh.smayzy.i2d_project_15_app
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
|
import android.bluetooth.BluetoothAdapter
|
||||||
|
import android.bluetooth.BluetoothDevice
|
||||||
|
import android.bluetooth.BluetoothSocket
|
||||||
|
import android.content.pm.PackageManager
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.activity.enableEdgeToEdge
|
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
|
||||||
import android.widget.Button
|
import android.widget.Button
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.core.view.ViewCompat
|
import androidx.activity.enableEdgeToEdge
|
||||||
import androidx.core.view.WindowInsetsCompat
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import androidx.core.app.ActivityCompat
|
||||||
|
import java.io.IOException
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
|
private val tag = "MainActivity"
|
||||||
|
private val requestEnableBt = 1
|
||||||
|
private val deviceName = "HC-05"
|
||||||
|
private val sppUuid: UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
|
||||||
|
|
||||||
|
private lateinit var bluetoothManager: android.bluetooth.BluetoothManager
|
||||||
|
private var bluetoothAdapter: BluetoothAdapter? = null
|
||||||
|
private var bluetoothSocket: BluetoothSocket? = null
|
||||||
|
private var isConnected = false
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
enableEdgeToEdge()
|
enableEdgeToEdge()
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
|
||||||
val connectBtn = findViewById<Button>(R.id.connect_btn)
|
val connectBtn = findViewById<Button>(R.id.connect_btn)
|
||||||
|
|
||||||
|
bluetoothManager = getSystemService(BLUETOOTH_SERVICE) as android.bluetooth.BluetoothManager
|
||||||
|
bluetoothAdapter = bluetoothManager.adapter
|
||||||
|
if (bluetoothAdapter == null) {
|
||||||
|
Toast.makeText(this, "Bluetooth not supported by your device", Toast.LENGTH_LONG).show()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
connectBtn.setOnClickListener {
|
connectBtn.setOnClickListener {
|
||||||
Log.i("MainActivity", "connect-btn was clicked")
|
if (isConnected) {
|
||||||
Toast.makeText(this, getString(R.string.connected), Toast.LENGTH_SHORT).show()
|
Toast.makeText(this, "Already connected", Toast.LENGTH_SHORT).show()
|
||||||
|
return@setOnClickListener
|
||||||
|
}
|
||||||
|
|
||||||
|
connectToHC05()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun connectToHC05() {
|
||||||
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
|
||||||
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.BLUETOOTH_CONNECT), requestEnableBt)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), requestEnableBt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val pairedDevices: Set<BluetoothDevice>? = bluetoothAdapter?.bondedDevices
|
||||||
|
val device = pairedDevices?.find { it.name == deviceName }
|
||||||
|
|
||||||
|
if (device == null) {
|
||||||
|
Toast.makeText(this, "HC-05 not found, try pairing it to your phone via bluetooth", Toast.LENGTH_LONG).show()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread {
|
||||||
|
try {
|
||||||
|
bluetoothSocket = device.createRfcommSocketToServiceRecord(sppUuid)
|
||||||
|
bluetoothSocket?.connect()
|
||||||
|
isConnected = true
|
||||||
|
runOnUiThread {
|
||||||
|
Toast.makeText(this, "Connected to HC-05", Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
listenForData()
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Log.e(tag, "Connection failed", e)
|
||||||
|
runOnUiThread {
|
||||||
|
Toast.makeText(this, "Connection failed: ${e.message}", Toast.LENGTH_LONG).show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.start()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun listenForData() {
|
||||||
|
val inputStream = bluetoothSocket?.inputStream ?: return
|
||||||
|
val buffer = ByteArray(64)
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
val bytes = inputStream.read(buffer)
|
||||||
|
val message = String(buffer, 0, bytes)
|
||||||
|
Log.i(tag, "Received: $message")
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Log.e(tag, "Can't read data", e)
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user