master
Zhengyu Peng 3 years ago
parent 0a7338fc16
commit 756fee013c

@ -5,6 +5,7 @@
<map> <map>
<entry key="..\:/Users/rooki/Documents/GitHub/hexapod/software/android/app/src/main/res/layout/activity_control.xml" value="0.32472826086956524" /> <entry key="..\:/Users/rooki/Documents/GitHub/hexapod/software/android/app/src/main/res/layout/activity_control.xml" value="0.32472826086956524" />
<entry key="..\:/Users/rooki/Documents/GitHub/hexapod/software/android/app/src/main/res/layout/activity_main.xml" value="0.2717391304347826" /> <entry key="..\:/Users/rooki/Documents/GitHub/hexapod/software/android/app/src/main/res/layout/activity_main.xml" value="0.2717391304347826" />
<entry key="..\:/Users/rooki/Documents/GitHub/hexapod/software/android/app/src/main/res/layout/device_name.xml" value="0.2826086956521739" />
</map> </map>
</option> </option>
</component> </component>

@ -1,35 +1,48 @@
package com.rookiedev.hexapod package com.rookiedev.hexapod
import android.Manifest
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager
import android.net.InetAddresses.isNumericAddress import android.net.InetAddresses.isNumericAddress
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.text.Editable import android.text.Editable
import android.text.TextWatcher import android.text.TextWatcher
import android.text.method.LinkMovementMethod import android.text.method.LinkMovementMethod
import android.widget.Button import android.view.View
import android.widget.TextView import android.widget.*
import android.widget.AdapterView.OnItemClickListener
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
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.TextInputEditText
import com.google.android.material.textfield.TextInputLayout import com.google.android.material.textfield.TextInputLayout
import java.lang.String
import kotlin.CharSequence
import kotlin.Int
import kotlin.apply
import kotlin.toString
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
companion object {
private const val BLUETOOTH_PERMISSION_CODE = 100
private const val INTERNET_PERMISSION_CODE = 101
}
private val SHAREDPREFSNAME = "com.rookiedev.hexapod_preferences" private val SHAREDPREFSNAME = "com.rookiedev.hexapod_preferences"
private val SHAREDPREFSIP = "IP" private val SHAREDPREFSIP = "IP"
private val SHAREDPREFSPORT = "PORT" private val SHAREDPREFSPORT = "PORT"
private lateinit var ipInput:TextInputEditText private var mContext: Context?=null
private lateinit var portInput:TextInputEditText
private lateinit var ipInput: TextInputEditText
private lateinit var portInput: TextInputEditText
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
ipInput = findViewById(R.id.ip_input) ipInput = findViewById(R.id.ip_input)
portInput = findViewById(R.id.port_input) portInput = findViewById(R.id.port_input)
val buttonConnect = findViewById<Button>(R.id.button_connect) val buttonConnect = findViewById<Button>(R.id.button_connect)
@ -37,9 +50,98 @@ class MainActivity : AppCompatActivity() {
val ipLayout = findViewById<TextInputLayout>(R.id.ip_input_layout) val ipLayout = findViewById<TextInputLayout>(R.id.ip_input_layout)
val portLayout = findViewById<TextInputLayout>(R.id.port_input_layout) val portLayout = findViewById<TextInputLayout>(R.id.port_input_layout)
val deviceList = findViewById<ListView>(R.id.paired_devices)
val sourceLink = findViewById<TextView>(R.id.textView_github) val sourceLink = findViewById<TextView>(R.id.textView_github)
sourceLink.movementMethod = LinkMovementMethod.getInstance() sourceLink.movementMethod = LinkMovementMethod.getInstance()
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
} else if (tab.text == "Bluetooth") {
checkPermission(Manifest.permission.BLUETOOTH_CONNECT, BLUETOOTH_PERMISSION_CODE)
if (ActivityCompat.checkSelfPermission(
mContext!!,
Manifest.permission.BLUETOOTH_CONNECT
) != PackageManager.PERMISSION_GRANTED
) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return
}
ipLayout.visibility = View.GONE
portLayout.visibility = View.GONE
deviceList.visibility = View.VISIBLE
// Initialize array adapters. One for already paired devices and
// one for newly discovered devices
// Initialize array adapters. One for already paired devices and
// one for newly discovered devices
val pairedDevicesArrayAdapter =
ArrayAdapter<kotlin.String>(mContext!!, R.layout.device_name)
// Find and set up the ListView for paired devices
// Find and set up the ListView for paired devices
val pairedListView = findViewById<ListView>(R.id.paired_devices)
pairedListView.adapter = pairedDevicesArrayAdapter
pairedListView.onItemClickListener = mDeviceClickListener
// Get the local Bluetooth adapter
// Get the local Bluetooth adapter
// mBtAdapter = BluetoothAdapter.getDefaultAdapter()
val bluetoothManager = mContext!!.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bluetoothManager.adapter
// Get a set of currently paired devices
// Get a set of currently paired devices
val pairedDevices: Set<BluetoothDevice> = bluetoothManager.adapter.bondedDevices
// If there are paired devices, add each one to the ArrayAdapter
// If there are paired devices, add each one to the ArrayAdapter
if (pairedDevices.isNotEmpty()) {
// findViewById<View>(R.id.title_paired_devices).visibility = View.VISIBLE
for (device in pairedDevices) {
pairedDevicesArrayAdapter.add(
"""
${device.name}
${device.address}
""".trimIndent()
)
}
pairedListView.layoutParams.height = 153*6
}
// else {
// val noDevices = resources.getText(R.string.none_paired).toString()
// pairedDevicesArrayAdapter.add(noDevices)
// }
}
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
}
override fun onTabReselected(tab: TabLayout.Tab?) {
}
}
)
readSharedPref() readSharedPref()
buttonConnect.setOnClickListener { buttonConnect.setOnClickListener {
@ -82,6 +184,59 @@ class MainActivity : AppCompatActivity() {
}) })
} }
// Function to check and request permission.
private fun checkPermission(permission: String, requestCode: Int) {
if (ActivityCompat.checkSelfPermission(this@MainActivity, 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()
}
}
// This function is called when the user accepts or decline the permission.
// Request Code is used to check which permission called this function.
// This request code is provided when the user is prompt for permission.
override fun onRequestPermissionsResult(requestCode: Int,
permissions: Array<String>,
grantResults: IntArray) {
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).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).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(
SHAREDPREFSNAME, SHAREDPREFSNAME,

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M17.71,7.71L12,2h-1v7.59L6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 11,14.41L11,22h1l5.71,-5.71 -4.3,-4.29 4.3,-4.29zM13,5.83l1.88,1.88L13,9.59L13,5.83zM14.88,16.29L13,18.17v-3.76l1.88,1.88z"/>
</vector>

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M3,18h18v-2L3,16v2zM3,13h18v-2L3,11v2zM3,6v2h18L21,6L3,6z"/>
</vector>

@ -55,7 +55,8 @@
app:layout_constraintBottom_toTopOf="@id/port_input_layout" app:layout_constraintBottom_toTopOf="@id/port_input_layout"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tab"> app:layout_constraintTop_toBottomOf="@+id/tab"
android:visibility="visible">
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputEditText
android:id="@+id/ip_input" android:id="@+id/ip_input"
@ -73,10 +74,11 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="4dp" android:layout_marginBottom="4dp"
app:errorEnabled="true" app:errorEnabled="true"
app:layout_constraintBottom_toTopOf="@id/button_connect" app:layout_constraintBottom_toTopOf="@id/paired_devices"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/ip_input_layout"> app:layout_constraintTop_toBottomOf="@id/ip_input_layout"
android:visibility="visible">
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputEditText
android:id="@+id/port_input" android:id="@+id/port_input"
@ -88,6 +90,26 @@
</com.google.android.material.textfield.TextInputLayout> </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/button_connect"
android:visibility="visible"/>
<!-- <include-->
<!-- layout="@layout/device_name"-->
<!-- app:layout_constraintEnd_toEndOf="parent"-->
<!-- app:layout_constraintHorizontal_bias="1.0"-->
<!-- app:layout_constraintStart_toStartOf="parent"-->
<!-- app:layout_constraintTop_toBottomOf="@+id/paired_devices" />-->
<Button <Button
android:id="@+id/button_connect" android:id="@+id/button_connect"
android:layout_width="0dp" android:layout_width="0dp"
@ -98,7 +120,7 @@
android:text="Connect" android:text="Connect"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/port_input_layout" /> app:layout_constraintTop_toBottomOf="@id/paired_devices" />
<TextView <TextView
android:id="@+id/textView_github" android:id="@+id/textView_github"

@ -0,0 +1,18 @@
<?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"
/>

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@+id/textView"
app:srcCompat="@drawable/ic_baseline_bluetooth_24" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceHeadline6"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceBody2"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_baseline_menu_24" />
</androidx.constraintlayout.widget.ConstraintLayout>
Loading…
Cancel
Save