@@ -72,48 +72,48 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
72
72
super .onDestroy()
73
73
}
74
74
75
- var connectedDeviceName: String = " "
75
+ var connectedDeviceName: String? = " "
76
76
77
77
@Synchronized
78
- fun connect (device : BluetoothDevice , secure : Boolean ) {
78
+ fun connect (device : BluetoothDevice ? , secure : Boolean ) {
79
79
if (state == Constants .STATE_CONNECTING ) {
80
80
if (mConnectThread != null ) {
81
- mConnectThread!! .cancel()
81
+ mConnectThread? .cancel()
82
82
mConnectThread = null
83
83
}
84
84
}
85
85
if (mConnectedThread != null ) {
86
- mConnectedThread!! .cancel()
86
+ mConnectedThread? .cancel()
87
87
mConnectedThread = null
88
88
}
89
89
90
90
mConnectThread = ConnectThread (device, secure)
91
- mConnectThread!! .start()
91
+ mConnectThread? .start()
92
92
updateUserInterfaceTitle()
93
93
}
94
94
95
95
@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
98
98
mDevice = device
99
99
if (mConnectThread != null ) {
100
- mConnectThread!! .cancel()
100
+ mConnectThread? .cancel()
101
101
mConnectThread = null
102
102
}
103
103
104
104
if (mConnectedThread != null ) {
105
- mConnectedThread!! .cancel()
105
+ mConnectedThread? .cancel()
106
106
mConnectedThread = null
107
107
}
108
108
109
109
startNotification()
110
- mConnectedThread = ConnectedThread (socket, socketType )
111
- mConnectedThread!! .start()
110
+ mConnectedThread = ConnectedThread (socket)
111
+ mConnectedThread? .start()
112
112
113
113
updateUserInterfaceTitle()
114
114
val msg = mHandler?.obtainMessage(Constants .MESSAGE_DEVICE_NAME )
115
115
val bundle = Bundle ()
116
- bundle.putString(Constants .DEVICE_NAME , device.name)
116
+ bundle.putString(Constants .DEVICE_NAME , device? .name)
117
117
msg?.data = bundle
118
118
mHandler?.sendMessage(msg ? : Message ())
119
119
}
@@ -122,11 +122,11 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
122
122
fun stop () {
123
123
bNoReconnect = true
124
124
if (mConnectThread != null ) {
125
- mConnectThread!! .cancel()
125
+ mConnectThread? .cancel()
126
126
mConnectThread = null
127
127
}
128
128
if (mConnectedThread != null ) {
129
- mConnectedThread!! .cancel()
129
+ mConnectedThread? .cancel()
130
130
mConnectedThread = null
131
131
}
132
132
@@ -142,7 +142,7 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
142
142
r = mConnectedThread
143
143
}
144
144
if (out != null ) {
145
- r!! .write(out )
145
+ r? .write(out )
146
146
}
147
147
}
148
148
@@ -152,44 +152,45 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
152
152
153
153
val preferences = PreferenceManager .getDefaultSharedPreferences(applicationContext)
154
154
if (mDevice != null && ! bNoReconnect && preferences.getBoolean(" reconnectBluetooth" , true )) {
155
- connect(mDevice!! , true )
155
+ connect(mDevice, true )
156
156
} else {
157
157
state = Constants .STATE_NONE
158
158
updateUserInterfaceTitle()
159
159
start()
160
160
}
161
161
}
162
162
163
- private inner class ConnectThread (private val mmDevice : BluetoothDevice , secure : Boolean ) : Thread() {
163
+ private inner class ConnectThread (private val mmDevice : BluetoothDevice ? , secure : Boolean ) : Thread() {
164
164
private val mmSocket: BluetoothSocket ?
165
165
private val mSocketType: String
166
166
override fun run () {
167
167
name = " ConnectThread$mSocketType "
168
168
this @BluetoothChatService.state = Constants .STATE_CONNECTING
169
169
mAdapter?.cancelDiscovery()
170
170
try {
171
- mmSocket!! .connect()
171
+ mmSocket? .connect()
172
172
} catch (e: Exception ) {
173
+ e.printStackTrace()
173
174
closeSocket()
174
175
connectionFailed()
175
176
return
176
177
}
177
178
synchronized(this @BluetoothChatService) { mConnectThread = null }
178
- connected(mmSocket, mmDevice, mSocketType )
179
+ connected(mmSocket, mmDevice)
179
180
}
180
181
181
182
fun cancel () { closeSocket() }
182
183
183
184
fun closeSocket () {
184
- try { mmSocket!! .close() }
185
+ try { mmSocket? .close() }
185
186
catch (e: Exception ) { e.printStackTrace() }
186
187
}
187
188
188
189
init {
189
190
var tmp: BluetoothSocket ? = null
190
191
mSocketType = if (secure) " Secure" else " Insecure"
191
192
try {
192
- tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID_SECURE )
193
+ tmp = mmDevice? .createRfcommSocketToServiceRecord(MY_UUID_SECURE )
193
194
this @BluetoothChatService.state = Constants .STATE_CONNECTING
194
195
} catch (e: Exception ) {
195
196
e.printStackTrace()
@@ -200,8 +201,8 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
200
201
}
201
202
}
202
203
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
205
206
private val mmInStream: InputStream ?
206
207
private val mmOutStream: OutputStream ?
207
208
override fun run () {
@@ -210,7 +211,7 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
210
211
var out : String
211
212
while (true ) {
212
213
try {
213
- bytes = mmInStream!! .read(buffer)
214
+ bytes = mmInStream? .read(buffer) ? : 0
214
215
out = String (buffer, 0 , bytes)
215
216
mHandler?.obtainMessage(Constants .MESSAGE_READ , bytes, - 1 , out )?.sendToTarget()
216
217
} catch (e: IOException ) {
@@ -223,7 +224,7 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
223
224
224
225
fun write (buffer : ByteArray ) {
225
226
try {
226
- mmOutStream!! .write(buffer)
227
+ mmOutStream? .write(buffer)
227
228
mHandler?.obtainMessage(Constants .MESSAGE_WRITE , - 1 , - 1 , buffer)?.sendToTarget()
228
229
} catch (e: IOException ) {
229
230
e.printStackTrace()
@@ -232,19 +233,18 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a
232
233
233
234
fun cancel () {
234
235
try {
235
- mmSocket!! .close()
236
+ mmSocket? .close()
236
237
} catch (e: Exception ) {
237
238
e.printStackTrace()
238
239
}
239
240
}
240
241
241
242
init {
242
- mmSocket = socket
243
243
var tmpIn: InputStream ? = null
244
244
var tmpOut: OutputStream ? = null
245
245
try {
246
- tmpIn = socket!! .inputStream
247
- tmpOut = socket.outputStream
246
+ tmpIn = socket? .inputStream
247
+ tmpOut = socket? .outputStream
248
248
} catch (e: IOException ) {
249
249
e.printStackTrace()
250
250
}
0 commit comments