Skip to content

Commit

Permalink
actions: add bluetooth checks (fixes #1878) (#1896)
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 authored Feb 13, 2024
1 parent 2dd5a54 commit 5c640ca
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
*/
package io.treehouses.remote.network

import android.Manifest
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothSocket
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.os.*
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.preference.PreferenceManager
import io.treehouses.remote.Constants
import io.treehouses.remote.bases.BaseBluetoothChatService
Expand Down Expand Up @@ -101,11 +105,12 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
return START_NOT_STICKY
}

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
override fun onCreate() {
super.onCreate()
val i = IntentFilter()
i.addAction(DISCONNECT_ACTION)
registerReceiver(receiver, i)
registerReceiver(receiver, i, RECEIVER_NOT_EXPORTED)
}

override fun onDestroy() {
Expand Down Expand Up @@ -159,6 +164,9 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
@Synchronized
fun connected(socket: BluetoothSocket?, device: BluetoothDevice, socketType: String) {
logD("connected, Socket Type:$socketType")
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
return
}
connectedDeviceName = device.name
mDevice = device
// Cancel the thread that completed the connection
Expand Down Expand Up @@ -264,6 +272,9 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
name = "ConnectThread$mSocketType"
this@BluetoothChatService.state = Constants.STATE_CONNECTING
// Always cancel discovery because it will slow down a connection
if (ActivityCompat.checkSelfPermission(context!!, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
return
}
mAdapter?.cancelDiscovery()

// Make a connection to the BluetoothSocket
Expand Down Expand Up @@ -301,7 +312,10 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
// given BluetoothDevice
try {
// if (secure) {
tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID_SECURE)
if (ActivityCompat.checkSelfPermission(context!!, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID_SECURE)
}

// } else {
this@BluetoothChatService.state = Constants.STATE_CONNECTING
} catch (e: Exception) {
Expand Down

0 comments on commit 5c640ca

Please sign in to comment.