Skip to content

Commit

Permalink
Backup all files - Forgot what changes I made
Browse files Browse the repository at this point in the history
  • Loading branch information
zakuArbor committed Sep 11, 2021
1 parent c422769 commit df192fb
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 54 deletions.
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.proxyauth">

<uses-permission android:name="android.permission.BLUETOOTH" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
68 changes: 34 additions & 34 deletions android/app/src/main/java/com/example/proxyauth/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
package com.example.proxyauth

import android.os.Bundle
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.proxyauth.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

val navView: BottomNavigationView = binding.navView

val navController = findNavController(R.id.nav_host_fragment_activity_main)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
package com.example.proxyauth

import android.os.Bundle
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.proxyauth.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

val navView: BottomNavigationView = binding.navView

val navController = findNavController(R.id.nav_host_fragment_activity_main)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,59 @@
package com.example.proxyauth.ui.home

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.TextView
import android.widget.*
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.example.proxyauth.R
import com.example.proxyauth.databinding.FragmentHomeBinding
import java.lang.reflect.Method

class HomeFragment : Fragment() {

private lateinit var homeViewModel: HomeViewModel
private var _binding: FragmentHomeBinding? = null

private var bluetoothAdapter: BluetoothAdapter? = null

var devAdapter : ArrayAdapter<String>? = null
private val deviceList : ArrayList<String> = ArrayList<String>()
private val deviceNames : ArrayList<String> = ArrayList<String>()
private var deviceList :ArrayList<BluetoothDevice> = ArrayList<BluetoothDevice>()
private val REQUEST_ENABLE_BT: Int = 3

// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()

if (bluetoothAdapter == null) {
activity?.let {
Toast.makeText(it, "Bluetooth is not available", Toast.LENGTH_LONG).show()
}
}
}

override fun onStart() {
super.onStart()

if (!bluetoothAdapter?.isEnabled!!) {
var enableIntent: Intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableIntent, REQUEST_ENABLE_BT)
}
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -43,18 +71,41 @@ class HomeFragment : Fragment() {
ArrayAdapter<String>(
it,
android.R.layout.simple_list_item_1,
deviceList
deviceNames
)
}
devListView.adapter = devAdapter
devListView.onItemClickListener = AdapterView.OnItemClickListener{_, _, pos, _ ->
activity?.let {
//device.address
//device.name
val is_connected: Boolean = isConnected(deviceList[pos])
Toast.makeText(it, deviceList[pos].name + " " + is_connected.toString(), Toast.LENGTH_LONG).show()
}
}
updateDevList();

var buttonView: Button = binding.refreshBtn
buttonView.setOnClickListener{updateDevList()}

return root
}

fun updateDevList() {
deviceList.clear()
deviceList.addAll(homeViewModel.fetchDeviceList())
fun isConnected(device: BluetoothDevice): Boolean {
return try {
val m: Method = device.javaClass.getMethod("isConnected")
m.invoke(device) as Boolean
} catch (e: Exception) {
throw IllegalStateException(e)
}
}

private fun updateDevList() {
deviceNames.clear()
deviceList = homeViewModel.fetchDeviceList(bluetoothAdapter);
for (device: BluetoothDevice in deviceList) {
deviceNames.add(device.name)
}
devAdapter?.notifyDataSetChanged()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package com.example.proxyauth.ui.home

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel


class HomeViewModel : ViewModel() {

private val _text = MutableLiveData<String>().apply {
value = "This is home Fragment"
}
private val trustedDevices : ArrayList<String> = ArrayList ()
/*
* Fetch all the devices that are trusted and connected
*/
fun fetchDeviceList() : ArrayList<String> {
trustedDevices.add("device 1")
trustedDevices.add("device 2")
Log.i("test", trustedDevices.toString())
return trustedDevices
fun fetchDeviceList(bluetoothAdapter: BluetoothAdapter?) : ArrayList<BluetoothDevice> {
var list: ArrayList<BluetoothDevice> = ArrayList<BluetoothDevice>()
list.addAll(bluetoothAdapter?.bondedDevices as Set<BluetoothDevice>)
return list
}
}
2 changes: 1 addition & 1 deletion android/app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
android:layout_height="wrap_content" />

<Button
android:id="@+id/refresh_devices"
android:id="@+id/refresh_btn"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.0"
classpath 'com.android.tools.build:gradle:4.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down

0 comments on commit df192fb

Please sign in to comment.