Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Flatigers committed Aug 29, 2022
1 parent b9cf7f0 commit e005cd8
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class BluetoothRecyclerAdapter(private val deviceList: List<BluetoothDeviceInfo>
}

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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
}
Expand All @@ -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()
}
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down
220 changes: 142 additions & 78 deletions src/main/res/layout/activity_bluetooth_connection.xml
Original file line number Diff line number Diff line change
@@ -1,92 +1,156 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFF">
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="51dp"
android:orientation="horizontal">
<TextView
android:id="@+id/connection_text_devices"
android:text="@string/app_name"
android:textSize="16sp"
android:textColor="@color/blue_dai"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:gravity="left|start|bottom"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
tools:ignore="RtlHardcoded" />
<TextView
android:id="@+id/connection_text_address"
android:text="@string/app_name"
android:textSize="14sp"
android:textColor="@color/blue_dai"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:gravity="end|bottom|right"
android:padding="2dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
tools:ignore="RtlHardcoded,SmallSp" />
</LinearLayout>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintGuide_percent="0.50"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/connection_text_devices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline6"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />

<LinearLayout
android:layout_width="match_parent"
<TextView
android:id="@+id/connection_text_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
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" />

<SeekBar
android:min="-128"
android:max="127"
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="40dp"
tools:ignore="UnusedAttribute" />
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="0dp"
android:layout_height="24dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:max="127"
android:min="-128"
android:rotation="-90"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider10"
app:layout_constraintVertical_bias="0.24"
tools:ignore="UnusedAttribute" />

<TextView
android:id="@+id/textView01"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="TextView"
android:textColor="@color/blue_dai2"
tools:ignore="HardcodedText" />
<SeekBar
android:id="@+id/seekBar2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:max="127"
android:min="-128"
app:layout_constraintBottom_toBottomOf="@+id/seekBar1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline6"
app:layout_constraintTop_toTopOf="@+id/seekBar1"
tools:ignore="UnusedAttribute" />

<SeekBar
android:min="-128"
android:max="127"
android:id="@+id/seekBar2"
android:layout_width="match_parent"
android:layout_height="40dp"
tools:ignore="UnusedAttribute" />
<View
android:id="@+id/divider10"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/connection_text_devices"
app:layout_constraintVertical_bias="0.0" />

<TextView
android:id="@+id/textView02"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="TextView"
android:textColor="@color/blue_dai2"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/textView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider10"
app:layout_constraintVertical_bias="0.0" />

<TextView
android:id="@+id/textView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="@+id/textView01"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline6"
app:layout_constraintTop_toTopOf="@+id/textView01" />

</LinearLayout>
<View
android:id="@+id/divider11"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/seekBar1"
app:layout_constraintVertical_bias="0.20" />

<LinearLayout
android:layout_width="match_parent"
<TextView
android:id="@+id/textView24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/connection_text_receive"
android:text="Receiving"
android:textColor="@color/blue_dai"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="HardcodedText" />
</LinearLayout>
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" />

</LinearLayout>
<EditText
android:id="@+id/connection_text_receive"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView24" />
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit e005cd8

Please sign in to comment.