|
|
@ -48,13 +48,16 @@ class MainActivity : AppCompatActivity() {
|
|
|
|
private lateinit var btDeviceName: TextView
|
|
|
|
private lateinit var btDeviceName: TextView
|
|
|
|
private lateinit var btDeviceMac: TextView
|
|
|
|
private lateinit var btDeviceMac: TextView
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private var btConnectPermission = false
|
|
|
|
|
|
|
|
private var btScanPermission = false
|
|
|
|
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setContentView(R.layout.activity_main)
|
|
|
|
setContentView(R.layout.activity_main)
|
|
|
|
|
|
|
|
|
|
|
|
mContext = applicationContext
|
|
|
|
mContext = applicationContext
|
|
|
|
|
|
|
|
|
|
|
|
// Tab layout
|
|
|
|
// tab layout
|
|
|
|
tabLayout = findViewById(R.id.tab)
|
|
|
|
tabLayout = findViewById(R.id.tab)
|
|
|
|
|
|
|
|
|
|
|
|
// TCP
|
|
|
|
// TCP
|
|
|
@ -63,22 +66,22 @@ class MainActivity : AppCompatActivity() {
|
|
|
|
ipInput = findViewById(R.id.ip_input)
|
|
|
|
ipInput = findViewById(R.id.ip_input)
|
|
|
|
portInput = findViewById(R.id.port_input)
|
|
|
|
portInput = findViewById(R.id.port_input)
|
|
|
|
|
|
|
|
|
|
|
|
// Bluetooth
|
|
|
|
// bluetooth
|
|
|
|
val selectedDevice = findViewById<ConstraintLayout>(R.id.selected)
|
|
|
|
val selectedDevice = findViewById<ConstraintLayout>(R.id.selected)
|
|
|
|
btDeviceName = findViewById(R.id.textView_device_name)
|
|
|
|
btDeviceName = findViewById(R.id.textView_device_name)
|
|
|
|
btDeviceMac = findViewById(R.id.textView_device_mac)
|
|
|
|
btDeviceMac = findViewById(R.id.textView_device_mac)
|
|
|
|
|
|
|
|
|
|
|
|
// Connect button
|
|
|
|
// connect button
|
|
|
|
val connectButton = findViewById<Button>(R.id.button_connect)
|
|
|
|
val connectButton = findViewById<Button>(R.id.button_connect)
|
|
|
|
|
|
|
|
|
|
|
|
// GitHub link
|
|
|
|
// GitHub link
|
|
|
|
val sourceLink = findViewById<TextView>(R.id.textView_github)
|
|
|
|
val sourceLink = findViewById<TextView>(R.id.textView_github)
|
|
|
|
sourceLink.movementMethod = LinkMovementMethod.getInstance()
|
|
|
|
sourceLink.movementMethod = LinkMovementMethod.getInstance()
|
|
|
|
|
|
|
|
|
|
|
|
// Read preference
|
|
|
|
// read shared preference
|
|
|
|
readSharedPref()
|
|
|
|
readSharedPref()
|
|
|
|
|
|
|
|
|
|
|
|
// Tab select listener
|
|
|
|
// tab select listener
|
|
|
|
tabLayout.addOnTabSelectedListener(
|
|
|
|
tabLayout.addOnTabSelectedListener(
|
|
|
|
object : TabLayout.OnTabSelectedListener {
|
|
|
|
object : TabLayout.OnTabSelectedListener {
|
|
|
|
override fun onTabSelected(tab: TabLayout.Tab?) {
|
|
|
|
override fun onTabSelected(tab: TabLayout.Tab?) {
|
|
|
@ -87,6 +90,10 @@ class MainActivity : AppCompatActivity() {
|
|
|
|
portLayout.visibility = View.VISIBLE
|
|
|
|
portLayout.visibility = View.VISIBLE
|
|
|
|
selectedDevice.visibility = View.GONE
|
|
|
|
selectedDevice.visibility = View.GONE
|
|
|
|
} else if (tab.text == TAB_BLUETOOTH) {
|
|
|
|
} else if (tab.text == TAB_BLUETOOTH) {
|
|
|
|
|
|
|
|
checkPermission(
|
|
|
|
|
|
|
|
"android.permission.BLUETOOTH_CONNECT",
|
|
|
|
|
|
|
|
BLUETOOTH_PERMISSION_CODE
|
|
|
|
|
|
|
|
)
|
|
|
|
ipLayout.visibility = View.GONE
|
|
|
|
ipLayout.visibility = View.GONE
|
|
|
|
portLayout.visibility = View.GONE
|
|
|
|
portLayout.visibility = View.GONE
|
|
|
|
selectedDevice.visibility = View.VISIBLE
|
|
|
|
selectedDevice.visibility = View.VISIBLE
|
|
|
@ -101,12 +108,15 @@ class MainActivity : AppCompatActivity() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// select bluetooth device
|
|
|
|
selectedDevice.setOnClickListener {
|
|
|
|
selectedDevice.setOnClickListener {
|
|
|
|
|
|
|
|
// request bluetooth permissions
|
|
|
|
checkPermission("android.permission.BLUETOOTH_CONNECT", BLUETOOTH_PERMISSION_CODE)
|
|
|
|
checkPermission("android.permission.BLUETOOTH_CONNECT", BLUETOOTH_PERMISSION_CODE)
|
|
|
|
checkPermission("android.permission.BLUETOOTH_SCAN", BLUETOOTH_SCAN_CODE)
|
|
|
|
checkPermission("android.permission.BLUETOOTH_SCAN", BLUETOOTH_SCAN_CODE)
|
|
|
|
|
|
|
|
if (btConnectPermission && btScanPermission) {
|
|
|
|
val serverIntent = Intent(this, DeviceListActivity::class.java)
|
|
|
|
val serverIntent = Intent(this, DeviceListActivity::class.java)
|
|
|
|
resultLauncher.launch(serverIntent)
|
|
|
|
resultLauncher.launch(serverIntent)
|
|
|
|
// startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -116,6 +126,7 @@ class MainActivity : AppCompatActivity() {
|
|
|
|
portLayout.visibility = View.VISIBLE
|
|
|
|
portLayout.visibility = View.VISIBLE
|
|
|
|
selectedDevice.visibility = View.GONE
|
|
|
|
selectedDevice.visibility = View.GONE
|
|
|
|
} else if (tabLayout.selectedTabPosition == 1) {
|
|
|
|
} else if (tabLayout.selectedTabPosition == 1) {
|
|
|
|
|
|
|
|
println("android.permission.BLUETOOTH_CONNECT")
|
|
|
|
checkPermission("android.permission.BLUETOOTH_CONNECT", BLUETOOTH_PERMISSION_CODE)
|
|
|
|
checkPermission("android.permission.BLUETOOTH_CONNECT", BLUETOOTH_PERMISSION_CODE)
|
|
|
|
ipLayout.visibility = View.GONE
|
|
|
|
ipLayout.visibility = View.GONE
|
|
|
|
portLayout.visibility = View.GONE
|
|
|
|
portLayout.visibility = View.GONE
|
|
|
@ -188,6 +199,7 @@ class MainActivity : AppCompatActivity() {
|
|
|
|
|
|
|
|
|
|
|
|
// Function to check and request permission.
|
|
|
|
// Function to check and request permission.
|
|
|
|
private fun checkPermission(permission: String, requestCode: Int) {
|
|
|
|
private fun checkPermission(permission: String, requestCode: Int) {
|
|
|
|
|
|
|
|
println(permission)
|
|
|
|
if (ActivityCompat.checkSelfPermission(
|
|
|
|
if (ActivityCompat.checkSelfPermission(
|
|
|
|
this@MainActivity,
|
|
|
|
this@MainActivity,
|
|
|
|
permission
|
|
|
|
permission
|
|
|
@ -196,8 +208,12 @@ class MainActivity : AppCompatActivity() {
|
|
|
|
// Requesting the permission
|
|
|
|
// Requesting the permission
|
|
|
|
ActivityCompat.requestPermissions(this@MainActivity, arrayOf(permission), requestCode)
|
|
|
|
ActivityCompat.requestPermissions(this@MainActivity, arrayOf(permission), requestCode)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// Toast.makeText(this@MainActivity, "Permission already granted", Toast.LENGTH_SHORT)
|
|
|
|
// Permission already granted
|
|
|
|
// .show()
|
|
|
|
if (requestCode == BLUETOOTH_PERMISSION_CODE) {
|
|
|
|
|
|
|
|
btConnectPermission = true
|
|
|
|
|
|
|
|
} else if (requestCode == BLUETOOTH_SCAN_CODE) {
|
|
|
|
|
|
|
|
btScanPermission = true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -212,17 +228,12 @@ class MainActivity : AppCompatActivity() {
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
|
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
|
|
|
if (requestCode == BLUETOOTH_PERMISSION_CODE) {
|
|
|
|
if (requestCode == BLUETOOTH_PERMISSION_CODE) {
|
|
|
|
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
btConnectPermission =
|
|
|
|
Toast.makeText(
|
|
|
|
grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED
|
|
|
|
this@MainActivity,
|
|
|
|
} else if (requestCode == BLUETOOTH_SCAN_CODE) {
|
|
|
|
"Bluetooth Permission Granted",
|
|
|
|
println("scan permission")
|
|
|
|
Toast.LENGTH_SHORT
|
|
|
|
btScanPermission =
|
|
|
|
)
|
|
|
|
grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED
|
|
|
|
.show()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Toast.makeText(this@MainActivity, "Bluetooth Permission Denied", Toast.LENGTH_SHORT)
|
|
|
|
|
|
|
|
.show()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -236,7 +247,7 @@ class MainActivity : AppCompatActivity() {
|
|
|
|
val prefs = getSharedPreferences(
|
|
|
|
val prefs = getSharedPreferences(
|
|
|
|
SHARED_PREFS_NAME,
|
|
|
|
SHARED_PREFS_NAME,
|
|
|
|
MODE_PRIVATE
|
|
|
|
MODE_PRIVATE
|
|
|
|
) // get the parameters from the Shared
|
|
|
|
)
|
|
|
|
// read values from the shared preferences
|
|
|
|
// read values from the shared preferences
|
|
|
|
ipInput.setText(prefs.getString(SHARED_PREFS_IP, "192.168.1.127"))
|
|
|
|
ipInput.setText(prefs.getString(SHARED_PREFS_IP, "192.168.1.127"))
|
|
|
|
portInput.setText(prefs.getString(SHARED_PREFS_PORT, "1234"))
|
|
|
|
portInput.setText(prefs.getString(SHARED_PREFS_PORT, "1234"))
|
|
|
|