update
This commit is contained in:
parent
c115d2ccce
commit
7ad3823f9b
@ -68,7 +68,6 @@ class DeviceListActivity : Activity() {
|
||||
@SuppressLint("MissingPermission")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
// Setup the window
|
||||
setContentView(R.layout.activity_device_list)
|
||||
|
||||
@ -77,14 +76,6 @@ class DeviceListActivity : Activity() {
|
||||
// Set result CANCELED in case the user backs out
|
||||
setResult(RESULT_CANCELED)
|
||||
|
||||
// Initialize array adapters. One for already paired devices and
|
||||
// one for newly discovered devices
|
||||
val pairedDevicesArrayAdapter = ArrayAdapter<String>(this, R.layout.device_list)
|
||||
// val bluetoothAdapter = BluetoothAdapter(this, R.layout.device_list, )
|
||||
|
||||
|
||||
|
||||
|
||||
// Get the local Bluetooth adapter
|
||||
bluetoothManager =
|
||||
mContext!!.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
|
||||
@ -97,22 +88,6 @@ class DeviceListActivity : Activity() {
|
||||
|
||||
val bluetoothAdapter = BluetoothAdapter(this, deviceList)
|
||||
|
||||
// // If there are paired devices, add each one to the ArrayAdapter
|
||||
// if (pairedDevices.isNotEmpty()) {
|
||||
// for (device in pairedDevices) {
|
||||
//
|
||||
// pairedDevicesArrayAdapter.add(
|
||||
// """
|
||||
// ${device.name}
|
||||
// ${device.address}
|
||||
// """.trimIndent()
|
||||
// )
|
||||
// }
|
||||
// } else {
|
||||
// val noDevices = "No device"
|
||||
// pairedDevicesArrayAdapter.add(noDevices)
|
||||
// }
|
||||
|
||||
// Find and set up the ListView for paired devices
|
||||
val pairedListView: ListView = findViewById<ListView>(R.id.paired_devices)
|
||||
pairedListView.adapter = bluetoothAdapter
|
||||
|
@ -1,37 +1,30 @@
|
||||
package com.rookiedev.hexapod
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.InetAddresses.isNumericAddress
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.text.method.LinkMovementMethod
|
||||
import android.view.View
|
||||
import android.widget.AdapterView.OnItemClickListener
|
||||
import android.widget.Button
|
||||
import android.widget.ListView
|
||||
import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.constraintlayout.widget.ConstraintLayout
|
||||
import androidx.core.app.ActivityCompat
|
||||
import com.google.android.material.tabs.TabLayout
|
||||
import com.google.android.material.textfield.TextInputEditText
|
||||
import com.google.android.material.textfield.TextInputLayout
|
||||
import org.w3c.dom.Text
|
||||
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
companion object {
|
||||
private const val BLUETOOTH_PERMISSION_CODE = 100
|
||||
private const val INTERNET_PERMISSION_CODE = 101
|
||||
}
|
||||
|
||||
private val REQUEST_CONNECT_DEVICE_SECURE = 1
|
||||
@ -41,6 +34,9 @@ class MainActivity : AppCompatActivity() {
|
||||
private val SHAREDPREFSNAME = "com.rookiedev.hexapod_preferences"
|
||||
private val SHAREDPREFSIP = "IP"
|
||||
private val SHAREDPREFSPORT = "PORT"
|
||||
private val SHARED_PREFS_TAB = "TAB"
|
||||
private val SHARED_PREFS_DEVICE_NAME = "DEVICE_NAME"
|
||||
private val SHARED_PREFS_DEVICE_ADDRESS = "DEVICE_ADDRESS"
|
||||
|
||||
private var mContext: Context? = null
|
||||
|
||||
@ -50,6 +46,8 @@ class MainActivity : AppCompatActivity() {
|
||||
private lateinit var deviceName: TextView
|
||||
private lateinit var deviceAddress: TextView
|
||||
|
||||
private lateinit var tabLayout: TabLayout
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
@ -63,29 +61,25 @@ class MainActivity : AppCompatActivity() {
|
||||
val ipLayout = findViewById<TextInputLayout>(R.id.ip_input_layout)
|
||||
val portLayout = findViewById<TextInputLayout>(R.id.port_input_layout)
|
||||
|
||||
val deviceList = findViewById<ListView>(R.id.paired_devices)
|
||||
val selectedDevice = findViewById<ConstraintLayout>(R.id.selected)
|
||||
deviceName = findViewById<TextView>(R.id.textView_device_name)
|
||||
deviceAddress = findViewById<TextView>(R.id.textView_device_address)
|
||||
deviceName = findViewById(R.id.textView_device_name)
|
||||
deviceAddress = findViewById(R.id.textView_device_address)
|
||||
|
||||
val sourceLink = findViewById<TextView>(R.id.textView_github)
|
||||
sourceLink.movementMethod = LinkMovementMethod.getInstance()
|
||||
|
||||
tabLayout = findViewById(R.id.tab)
|
||||
|
||||
val tabLayout = findViewById<TabLayout>(R.id.tab)
|
||||
tabLayout.addOnTabSelectedListener(
|
||||
object : TabLayout.OnTabSelectedListener {
|
||||
@RequiresApi(Build.VERSION_CODES.S)
|
||||
override fun onTabSelected(tab: TabLayout.Tab?) {
|
||||
if (tab!!.text == "WiFi") {
|
||||
ipLayout.visibility = View.VISIBLE
|
||||
portLayout.visibility = View.VISIBLE
|
||||
deviceList.visibility = View.GONE
|
||||
selectedDevice.visibility = View.GONE
|
||||
} else if (tab.text == "Bluetooth") {
|
||||
ipLayout.visibility = View.GONE
|
||||
portLayout.visibility = View.GONE
|
||||
deviceList.visibility = View.VISIBLE
|
||||
selectedDevice.visibility = View.VISIBLE
|
||||
}
|
||||
}
|
||||
@ -99,7 +93,7 @@ class MainActivity : AppCompatActivity() {
|
||||
)
|
||||
|
||||
selectedDevice.setOnClickListener {
|
||||
checkPermission(Manifest.permission.BLUETOOTH_CONNECT, BLUETOOTH_PERMISSION_CODE)
|
||||
checkPermission("android.permission.BLUETOOTH_CONNECT", BLUETOOTH_PERMISSION_CODE)
|
||||
val serverIntent = Intent(this, DeviceListActivity::class.java)
|
||||
resultLauncher.launch(serverIntent)
|
||||
// startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE)
|
||||
@ -107,6 +101,17 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
readSharedPref()
|
||||
|
||||
if (tabLayout.selectedTabPosition == 0){
|
||||
ipLayout.visibility = View.VISIBLE
|
||||
portLayout.visibility = View.VISIBLE
|
||||
selectedDevice.visibility = View.GONE
|
||||
} else if (tabLayout.selectedTabPosition == 1){
|
||||
checkPermission("android.permission.BLUETOOTH_CONNECT", BLUETOOTH_PERMISSION_CODE)
|
||||
ipLayout.visibility = View.GONE
|
||||
portLayout.visibility = View.GONE
|
||||
selectedDevice.visibility = View.VISIBLE
|
||||
}
|
||||
|
||||
buttonConnect.setOnClickListener {
|
||||
// your code to perform when the user clicks on the button
|
||||
|
||||
@ -147,7 +152,7 @@ class MainActivity : AppCompatActivity() {
|
||||
})
|
||||
}
|
||||
|
||||
var resultLauncher =
|
||||
private var resultLauncher =
|
||||
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
// There are no request codes
|
||||
@ -166,12 +171,12 @@ class MainActivity : AppCompatActivity() {
|
||||
permission
|
||||
) == PackageManager.PERMISSION_DENIED
|
||||
) {
|
||||
|
||||
// Requesting the permission
|
||||
ActivityCompat.requestPermissions(this@MainActivity, arrayOf(permission), requestCode)
|
||||
} else {
|
||||
Toast.makeText(this@MainActivity, "Permission already granted", Toast.LENGTH_SHORT)
|
||||
.show()
|
||||
// Toast.makeText(this@MainActivity, "Permission already granted", Toast.LENGTH_SHORT)
|
||||
// .show()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -186,23 +191,20 @@ class MainActivity : AppCompatActivity() {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||||
if (requestCode == BLUETOOTH_PERMISSION_CODE) {
|
||||
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
Toast.makeText(this@MainActivity, "Camera Permission Granted", Toast.LENGTH_SHORT)
|
||||
Toast.makeText(this@MainActivity, "Bluetooth Permission Granted", Toast.LENGTH_SHORT)
|
||||
.show()
|
||||
} else {
|
||||
Toast.makeText(this@MainActivity, "Camera Permission Denied", Toast.LENGTH_SHORT)
|
||||
.show()
|
||||
}
|
||||
} else if (requestCode == INTERNET_PERMISSION_CODE) {
|
||||
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
Toast.makeText(this@MainActivity, "Storage Permission Granted", Toast.LENGTH_SHORT)
|
||||
.show()
|
||||
} else {
|
||||
Toast.makeText(this@MainActivity, "Storage Permission Denied", Toast.LENGTH_SHORT)
|
||||
Toast.makeText(this@MainActivity, "Bluetooth Permission Denied", Toast.LENGTH_SHORT)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
saveSharedPref()
|
||||
}
|
||||
|
||||
|
||||
private fun readSharedPref() {
|
||||
val prefs = getSharedPreferences(
|
||||
@ -212,6 +214,18 @@ class MainActivity : AppCompatActivity() {
|
||||
// read values from the shared preferences
|
||||
ipInput.setText(prefs.getString(SHAREDPREFSIP, "192.168.1.127"))
|
||||
portInput.setText(prefs.getString(SHAREDPREFSPORT, "1234"))
|
||||
|
||||
val selectedTab = prefs.getString(SHARED_PREFS_TAB, "WiFi")
|
||||
if (selectedTab == "WiFi"){
|
||||
val tab = tabLayout.getTabAt(0)
|
||||
tab!!.select()
|
||||
} else if (selectedTab == "Bluetooth") {
|
||||
val tab = tabLayout.getTabAt(1)
|
||||
tab!!.select()
|
||||
}
|
||||
|
||||
deviceName.text = prefs.getString(SHARED_PREFS_DEVICE_NAME, "Click to select a device")
|
||||
deviceAddress.text = prefs.getString(SHARED_PREFS_DEVICE_ADDRESS, "")
|
||||
}
|
||||
|
||||
private fun saveSharedPref() {
|
||||
@ -222,6 +236,15 @@ class MainActivity : AppCompatActivity() {
|
||||
val editor = prefs.edit()
|
||||
editor.putString(SHAREDPREFSIP, ipInput.text.toString())
|
||||
editor.putString(SHAREDPREFSPORT, portInput.text.toString())
|
||||
if (tabLayout.selectedTabPosition == 0){
|
||||
editor.putString(SHARED_PREFS_TAB, "WiFi")
|
||||
} else if (tabLayout.selectedTabPosition == 1){
|
||||
editor.putString(SHARED_PREFS_TAB, "Bluetooth")
|
||||
}
|
||||
|
||||
editor.putString(SHARED_PREFS_DEVICE_NAME, deviceName.text.toString())
|
||||
editor.putString(SHARED_PREFS_DEVICE_ADDRESS, deviceAddress.text.toString())
|
||||
|
||||
editor.apply()
|
||||
}
|
||||
}
|
@ -37,11 +37,13 @@
|
||||
app:layout_constraintTop_toBottomOf="@+id/hexapod_logo">
|
||||
|
||||
<com.google.android.material.tabs.TabItem
|
||||
android:id="@+id/wifi_tab"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="WiFi" />
|
||||
|
||||
<com.google.android.material.tabs.TabItem
|
||||
android:id="@+id/bluetooth_tab"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Bluetooth" />
|
||||
@ -92,19 +94,6 @@
|
||||
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
<ListView
|
||||
android:id="@+id/paired_devices"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="fill_parent"
|
||||
android:layout_weight="1"
|
||||
android:stackFromBottom="true"
|
||||
android:orientation="vertical"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/port_input_layout"
|
||||
app:layout_constraintBottom_toTopOf="@id/selected"
|
||||
android:visibility="visible"/>
|
||||
|
||||
<include
|
||||
android:id="@+id/selected"
|
||||
android:visibility="visible"
|
||||
|
@ -1,18 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Copyright (C) 2014 The Android Open Source Project
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="5dp"
|
||||
android:textSize="18sp"
|
||||
/>
|
@ -6,7 +6,9 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:background="?android:attr/selectableItemBackground">
|
||||
android:background="?android:attr/selectableItemBackground"
|
||||
android:paddingTop="24dp"
|
||||
android:paddingBottom="24dp">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/imageView"
|
||||
|
Loading…
x
Reference in New Issue
Block a user