From 0850b796bba8790df9ddddaebde1e75375c0acfa Mon Sep 17 00:00:00 2001 From: jhchoi Date: Thu, 7 Nov 2024 16:16:32 +0900 Subject: [PATCH 1/2] =?UTF-8?q?chore:=20=EB=9D=BC=EC=9D=B4=EB=B8=8C?= =?UTF-8?q?=EB=9F=AC=EB=A6=AC=20=EB=B2=84=EC=A0=84=EB=AA=85=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD,=20build:=20=EC=82=AC=EC=9A=A9=ED=95=98=EC=A7=80=20?= =?UTF-8?q?=EC=95=8A=EB=8A=94=20bintray=20=EB=B9=84=ED=99=9C=EC=84=B1.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- communicator/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/communicator/build.gradle b/communicator/build.gradle index 0d08270..84604fa 100644 --- a/communicator/build.gradle +++ b/communicator/build.gradle @@ -8,7 +8,7 @@ buildscript { } dependencies { classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' - classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' +// classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' } } @@ -37,7 +37,7 @@ dependencies { } group = 'com.neofect.communicator' -version = '2.1.9' +version = '2.1.10-SNAPSHOT' //if (rootProject.name == 'Communicator') { // apply from: project.file('../gradle/bintray-upload.gradle') From d86ef637833d29bc751cc7c837e4f5c0ffb5458a Mon Sep 17 00:00:00 2001 From: jhchoi Date: Thu, 7 Nov 2024 16:16:55 +0900 Subject: [PATCH 2/2] refac: bluetooth spp connection. java -> kotlin --- .../spp/BluetoothSppConnectThread.java | 86 ------------------ .../spp/BluetoothSppConnectThread.kt | 91 +++++++++++++++++++ 2 files changed, 91 insertions(+), 86 deletions(-) delete mode 100644 communicator/src/main/java/com/neofect/communicator/bluetooth/spp/BluetoothSppConnectThread.java create mode 100644 communicator/src/main/java/com/neofect/communicator/bluetooth/spp/BluetoothSppConnectThread.kt diff --git a/communicator/src/main/java/com/neofect/communicator/bluetooth/spp/BluetoothSppConnectThread.java b/communicator/src/main/java/com/neofect/communicator/bluetooth/spp/BluetoothSppConnectThread.java deleted file mode 100644 index b9a8a39..0000000 --- a/communicator/src/main/java/com/neofect/communicator/bluetooth/spp/BluetoothSppConnectThread.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright 2014-2015 Neofect Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.neofect.communicator.bluetooth.spp; - -import android.bluetooth.BluetoothAdapter; -import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothSocket; -import android.util.Log; - -import com.neofect.communicator.ConnectionType; - -import java.io.IOException; -import java.util.UUID; - -/** - * @author neo.kim@neofect.com - */ -class BluetoothSppConnectThread extends Thread { - - private static final String LOG_TAG = BluetoothSppConnectThread.class.getSimpleName(); - - private final BluetoothSppConnection connection; - - BluetoothSppConnectThread(BluetoothSppConnection connection) { - super("BluetoothSppConnectThread"); - this.connection = connection; - } - - @Override - public void run() { - // Always cancel discovery because it will slow down a connection. - // As recommended in http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingAsAClient - BluetoothAdapter.getDefaultAdapter().cancelDiscovery(); - - BluetoothDevice device = connection.getBluetoothDevice(); - - // Get a BluetoothSocket for a connection with the given BluetoothDevice. - BluetoothSocket socket; - try { - // UUID for SPP connection - UUID sppUuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); - - if (connection.getConnectionType() == ConnectionType.BLUETOOTH_SPP) { - socket = device.createRfcommSocketToServiceRecord(sppUuid); - } else if (connection.getConnectionType() == ConnectionType.BLUETOOTH_SPP_INSECURE) { - socket = device.createInsecureRfcommSocketToServiceRecord(sppUuid); - } else { - connection.onFailedToConnect(new RuntimeException("Unknown bluetooth connection type! '" + connection.getConnectionType() + "'")); - return; - } - } catch (IOException e) { - connection.onFailedToConnect(new RuntimeException("Failed to create bluetooth socket!", e)); - return; - } - - // Make a connection to the BluetoothSocket - try { - // This is a blocking call and will only return on a successful connection or an exception. - socket.connect(); - } catch (IOException e) { - try { - socket.close(); - } catch (IOException e1) { - Log.e(LOG_TAG, "", e1); - } - connection.onFailedToConnect(new RuntimeException("Failed to connect to device '" + connection.getDescriptionWithAddress() + "'", e)); - return; - } - - connection.onSucceededToConnect(socket); - } - -} diff --git a/communicator/src/main/java/com/neofect/communicator/bluetooth/spp/BluetoothSppConnectThread.kt b/communicator/src/main/java/com/neofect/communicator/bluetooth/spp/BluetoothSppConnectThread.kt new file mode 100644 index 0000000..49e53f1 --- /dev/null +++ b/communicator/src/main/java/com/neofect/communicator/bluetooth/spp/BluetoothSppConnectThread.kt @@ -0,0 +1,91 @@ +/* + * Copyright 2014-2015 Neofect Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.neofect.communicator.bluetooth.spp + +import android.bluetooth.BluetoothAdapter +import android.bluetooth.BluetoothSocket +import android.util.Log +import com.neofect.communicator.ConnectionType +import java.io.IOException +import java.util.UUID + +/** + * @author neo.kim@neofect.com + */ +internal class BluetoothSppConnectThread(private val connection: BluetoothSppConnection) : + Thread("BluetoothSppConnectThread") { + override fun run() { + // Always cancel discovery because it will slow down a connection. + // As recommended in http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingAsAClient + BluetoothAdapter.getDefaultAdapter().cancelDiscovery() + + val device = connection.bluetoothDevice + + + // Get a BluetoothSocket for a connection with the given BluetoothDevice. + val socket: BluetoothSocket? = try { + // UUID for SPP connection + val sppUuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB") + + when (connection.connectionType) { + ConnectionType.BLUETOOTH_SPP -> { + device.createRfcommSocketToServiceRecord(sppUuid) + } + + ConnectionType.BLUETOOTH_SPP_INSECURE -> { + device.createInsecureRfcommSocketToServiceRecord(sppUuid) + } + + else -> { + connection.onFailedToConnect(RuntimeException("Unknown bluetooth connection type! '" + connection.connectionType + "'")) + null + } + } + } catch (e: IOException) { + connection.onFailedToConnect(RuntimeException("Failed to create bluetooth socket!", e)) + null + } + if (socket == null) { + return + } + + + // Make a connection to the BluetoothSocket + try { + // This is a blocking call and will only return on a successful connection or an exception. + socket.connect() + } catch (e: IOException) { + try { + socket.close() + } catch (e1: IOException) { + Log.e(LOG_TAG, "", e1) + } + connection.onFailedToConnect( + RuntimeException( + "Failed to connect to device '" + connection.descriptionWithAddress + "'", + e + ) + ) + return + } + + connection.onSucceededToConnect(socket) + } + + companion object { + private val LOG_TAG: String = BluetoothSppConnectThread::class.java.simpleName + } +}