Skip to content

Commit

Permalink
Add list for connected and disconnected devices (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sami Väntti authored Feb 11, 2022
1 parent 3f31544 commit a1d6b4e
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 6 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions app/src/main/java/com/automationdev/clearbluetooth/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,29 @@ import android.bluetooth.BluetoothManager
import android.content.Context
import android.os.Build
import android.util.Log
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.ListView
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import java.lang.Exception

class MainActivity : AppCompatActivity() {
private val TAG: String = MainActivity::class.java.getSimpleName()

// Elements
private lateinit var unpairButton: Button
private lateinit var statusText: TextView
private lateinit var pairedDevicesListView: ListView
private lateinit var unpairedDevicesListView: ListView
private lateinit var pairedDevices: Set<BluetoothDevice>

// Variables
private var pairedDevicesList: MutableList<String> = mutableListOf()
private var unpairedDevicesList: MutableList<String> = mutableListOf()
private lateinit var arrayAdapterPaired: ArrayAdapter<String>
private lateinit var arrayAdapterUnpaired: ArrayAdapter<String>

private val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (isGranted) {
Expand All @@ -42,6 +53,18 @@ class MainActivity : AppCompatActivity() {
val bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
pairedDevices = bluetoothManager.getAdapter().bondedDevices

// Get names for paired devices
for (device in pairedDevices) {
pairedDevicesList.add(device.address)
}

// Set adapters
pairedDevicesListView = findViewById(R.id.pairedDevices)
arrayAdapterPaired = ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, this.pairedDevicesList)
pairedDevicesListView.adapter = arrayAdapterPaired



// Change text
statusText = findViewById(R.id.bl_devices_text)
statusText.setText("There is currently ${pairedDevices.size} paired bluetooth devices.")
Expand All @@ -54,6 +77,10 @@ class MainActivity : AppCompatActivity() {
}

private fun unpairAll(pairedDevices: Set<BluetoothDevice>) {
// empty list
this.unpairedDevicesList = mutableListOf()

// Set unpair button
this.unpairButton.setEnabled(false)
this.unpairButton.setText("Please wait...")

Expand All @@ -63,11 +90,18 @@ class MainActivity : AppCompatActivity() {
try {
device::class.java.getMethod("removeBond").invoke(device)
unpairCount++
this.unpairedDevicesList.add(device.address)
this.pairedDevicesList.remove(device.address)

} catch (e: Exception) {
Log.w(TAG,"Failed to un-pair device: ${device.address}")
}
}
// Show unpaired devices on the list
this.arrayAdapterPaired.notifyDataSetChanged()
unpairedDevicesListView = findViewById(R.id.unpairedDevices)
arrayAdapterUnpaired = ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, this.unpairedDevicesList)
unpairedDevicesListView.adapter = arrayAdapterUnpaired
}

statusText.text = String.format("${unpairCount} of ${pairedDevices.size} devices un-paired.")
Expand Down
74 changes: 68 additions & 6 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
tools:context=".MainActivity">

<Button
android:id="@+id/clearAllButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="16dp"
android:text="@string/unpair_all"
app:layout_constraintBottom_toTopOf="@+id/bl_devices_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Expand All @@ -25,11 +24,74 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/clearAllButton" />
app:layout_constraintTop_toBottomOf="@+id/clearAllButton"
app:layout_constraintVertical_bias="0.114" />

<RelativeLayout
android:id="@+id/pairedDevicesLayout"
android:layout_width="178dp"
android:layout_height="365dp"
android:layout_marginStart="16dp"
android:layout_marginTop="84dp"
android:layout_marginBottom="16dp"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bl_devices_text"
tools:text="@tools:sample/lorem">
<TextView
android:id="@+id/connectedDevicesTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Paired Devices"
app:layout_constraintStart_toStartOf="parent" />

<ListView
android:id="@+id/pairedDevices"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/connectedDevicesTitle"
android:layout_marginTop="0dp" />
</RelativeLayout>

<RelativeLayout
android:id="@+id/unpairedDevicesLayout"
android:layout_width="178dp"
android:layout_height="365dp"
android:layout_marginStart="16dp"
android:layout_marginTop="84dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/pairedDevicesLayout"
app:layout_constraintTop_toBottomOf="@+id/bl_devices_text">

<TextView
android:id="@+id/disconnectedDevicesTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Unpaired Devices"
app:layout_constraintStart_toStartOf="parent" />

<ListView
android:id="@+id/unpairedDevices"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/disconnectedDevicesTitle" />
</RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit a1d6b4e

Please sign in to comment.