update
This commit is contained in:
parent
f7a3c0ec27
commit
eceac6ca6d
@ -126,14 +126,8 @@ class DeviceListActivity : Activity() {
|
|||||||
private val mDeviceClickListener =
|
private val mDeviceClickListener =
|
||||||
OnItemClickListener { av, v, arg2, arg3 ->
|
OnItemClickListener { av, v, arg2, arg3 ->
|
||||||
|
|
||||||
|
|
||||||
val device: BluetoothDevice = av.getItemAtPosition(arg2) as BluetoothDevice
|
val device: BluetoothDevice = av.getItemAtPosition(arg2) as BluetoothDevice
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
println(device.name)
|
|
||||||
println(device.address)
|
|
||||||
|
|
||||||
// Create the result Intent and include the MAC address
|
// Create the result Intent and include the MAC address
|
||||||
val intent = Intent()
|
val intent = Intent()
|
||||||
intent.putExtra(EXTRA_DEVICE_ADDRESS, device.address)
|
intent.putExtra(EXTRA_DEVICE_ADDRESS, device.address)
|
||||||
@ -142,7 +136,6 @@ class DeviceListActivity : Activity() {
|
|||||||
// Set result and finish this Activity
|
// Set result and finish this Activity
|
||||||
setResult(RESULT_OK, intent)
|
setResult(RESULT_OK, intent)
|
||||||
|
|
||||||
println("selected")
|
|
||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@ import androidx.core.app.ActivityCompat
|
|||||||
import com.google.android.material.tabs.TabLayout
|
import com.google.android.material.tabs.TabLayout
|
||||||
import com.google.android.material.textfield.TextInputEditText
|
import com.google.android.material.textfield.TextInputEditText
|
||||||
import com.google.android.material.textfield.TextInputLayout
|
import com.google.android.material.textfield.TextInputLayout
|
||||||
|
import org.w3c.dom.Text
|
||||||
|
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
@ -41,11 +42,14 @@ class MainActivity : AppCompatActivity() {
|
|||||||
private val SHAREDPREFSIP = "IP"
|
private val SHAREDPREFSIP = "IP"
|
||||||
private val SHAREDPREFSPORT = "PORT"
|
private val SHAREDPREFSPORT = "PORT"
|
||||||
|
|
||||||
private var mContext: Context?=null
|
private var mContext: Context? = null
|
||||||
|
|
||||||
private lateinit var ipInput: TextInputEditText
|
private lateinit var ipInput: TextInputEditText
|
||||||
private lateinit var portInput: TextInputEditText
|
private lateinit var portInput: TextInputEditText
|
||||||
|
|
||||||
|
private lateinit var deviceName: TextView
|
||||||
|
private lateinit var deviceAddress: TextView
|
||||||
|
|
||||||
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)
|
||||||
@ -61,8 +65,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
val deviceList = findViewById<ListView>(R.id.paired_devices)
|
val deviceList = findViewById<ListView>(R.id.paired_devices)
|
||||||
val selectedDevice = findViewById<ConstraintLayout>(R.id.selected)
|
val selectedDevice = findViewById<ConstraintLayout>(R.id.selected)
|
||||||
val deviceName = findViewById<TextView>(R.id.textView_device_name)
|
deviceName = findViewById<TextView>(R.id.textView_device_name)
|
||||||
val deviceAddress = findViewById<TextView>(R.id.textView_device_address)
|
deviceAddress = findViewById<TextView>(R.id.textView_device_address)
|
||||||
|
|
||||||
val sourceLink = findViewById<TextView>(R.id.textView_github)
|
val sourceLink = findViewById<TextView>(R.id.textView_github)
|
||||||
sourceLink.movementMethod = LinkMovementMethod.getInstance()
|
sourceLink.movementMethod = LinkMovementMethod.getInstance()
|
||||||
@ -94,7 +98,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
selectedDevice.setOnClickListener{
|
selectedDevice.setOnClickListener {
|
||||||
checkPermission(Manifest.permission.BLUETOOTH_CONNECT, BLUETOOTH_PERMISSION_CODE)
|
checkPermission(Manifest.permission.BLUETOOTH_CONNECT, BLUETOOTH_PERMISSION_CODE)
|
||||||
val serverIntent = Intent(this, DeviceListActivity::class.java)
|
val serverIntent = Intent(this, DeviceListActivity::class.java)
|
||||||
resultLauncher.launch(serverIntent)
|
resultLauncher.launch(serverIntent)
|
||||||
@ -143,66 +147,62 @@ class MainActivity : AppCompatActivity() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
var resultLauncher =
|
||||||
if (result.resultCode == Activity.RESULT_OK) {
|
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||||
// There are no request codes
|
if (result.resultCode == Activity.RESULT_OK) {
|
||||||
val data: Intent? = result.data
|
// There are no request codes
|
||||||
// doSomeOperations()
|
val data: Intent? = result.data
|
||||||
|
|
||||||
|
deviceName.text = data!!.getStringExtra("device_name")
|
||||||
|
deviceAddress.text = data.getStringExtra("device_address")
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 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) {
|
||||||
if (ActivityCompat.checkSelfPermission(this@MainActivity, permission) == PackageManager.PERMISSION_DENIED) {
|
if (ActivityCompat.checkSelfPermission(
|
||||||
|
this@MainActivity,
|
||||||
|
permission
|
||||||
|
) == PackageManager.PERMISSION_DENIED
|
||||||
|
) {
|
||||||
|
|
||||||
// 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).show()
|
Toast.makeText(this@MainActivity, "Permission already granted", Toast.LENGTH_SHORT)
|
||||||
|
.show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function is called when the user accepts or decline the permission.
|
// This function is called when the user accepts or decline the permission.
|
||||||
// Request Code is used to check which permission called this function.
|
// Request Code is used to check which permission called this function.
|
||||||
// This request code is provided when the user is prompt for permission.
|
// This request code is provided when the user is prompt for permission.
|
||||||
override fun onRequestPermissionsResult(requestCode: Int,
|
override fun onRequestPermissionsResult(
|
||||||
permissions: Array<String>,
|
requestCode: Int,
|
||||||
grantResults: IntArray) {
|
permissions: Array<String>,
|
||||||
|
grantResults: IntArray
|
||||||
|
) {
|
||||||
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) {
|
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
Toast.makeText(this@MainActivity, "Camera Permission Granted", Toast.LENGTH_SHORT).show()
|
Toast.makeText(this@MainActivity, "Camera Permission Granted", Toast.LENGTH_SHORT)
|
||||||
|
.show()
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(this@MainActivity, "Camera Permission Denied", Toast.LENGTH_SHORT).show()
|
Toast.makeText(this@MainActivity, "Camera Permission Denied", Toast.LENGTH_SHORT)
|
||||||
|
.show()
|
||||||
}
|
}
|
||||||
} else if (requestCode == INTERNET_PERMISSION_CODE) {
|
} else if (requestCode == INTERNET_PERMISSION_CODE) {
|
||||||
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
Toast.makeText(this@MainActivity, "Storage Permission Granted", Toast.LENGTH_SHORT).show()
|
Toast.makeText(this@MainActivity, "Storage Permission Granted", Toast.LENGTH_SHORT)
|
||||||
|
.show()
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(this@MainActivity, "Storage Permission Denied", Toast.LENGTH_SHORT).show()
|
Toast.makeText(this@MainActivity, "Storage Permission Denied", Toast.LENGTH_SHORT)
|
||||||
|
.show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The on-click listener for all devices in the ListViews
|
|
||||||
*/
|
|
||||||
private val mDeviceClickListener =
|
|
||||||
OnItemClickListener { av, v, arg2, arg3 -> // Cancel discovery because it's costly and we're about to connect
|
|
||||||
// mBtAdapter.cancelDiscovery()
|
|
||||||
//
|
|
||||||
// // Get the device MAC address, which is the last 17 chars in the View
|
|
||||||
// val info = (v as TextView).text.toString()
|
|
||||||
// val address = info.substring(info.length - 17)
|
|
||||||
//
|
|
||||||
// // Create the result Intent and include the MAC address
|
|
||||||
// val intent = Intent()
|
|
||||||
// intent.putExtra(EXTRA_DEVICE_ADDRESS, address)
|
|
||||||
//
|
|
||||||
// // Set result and finish this Activity
|
|
||||||
// setResult(RESULT_OK, intent)
|
|
||||||
// finish()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun readSharedPref() {
|
private fun readSharedPref() {
|
||||||
val prefs = getSharedPreferences(
|
val prefs = getSharedPreferences(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user