Main Activity KT
Main Activity KT
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.*
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()
}
}
btnSendCommand.setOnClickListener {
sendData("1") // يمكنك تخصيص الأمر
}
startAutomaticDataReceiving()
setupSmartInterface(smartInterface)
}
withContext(Dispatchers.Main) {
findViewById<TextView>(R.id.dataDisplay).text =
receivedData
}
analyzeAndActOnData(receivedData)