From e005cd8b473b487310d1d5c230cb7b3a534dfdd5 Mon Sep 17 00:00:00 2001 From: flatig_linux Date: Mon, 29 Aug 2022 23:35:30 +0800 Subject: [PATCH] Initial commit --- .../logic/tools/BluetoothRecyclerAdapter.kt | 4 +- .../view/BluetoothConnectionActivity.kt | 34 +-- .../layout/activity_bluetooth_connection.xml | 220 +++++++++++------- 3 files changed, 163 insertions(+), 95 deletions(-) diff --git a/src/main/java/com/flatig/bananaports/logic/tools/BluetoothRecyclerAdapter.kt b/src/main/java/com/flatig/bananaports/logic/tools/BluetoothRecyclerAdapter.kt index 81428bb..6f74718 100644 --- a/src/main/java/com/flatig/bananaports/logic/tools/BluetoothRecyclerAdapter.kt +++ b/src/main/java/com/flatig/bananaports/logic/tools/BluetoothRecyclerAdapter.kt @@ -21,7 +21,9 @@ class BluetoothRecyclerAdapter(private val deviceList: List } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { - val view = LayoutInflater.from(parent.context).inflate(R.layout.recycler_bluetooth, parent, false) + val view = LayoutInflater + .from(parent.context) + .inflate(R.layout.recycler_bluetooth, parent, false) val viewHolder = ViewHolder(view) viewHolder.itemView.setOnClickListener { diff --git a/src/main/java/com/flatig/bananaports/view/BluetoothConnectionActivity.kt b/src/main/java/com/flatig/bananaports/view/BluetoothConnectionActivity.kt index 182a507..672497c 100755 --- a/src/main/java/com/flatig/bananaports/view/BluetoothConnectionActivity.kt +++ b/src/main/java/com/flatig/bananaports/view/BluetoothConnectionActivity.kt @@ -57,14 +57,11 @@ class BluetoothConnectionActivity : AppCompatActivity() { setContentView(R.layout.activity_bluetooth_connection) initView() connectDevices() - } // Initial the view to make code clean private fun initView() { - val intent = intent -// deviceName = intent.getStringExtra("device").toString() -// deviceAddress = intent.getStringExtra("address").toString() + deviceName = StaticSingleData.bluetoothDeviceName deviceAddress = StaticSingleData.bluetoothDeviceAddress @@ -90,6 +87,9 @@ class BluetoothConnectionActivity : AppCompatActivity() { override fun onStartTrackingTouch(p0: SeekBar?) { } override fun onStopTrackingTouch(p0: SeekBar?) { + if ((-32 < seekBar1.progress) and (seekBar1.progress < 32)) { + seekBar1.progress = 0 + } numInt01 = seekBar1.progress textView01.text = numInt01.toString() } @@ -104,6 +104,9 @@ class BluetoothConnectionActivity : AppCompatActivity() { override fun onStartTrackingTouch(p0: SeekBar?) { } override fun onStopTrackingTouch(p0: SeekBar?) { + if ((-40 < seekBar2.progress) and (seekBar2.progress < 40)) { + seekBar2.progress = 0 + } numInt02 = seekBar2.progress textView02.text = numInt02.toString() } @@ -112,7 +115,13 @@ class BluetoothConnectionActivity : AppCompatActivity() { } - // Make connection to get Bluetooth Sockets + + override fun onDestroy() { + super.onDestroy() + sendDataThread?.interrupt() + receiveDataThread?.interrupt() + } + @SuppressLint("MissingPermission") private fun connectDevices() { bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager @@ -136,14 +145,6 @@ class BluetoothConnectionActivity : AppCompatActivity() { } } - - override fun onDestroy() { - super.onDestroy() - sendDataThread?.interrupt() - receiveDataThread?.interrupt() - } - - // Function to send message private fun sendMessage(content: String) { if (bluetoothSocket.isConnected) { try { @@ -182,7 +183,7 @@ class BluetoothConnectionActivity : AppCompatActivity() { try { message = "$stringT01:$stringT02" sendMessage(message) - Thread.sleep(75) + Thread.sleep(50) } catch (e: InterruptedException) { e.printStackTrace() break @@ -194,7 +195,7 @@ class BluetoothConnectionActivity : AppCompatActivity() { } } inner class ReceiveDataThread: Thread() { - private var inputStream: InputStream? = null + private var inputStream = bluetoothSocket.inputStream override fun run() { super.run() try { @@ -204,7 +205,8 @@ class BluetoothConnectionActivity : AppCompatActivity() { inputStream!!.read(buffer) val a = String(buffer, 0, buffer.size, charset("US-ASCII")) runOnUiThread { textViewContent.append(a) } - Thread.sleep(500) + Thread.sleep(1000) + runOnUiThread { textViewContent.setText("") } } catch (e: InterruptedException) { e.printStackTrace() break diff --git a/src/main/res/layout/activity_bluetooth_connection.xml b/src/main/res/layout/activity_bluetooth_connection.xml index 0da3250..2895256 100755 --- a/src/main/res/layout/activity_bluetooth_connection.xml +++ b/src/main/res/layout/activity_bluetooth_connection.xml @@ -1,92 +1,156 @@ - + android:layout_height="match_parent"> - - - - + + + - + android:layout_marginEnd="24dp" + app:layout_constraintBottom_toBottomOf="@+id/connection_text_devices" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintHorizontal_bias="1.0" + app:layout_constraintStart_toStartOf="@+id/guideline6" + app:layout_constraintTop_toTopOf="@+id/connection_text_devices" + app:layout_constraintVertical_bias="0.0" /> - + - + - + - + + + - + - - - + android:layout_marginStart="24dp" + android:layout_marginTop="8dp" + android:layout_marginEnd="24dp" + android:orientation="horizontal" + android:text="Receiving" + android:textSize="16sp" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@+id/divider11" + app:layout_constraintVertical_bias="0.0" /> - \ No newline at end of file + + \ No newline at end of file