From a857002fa953dc0570a06e6c311eb4d31a99d377 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 24 Sep 2016 18:59:24 +0900 Subject: [PATCH] Fixed possible crash in Bluetooth code --- .../bluetooth/BluetoothConnectThread.scala | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/android/src/main/scala/com/nutomic/ensichat/bluetooth/BluetoothConnectThread.scala b/android/src/main/scala/com/nutomic/ensichat/bluetooth/BluetoothConnectThread.scala index 100f1e8..db7e9a8 100644 --- a/android/src/main/scala/com/nutomic/ensichat/bluetooth/BluetoothConnectThread.scala +++ b/android/src/main/scala/com/nutomic/ensichat/bluetooth/BluetoothConnectThread.scala @@ -12,18 +12,26 @@ class BluetoothConnectThread(device: Device, onConnected: (Device, BluetoothSock private val Tag = "ConnectThread" - private val socket = - device.btDevice.get.createInsecureRfcommSocketToServiceRecord(BluetoothInterface.AppUuid) + private val socket = try { + Option(device.btDevice.get.createInsecureRfcommSocketToServiceRecord(BluetoothInterface.AppUuid)) + } catch { + case e: IOException => + Log.w(Tag, "Failed to open Bluetooth connection", e) + None + } override def run(): Unit = { + if (socket.isEmpty) + return + Log.i(Tag, "Connecting to " + device.toString) try { - socket.connect() + socket.get.connect() } catch { case e: IOException => Log.v(Tag, "Failed to connect to " + device.toString, e) try { - socket.close() + socket.get.close() } catch { case e2: IOException => Log.e(Tag, "Failed to close socket", e2) @@ -32,7 +40,7 @@ class BluetoothConnectThread(device: Device, onConnected: (Device, BluetoothSock } Log.i(Tag, "Successfully connected to device " + device.name) - onConnected(new Device(device.btDevice.get, true), socket) + onConnected(new Device(device.btDevice.get, true), socket.get) } }