0% found this document useful (0 votes)
7 views3 pages

Main Activity KT

The document is a Kotlin code for an Android application that manages Bluetooth connections. It allows users to enable Bluetooth, view paired devices, send commands, and receive data automatically while providing a smart interface for manual and automatic control. The app also includes functionality to analyze received data and respond accordingly based on specific conditions.

Uploaded by

eyedy2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Main Activity KT

The document is a Kotlin code for an Android application that manages Bluetooth connections. It allows users to enable Bluetooth, view paired devices, send commands, and receive data automatically while providing a smart interface for manual and automatic control. The app also includes functionality to analyze received data and respond accordingly based on specific conditions.

Uploaded by

eyedy2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package com.example.

suhailcontrol

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothSocket
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.*
import java.io.InputStream
import java.io.OutputStream
import java.util.*

class MainActivity : AppCompatActivity() {

private lateinit var bluetoothAdapter: BluetoothAdapter


private lateinit var deviceListAdapter: ArrayAdapter<String>
private var bluetoothSocket: BluetoothSocket? = null
private var outputStream: OutputStream? = null
private var inputStream: InputStream? = null

private val uuid: UUID = UUID.fromString("00001101-0000-1000-8000-


00805F9B34FB")

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
val btnEnableBluetooth: Button = findViewById(R.id.btnEnableBluetooth)
val deviceList: ListView = findViewById(R.id.deviceList)
val btnSendCommand: Button = findViewById(R.id.btnSendCommand)
val dataDisplay: TextView = findViewById(R.id.dataDisplay)
val smartInterface: LinearLayout = findViewById(R.id.smartInterface)

btnEnableBluetooth.setOnClickListener {
if (!bluetoothAdapter.isEnabled) {
bluetoothAdapter.enable()
Toast.makeText(this, "Bluetooth enabled",
Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Bluetooth already enabled",
Toast.LENGTH_SHORT).show()
}
}

val pairedDevices: Set<BluetoothDevice> = bluetoothAdapter.bondedDevices


val deviceListNames = ArrayList<String>()
for (device in pairedDevices) {
deviceListNames.add("${device.name} - ${device.address}")
}
deviceListAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1,
deviceListNames)
deviceList.adapter = deviceListAdapter

btnSendCommand.setOnClickListener {
sendData("1") // ‫يمكنك تخصيص الأمر‬
}
startAutomaticDataReceiving()
setupSmartInterface(smartInterface)
}

private fun sendData(data: String) {


if (outputStream != null) {
outputStream?.write(data.toByteArray())
Toast.makeText(this, "‫"تم إرسال البيانات‬, Toast.LENGTH_SHORT).show()
}
}

private fun startAutomaticDataReceiving() {


GlobalScope.launch(Dispatchers.IO) {
val buffer = ByteArray(1024)
var bytes: Int
while (true) {
try {
bytes = inputStream?.read(buffer) ?: break
val receivedData = String(buffer, 0, bytes)

withContext(Dispatchers.Main) {
findViewById<TextView>(R.id.dataDisplay).text =
receivedData
}

analyzeAndActOnData(receivedData)

} catch (e: Exception) {


e.printStackTrace()
break
}
}
}
}

private fun analyzeAndActOnData(data: String) {


when (data.trim()) {
"HIGH_TEMP" -> sendData("‫)"خفض الحرارة‬
"LOW_BATTERY" -> sendData("‫)"اشحن البطارية‬
// ‫شروط إضافية‬
}
}

private fun setupSmartInterface(smartInterface: LinearLayout) {


val buttonAutomatic = Button(this).apply {
text = "‫"تفعيل التحكم التلقائي‬
setOnClickListener {
Toast.makeText(this@MainActivity, "‫"تم تفعيل التحكم التلقائي‬,
Toast.LENGTH_SHORT).show()
}
}

val buttonManual = Button(this).apply {


text = "‫"تفعيل التحكم اليدوي‬
setOnClickListener {
Toast.makeText(this@MainActivity, "‫"تم تفعيل التحكم اليدوي‬,
Toast.LENGTH_SHORT).show()
}
}
smartInterface.addView(buttonAutomatic)
smartInterface.addView(buttonManual)
}
}

You might also like