Skip to content

Commit

Permalink
update to use BluetoothManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Feb 9, 2024
1 parent 68959ae commit f5efd56
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.app.PendingIntent.FLAG_IMMUTABLE
import android.app.Service
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
Expand All @@ -21,22 +22,17 @@ import java.io.Serializable
import java.util.*

open class BaseBluetoothChatService @JvmOverloads constructor(handler: Handler? = null, applicationContext: Context? = null) : Service(), Serializable {

var mDevice: BluetoothDevice? = null
var context: Context?
var context: Context? = null
var mNewState: Int
var bNoReconnect = false
var state: Int
// private AcceptThread mSecureAcceptThread;
//private AcceptThread mInsecureAcceptThread;

/**
* Return the current connection state.
*/
@get:Synchronized
var state: Int
set

val mAdapter: BluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
val mAdapter: BluetoothAdapter? by lazy {
(applicationContext?.getSystemService(Context.BLUETOOTH_SERVICE) as? BluetoothManager)?.adapter
}

/**
* Indicate that the connection attempt failed and notify the UI Activity.
Expand Down Expand Up @@ -109,7 +105,7 @@ open class BaseBluetoothChatService @JvmOverloads constructor(handler: Handler?
state = Constants.STATE_NONE
mNewState = state
mHandler = handler
context = applicationContext
this.context = applicationContext
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.treehouses.remote.bases

import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.Intent
import android.os.Handler
Expand All @@ -25,7 +26,7 @@ open class BaseFragment : Fragment() {
super.onAttach(context)
listener = if (context is HomeInteractListener) context else throw RuntimeException("Implement interface first")
mChatService = listener.getChatService()
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
mBluetoothAdapter = (context.getSystemService(Context.BLUETOOTH_SERVICE) as? BluetoothManager)?.adapter
}

fun isListenerInitialized() = ::listener.isInitialized
Expand All @@ -39,7 +40,7 @@ open class BaseFragment : Fragment() {
protected fun onLoad(mHandler: Handler?) {
// mChatService = listener.getChatService()
mChatService.updateHandler(mHandler!!)
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
mBluetoothAdapter = (context?.getSystemService(Context.BLUETOOTH_SERVICE) as? BluetoothManager)?.adapter

// If the adapter is null, then Bluetooth is not supported
if (mBluetoothAdapter == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.app.Dialog
import android.app.ProgressDialog
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
import android.content.*
import android.os.Bundle
import android.view.ContextThemeWrapper
Expand Down Expand Up @@ -43,7 +44,8 @@ class RPIDialogFragment : BaseDialogFragment(), DeviceDeleteListener {
private var bind: ActivityRpiDialogFragmentBinding? = null
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
instance = this
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
val bluetoothManager = context?.getSystemService(Context.BLUETOOTH_SERVICE) as? BluetoothManager
mBluetoothAdapter = bluetoothManager?.adapter
bluetoothCheck()
if (mBluetoothAdapter!!.isDiscovering) mBluetoothAdapter!!.cancelDiscovery()
mBluetoothAdapter!!.startDiscovery()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ 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
mAdapter.cancelDiscovery()
mAdapter?.cancelDiscovery()

// Make a connection to the BluetoothSocket
try {
Expand Down

0 comments on commit f5efd56

Please sign in to comment.