K
K
suhailcontrol
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothSocket
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.os.Bundle
import android.widget.*
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.coroutines.*
import java.io.InputStream
import java.io.OutputStream
import java.util.*
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions())
{ permissions ->
if (permissions[android.Manifest.permission.BLUETOOTH_CONNECT] == true
&&
permissions[android.Manifest.permission.BLUETOOTH_SCAN] == true
) {
setupBluetooth()
} else {
Toast.makeText(this, "Permissions are required for Bluetooth
functionality", Toast.LENGTH_LONG).show()
}
}
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) {
Toast.makeText(this, "Bluetooth is already enabled",
Toast.LENGTH_SHORT).show()
} else {
requestPermissions()
}
}
btnSendCommand.setOnClickListener {
sendData("1") // Customize the command to be sent
}
startAutomaticDataReceiving()
setupSmartInterface(smartInterface)
}
if (missingPermissions.isNotEmpty()) {
requestPermissionsLauncher.launch(missingPermissions.toTypedArray())
} else {
setupBluetooth()
}
}
deviceListAdapter.clear()
deviceListAdapter.addAll(deviceListNames)
}
withContext(Dispatchers.Main) {
findViewById<TextView>(R.id.dataDisplay).text =
receivedData
}
analyzeAndActOnData(receivedData)
smartInterface.addView(buttonAutomatic)
smartInterface.addView(buttonManual)
}
}