package [Link].
suhailcontrol
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link].*
import [Link]
import [Link]
import [Link]
import [Link]
import [Link].*
import [Link]
import [Link]
import [Link].*
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 = [Link]("00001101-0000-1000-8000-
00805F9B34FB")
// Request permissions on runtime
private val requestPermissionsLauncher =
registerForActivityResult([Link]())
{ permissions ->
if (permissions[[Link].BLUETOOTH_CONNECT] == true
&&
permissions[[Link].BLUETOOTH_SCAN] == true
) {
setupBluetooth()
} else {
[Link](this, "Permissions are required for Bluetooth
functionality", Toast.LENGTH_LONG).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
bluetoothAdapter = [Link]()
val btnEnableBluetooth: Button = findViewById([Link])
val deviceList: ListView = findViewById([Link])
val btnSendCommand: Button = findViewById([Link])
val dataDisplay: TextView = findViewById([Link])
val smartInterface: LinearLayout = findViewById([Link])
[Link] {
if ([Link]) {
[Link](this, "Bluetooth is already enabled",
Toast.LENGTH_SHORT).show()
} else {
requestPermissions()
}
}
val pairedDevices: Set<BluetoothDevice> = [Link]
val deviceListNames = ArrayList<String>()
[Link] { device ->
[Link]("${[Link]} - ${[Link]}")
}
deviceListAdapter = ArrayAdapter(this, [Link].simple_list_item_1,
deviceListNames)
[Link] = deviceListAdapter
[Link] {
sendData("1") // Customize the command to be sent
}
startAutomaticDataReceiving()
setupSmartInterface(smartInterface)
}
private fun requestPermissions() {
val permissionsNeeded = mutableListOf(
[Link].BLUETOOTH_CONNECT,
[Link].BLUETOOTH_SCAN,
[Link].ACCESS_FINE_LOCATION
)
val missingPermissions = [Link] {
[Link](this, it) !=
PackageManager.PERMISSION_GRANTED
}
if ([Link]()) {
[Link]([Link]())
} else {
setupBluetooth()
}
}
private fun setupBluetooth() {
if (![Link]) {
[Link]()
}
val pairedDevices: Set<BluetoothDevice> = [Link]
val deviceListNames = ArrayList<String>()
[Link] { device ->
[Link]("${[Link]} - ${[Link]}")
}
[Link]()
[Link](deviceListNames)
}
private fun sendData(data: String) {
outputStream?.write([Link]())
[Link](this, "Data sent", Toast.LENGTH_SHORT).show()
}
private fun startAutomaticDataReceiving() {
[Link]([Link]) {
val buffer = ByteArray(1024)
var bytes: Int
while (true) {
try {
bytes = inputStream?.read(buffer) ?: break
val receivedData = String(buffer, 0, bytes)
withContext([Link]) {
findViewById<TextView>([Link]).text =
receivedData
}
analyzeAndActOnData(receivedData)
} catch (e: Exception) {
[Link]()
break
}
}
}
}
private fun analyzeAndActOnData(data: String) {
when ([Link]()) {
"HIGH_TEMP" -> sendData(")"خفض الحرارة
"LOW_BATTERY" -> sendData(")"اشحن البطارية
// Add more conditions as needed
}
}
private fun setupSmartInterface(smartInterface: LinearLayout) {
val buttonAutomatic = Button(this).apply {
text = ""تفعيل التحكم التلقائي
setOnClickListener {
[Link](this@MainActivity, ""تم تفعيل التحكم التلقائي,
Toast.LENGTH_SHORT).show()
}
}
val buttonManual = Button(this).apply {
text = ""تفعيل التحكم اليدوي
setOnClickListener {
[Link](this@MainActivity, ""تم تفعيل التحكم اليدوي,
Toast.LENGTH_SHORT).show()
}
}
[Link](buttonAutomatic)
[Link](buttonManual)
}
}