Skip to content

Commit de32c75

Browse files
Okuro3499dogi
andauthored
all: smoother bluetooth (fixes #2134) (#2138)
Co-authored-by: dogi <[email protected]>
1 parent a1dce11 commit de32c75

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

app/src/main/kotlin/io/treehouses/remote/network/BluetoothChatService.kt

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -72,48 +72,48 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
7272
super.onDestroy()
7373
}
7474

75-
var connectedDeviceName: String = ""
75+
var connectedDeviceName: String? = ""
7676

7777
@Synchronized
78-
fun connect(device: BluetoothDevice, secure: Boolean) {
78+
fun connect(device: BluetoothDevice?, secure: Boolean) {
7979
if (state == Constants.STATE_CONNECTING) {
8080
if (mConnectThread != null) {
81-
mConnectThread!!.cancel()
81+
mConnectThread?.cancel()
8282
mConnectThread = null
8383
}
8484
}
8585
if (mConnectedThread != null) {
86-
mConnectedThread!!.cancel()
86+
mConnectedThread?.cancel()
8787
mConnectedThread = null
8888
}
8989

9090
mConnectThread = ConnectThread(device, secure)
91-
mConnectThread!!.start()
91+
mConnectThread?.start()
9292
updateUserInterfaceTitle()
9393
}
9494

9595
@Synchronized
96-
fun connected(socket: BluetoothSocket?, device: BluetoothDevice, socketType: String) {
97-
connectedDeviceName = device.name
96+
fun connected(socket: BluetoothSocket?, device: BluetoothDevice?) {
97+
connectedDeviceName = device?.name
9898
mDevice = device
9999
if (mConnectThread != null) {
100-
mConnectThread!!.cancel()
100+
mConnectThread?.cancel()
101101
mConnectThread = null
102102
}
103103

104104
if (mConnectedThread != null) {
105-
mConnectedThread!!.cancel()
105+
mConnectedThread?.cancel()
106106
mConnectedThread = null
107107
}
108108

109109
startNotification()
110-
mConnectedThread = ConnectedThread(socket, socketType)
111-
mConnectedThread!!.start()
110+
mConnectedThread = ConnectedThread(socket)
111+
mConnectedThread?.start()
112112

113113
updateUserInterfaceTitle()
114114
val msg = mHandler?.obtainMessage(Constants.MESSAGE_DEVICE_NAME)
115115
val bundle = Bundle()
116-
bundle.putString(Constants.DEVICE_NAME, device.name)
116+
bundle.putString(Constants.DEVICE_NAME, device?.name)
117117
msg?.data = bundle
118118
mHandler?.sendMessage(msg ?: Message())
119119
}
@@ -122,11 +122,11 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
122122
fun stop() {
123123
bNoReconnect = true
124124
if (mConnectThread != null) {
125-
mConnectThread!!.cancel()
125+
mConnectThread?.cancel()
126126
mConnectThread = null
127127
}
128128
if (mConnectedThread != null) {
129-
mConnectedThread!!.cancel()
129+
mConnectedThread?.cancel()
130130
mConnectedThread = null
131131
}
132132

@@ -142,7 +142,7 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
142142
r = mConnectedThread
143143
}
144144
if (out != null) {
145-
r!!.write(out)
145+
r?.write(out)
146146
}
147147
}
148148

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

153153
val preferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
154154
if (mDevice != null && !bNoReconnect && preferences.getBoolean("reconnectBluetooth", true)) {
155-
connect(mDevice!!, true)
155+
connect(mDevice, true)
156156
} else {
157157
state = Constants.STATE_NONE
158158
updateUserInterfaceTitle()
159159
start()
160160
}
161161
}
162162

163-
private inner class ConnectThread(private val mmDevice: BluetoothDevice, secure: Boolean) : Thread() {
163+
private inner class ConnectThread(private val mmDevice: BluetoothDevice?, secure: Boolean) : Thread() {
164164
private val mmSocket: BluetoothSocket?
165165
private val mSocketType: String
166166
override fun run() {
167167
name = "ConnectThread$mSocketType"
168168
this@BluetoothChatService.state = Constants.STATE_CONNECTING
169169
mAdapter?.cancelDiscovery()
170170
try {
171-
mmSocket!!.connect()
171+
mmSocket?.connect()
172172
} catch (e: Exception) {
173+
e.printStackTrace()
173174
closeSocket()
174175
connectionFailed()
175176
return
176177
}
177178
synchronized(this@BluetoothChatService) { mConnectThread = null }
178-
connected(mmSocket, mmDevice, mSocketType)
179+
connected(mmSocket, mmDevice)
179180
}
180181

181182
fun cancel() { closeSocket() }
182183

183184
fun closeSocket() {
184-
try { mmSocket!!.close() }
185+
try { mmSocket?.close() }
185186
catch (e: Exception) { e.printStackTrace() }
186187
}
187188

188189
init {
189190
var tmp: BluetoothSocket? = null
190191
mSocketType = if (secure) "Secure" else "Insecure"
191192
try {
192-
tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID_SECURE)
193+
tmp = mmDevice?.createRfcommSocketToServiceRecord(MY_UUID_SECURE)
193194
this@BluetoothChatService.state = Constants.STATE_CONNECTING
194195
} catch (e: Exception) {
195196
e.printStackTrace()
@@ -200,8 +201,8 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
200201
}
201202
}
202203

203-
private inner class ConnectedThread(socket: BluetoothSocket?, socketType: String) : Thread() {
204-
private val mmSocket: BluetoothSocket?
204+
private inner class ConnectedThread(socket: BluetoothSocket?) : Thread() {
205+
private val mmSocket: BluetoothSocket? = socket
205206
private val mmInStream: InputStream?
206207
private val mmOutStream: OutputStream?
207208
override fun run() {
@@ -210,7 +211,7 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
210211
var out: String
211212
while (true) {
212213
try {
213-
bytes = mmInStream!!.read(buffer)
214+
bytes = mmInStream?.read(buffer) ?: 0
214215
out = String(buffer, 0, bytes)
215216
mHandler?.obtainMessage(Constants.MESSAGE_READ, bytes, -1, out)?.sendToTarget()
216217
} catch (e: IOException) {
@@ -223,7 +224,7 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
223224

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

233234
fun cancel() {
234235
try {
235-
mmSocket!!.close()
236+
mmSocket?.close()
236237
} catch (e: Exception) {
237238
e.printStackTrace()
238239
}
239240
}
240241

241242
init {
242-
mmSocket = socket
243243
var tmpIn: InputStream? = null
244244
var tmpOut: OutputStream? = null
245245
try {
246-
tmpIn = socket!!.inputStream
247-
tmpOut = socket.outputStream
246+
tmpIn = socket?.inputStream
247+
tmpOut = socket?.outputStream
248248
} catch (e: IOException) {
249249
e.printStackTrace()
250250
}

app/src/main/kotlin/io/treehouses/remote/ui/home/HomeViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class HomeViewModel(application: Application) : FragmentViewModel(application) {
102102
}
103103
s == RESULTS.VERSION && checkVersionSent -> checkVersion(output)
104104
s == RESULTS.REMOTE_CHECK -> {
105-
sendLog(mChatService.connectedDeviceName, output.trim().split(" "))
105+
sendLog("${mChatService.connectedDeviceName}", output.trim().split(" "))
106106
sendMessage(getString(R.string.TREEHOUSES_UPGRADE_CHECK))
107107
}
108108
else -> moreActions(output, s)

0 commit comments

Comments
 (0)