Skip to content

Commit

Permalink
all: smoother bluetooth (fixes #2134) (#2138)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Nov 12, 2024
1 parent a1dce11 commit de32c75
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,48 +72,48 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
super.onDestroy()
}

var connectedDeviceName: String = ""
var connectedDeviceName: String? = ""

@Synchronized
fun connect(device: BluetoothDevice, secure: Boolean) {
fun connect(device: BluetoothDevice?, secure: Boolean) {
if (state == Constants.STATE_CONNECTING) {
if (mConnectThread != null) {
mConnectThread!!.cancel()
mConnectThread?.cancel()
mConnectThread = null
}
}
if (mConnectedThread != null) {
mConnectedThread!!.cancel()
mConnectedThread?.cancel()
mConnectedThread = null
}

mConnectThread = ConnectThread(device, secure)
mConnectThread!!.start()
mConnectThread?.start()
updateUserInterfaceTitle()
}

@Synchronized
fun connected(socket: BluetoothSocket?, device: BluetoothDevice, socketType: String) {
connectedDeviceName = device.name
fun connected(socket: BluetoothSocket?, device: BluetoothDevice?) {
connectedDeviceName = device?.name
mDevice = device
if (mConnectThread != null) {
mConnectThread!!.cancel()
mConnectThread?.cancel()
mConnectThread = null
}

if (mConnectedThread != null) {
mConnectedThread!!.cancel()
mConnectedThread?.cancel()
mConnectedThread = null
}

startNotification()
mConnectedThread = ConnectedThread(socket, socketType)
mConnectedThread!!.start()
mConnectedThread = ConnectedThread(socket)
mConnectedThread?.start()

updateUserInterfaceTitle()
val msg = mHandler?.obtainMessage(Constants.MESSAGE_DEVICE_NAME)
val bundle = Bundle()
bundle.putString(Constants.DEVICE_NAME, device.name)
bundle.putString(Constants.DEVICE_NAME, device?.name)
msg?.data = bundle
mHandler?.sendMessage(msg ?: Message())
}
Expand All @@ -122,11 +122,11 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
fun stop() {
bNoReconnect = true
if (mConnectThread != null) {
mConnectThread!!.cancel()
mConnectThread?.cancel()
mConnectThread = null
}
if (mConnectedThread != null) {
mConnectedThread!!.cancel()
mConnectedThread?.cancel()
mConnectedThread = null
}

Expand All @@ -142,7 +142,7 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
r = mConnectedThread
}
if (out != null) {
r!!.write(out)
r?.write(out)
}
}

Expand All @@ -152,44 +152,45 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a

val preferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
if (mDevice != null && !bNoReconnect && preferences.getBoolean("reconnectBluetooth", true)) {
connect(mDevice!!, true)
connect(mDevice, true)
} else {
state = Constants.STATE_NONE
updateUserInterfaceTitle()
start()
}
}

private inner class ConnectThread(private val mmDevice: BluetoothDevice, secure: Boolean) : Thread() {
private inner class ConnectThread(private val mmDevice: BluetoothDevice?, secure: Boolean) : Thread() {
private val mmSocket: BluetoothSocket?
private val mSocketType: String
override fun run() {
name = "ConnectThread$mSocketType"
this@BluetoothChatService.state = Constants.STATE_CONNECTING
mAdapter?.cancelDiscovery()
try {
mmSocket!!.connect()
mmSocket?.connect()
} catch (e: Exception) {
e.printStackTrace()
closeSocket()
connectionFailed()
return
}
synchronized(this@BluetoothChatService) { mConnectThread = null }
connected(mmSocket, mmDevice, mSocketType)
connected(mmSocket, mmDevice)
}

fun cancel() { closeSocket() }

fun closeSocket() {
try { mmSocket!!.close() }
try { mmSocket?.close() }
catch (e: Exception) { e.printStackTrace() }
}

init {
var tmp: BluetoothSocket? = null
mSocketType = if (secure) "Secure" else "Insecure"
try {
tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID_SECURE)
tmp = mmDevice?.createRfcommSocketToServiceRecord(MY_UUID_SECURE)
this@BluetoothChatService.state = Constants.STATE_CONNECTING
} catch (e: Exception) {
e.printStackTrace()
Expand All @@ -200,8 +201,8 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
}
}

private inner class ConnectedThread(socket: BluetoothSocket?, socketType: String) : Thread() {
private val mmSocket: BluetoothSocket?
private inner class ConnectedThread(socket: BluetoothSocket?) : Thread() {
private val mmSocket: BluetoothSocket? = socket
private val mmInStream: InputStream?
private val mmOutStream: OutputStream?
override fun run() {
Expand All @@ -210,7 +211,7 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
var out: String
while (true) {
try {
bytes = mmInStream!!.read(buffer)
bytes = mmInStream?.read(buffer) ?: 0
out = String(buffer, 0, bytes)
mHandler?.obtainMessage(Constants.MESSAGE_READ, bytes, -1, out)?.sendToTarget()
} catch (e: IOException) {
Expand All @@ -223,7 +224,7 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a

fun write(buffer: ByteArray) {
try {
mmOutStream!!.write(buffer)
mmOutStream?.write(buffer)
mHandler?.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)?.sendToTarget()
} catch (e: IOException) {
e.printStackTrace()
Expand All @@ -232,19 +233,18 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a

fun cancel() {
try {
mmSocket!!.close()
mmSocket?.close()
} catch (e: Exception) {
e.printStackTrace()
}
}

init {
mmSocket = socket
var tmpIn: InputStream? = null
var tmpOut: OutputStream? = null
try {
tmpIn = socket!!.inputStream
tmpOut = socket.outputStream
tmpIn = socket?.inputStream
tmpOut = socket?.outputStream
} catch (e: IOException) {
e.printStackTrace()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class HomeViewModel(application: Application) : FragmentViewModel(application) {
}
s == RESULTS.VERSION && checkVersionSent -> checkVersion(output)
s == RESULTS.REMOTE_CHECK -> {
sendLog(mChatService.connectedDeviceName, output.trim().split(" "))
sendLog("${mChatService.connectedDeviceName}", output.trim().split(" "))
sendMessage(getString(R.string.TREEHOUSES_UPGRADE_CHECK))
}
else -> moreActions(output, s)
Expand Down

0 comments on commit de32c75

Please sign in to comment.