check if Bluetooth is enabled

master
Zhengyu Peng 3 years ago
parent b3dc1f3fd1
commit 3daa95c276

@ -2,6 +2,8 @@ package com.rookiedev.hexapod
import android.app.Activity import android.app.Activity
import android.app.AlertDialog import android.app.AlertDialog
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
@ -39,6 +41,7 @@ class MainActivity : AppCompatActivity() {
private const val ERROR_NO_PERMISSION = 0 private const val ERROR_NO_PERMISSION = 0
private const val ERROR_NO_DEVICE = 1 private const val ERROR_NO_DEVICE = 1
private const val ERROR_BLUETOOTH_DISABLED = 2
} }
private var mContext: Context? = null private var mContext: Context? = null
@ -115,12 +118,20 @@ class MainActivity : AppCompatActivity() {
// select bluetooth device // select bluetooth device
selectedDevice.setOnClickListener { selectedDevice.setOnClickListener {
val bluetoothManager =
if (btConnectPermission && btScanPermission) { this.mContext!!.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
val serverIntent = Intent(this, DeviceListActivity::class.java) val bluetoothAdapter: BluetoothAdapter? = bluetoothManager.adapter
resultLauncher.launch(serverIntent) if (bluetoothAdapter != null) {
if (!bluetoothAdapter.isEnabled) {
alertDialog(ERROR_BLUETOOTH_DISABLED)
}
} else { } else {
alertDialog(ERROR_NO_PERMISSION) if (btConnectPermission && btScanPermission) {
val serverIntent = Intent(this, DeviceListActivity::class.java)
resultLauncher.launch(serverIntent)
} else {
alertDialog(ERROR_NO_PERMISSION)
}
} }
} }
@ -159,15 +170,24 @@ class MainActivity : AppCompatActivity() {
} }
} else if (tabLayout.selectedTabPosition == 1) { } else if (tabLayout.selectedTabPosition == 1) {
if (btDeviceMac.text.isNotBlank()) { if (btDeviceMac.text.isNotBlank()) {
if (btConnectPermission && btScanPermission) { val bluetoothManager =
saveSharedPref() this.mContext!!.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
val intent = Intent(this, ControlActivity::class.java).apply { val bluetoothAdapter: BluetoothAdapter? = bluetoothManager.adapter
putExtra("interface", TAB_BLUETOOTH) if (bluetoothAdapter != null) {
putExtra("mac", btDeviceMac.text.toString()) if (!bluetoothAdapter.isEnabled) {
alertDialog(ERROR_BLUETOOTH_DISABLED)
} }
startActivity(intent)
} else { } else {
alertDialog(ERROR_NO_PERMISSION) if (btConnectPermission && btScanPermission) {
saveSharedPref()
val intent = Intent(this, ControlActivity::class.java).apply {
putExtra("interface", TAB_BLUETOOTH)
putExtra("mac", btDeviceMac.text.toString())
}
startActivity(intent)
} else {
alertDialog(ERROR_NO_PERMISSION)
}
} }
} else { } else {
alertDialog(ERROR_NO_DEVICE) alertDialog(ERROR_NO_DEVICE)
@ -200,10 +220,8 @@ class MainActivity : AppCompatActivity() {
if (result.resultCode == Activity.RESULT_OK) { if (result.resultCode == Activity.RESULT_OK) {
// There are no request codes // There are no request codes
val data: Intent? = result.data val data: Intent? = result.data
btDeviceName.text = data!!.getStringExtra("device_name") btDeviceName.text = data!!.getStringExtra("device_name")
btDeviceMac.text = data.getStringExtra("device_address") btDeviceMac.text = data.getStringExtra("device_address")
} }
} }
@ -322,6 +340,18 @@ class MainActivity : AppCompatActivity() {
"OK" "OK"
) { _, _ -> } ) { _, _ -> }
} }
ERROR_BLUETOOTH_DISABLED -> {
alert.setTitle("Bluetooth Disabled")
alert.setIcon(R.drawable.ic_baseline_error_24)
alert.setMessage(
"Please enable Bluetooth in Settings"
)
alert.setOnCancelListener { }
alert.setButton(
AlertDialog.BUTTON_POSITIVE,
"OK"
) { _, _ -> }
}
} }
alert.show() alert.show()
} }

Loading…
Cancel
Save