add graph
This commit is contained in:
parent
06c4940a20
commit
4a4c5893af
@ -45,4 +45,5 @@ dependencies {
|
|||||||
testImplementation(libs.junit)
|
testImplementation(libs.junit)
|
||||||
androidTestImplementation(libs.androidx.junit)
|
androidTestImplementation(libs.androidx.junit)
|
||||||
androidTestImplementation(libs.androidx.espresso.core)
|
androidTestImplementation(libs.androidx.espresso.core)
|
||||||
|
implementation("com.github.PhilJay:MPAndroidChart:v3.1.0")
|
||||||
}
|
}
|
||||||
@ -4,6 +4,7 @@ import android.Manifest
|
|||||||
import android.bluetooth.BluetoothAdapter
|
import android.bluetooth.BluetoothAdapter
|
||||||
import android.bluetooth.BluetoothDevice
|
import android.bluetooth.BluetoothDevice
|
||||||
import android.bluetooth.BluetoothSocket
|
import android.bluetooth.BluetoothSocket
|
||||||
|
import android.content.Context
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
@ -12,10 +13,19 @@ import android.widget.Toast
|
|||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
|
import com.github.mikephil.charting.charts.LineChart
|
||||||
|
import com.github.mikephil.charting.components.XAxis
|
||||||
|
import com.github.mikephil.charting.formatter.ValueFormatter
|
||||||
|
import com.github.mikephil.charting.data.Entry
|
||||||
|
import com.github.mikephil.charting.data.LineData
|
||||||
|
import com.github.mikephil.charting.data.LineDataSet
|
||||||
import java.io.BufferedReader
|
import java.io.BufferedReader
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.io.InputStreamReader
|
import java.io.InputStreamReader
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import android.graphics.Color
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
@ -32,6 +42,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
private data class SensorReading(val secSinceStart: Long, val temperature: Float, val humidity: Float)
|
private data class SensorReading(val secSinceStart: Long, val temperature: Float, val humidity: Float)
|
||||||
private val sensorHistory = mutableListOf<SensorReading>()
|
private val sensorHistory = mutableListOf<SensorReading>()
|
||||||
|
|
||||||
|
private lateinit var lineChart: LineChart
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
enableEdgeToEdge()
|
enableEdgeToEdge()
|
||||||
@ -39,6 +51,9 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
val connectBtn = findViewById<Button>(R.id.connect_btn)
|
val connectBtn = findViewById<Button>(R.id.connect_btn)
|
||||||
|
|
||||||
|
lineChart = findViewById(R.id.Chart)
|
||||||
|
setupChart()
|
||||||
|
|
||||||
bluetoothManager = getSystemService(BLUETOOTH_SERVICE) as android.bluetooth.BluetoothManager
|
bluetoothManager = getSystemService(BLUETOOTH_SERVICE) as android.bluetooth.BluetoothManager
|
||||||
bluetoothAdapter = bluetoothManager.adapter
|
bluetoothAdapter = bluetoothManager.adapter
|
||||||
if (bluetoothAdapter == null) {
|
if (bluetoothAdapter == null) {
|
||||||
@ -113,6 +128,11 @@ class MainActivity : AppCompatActivity() {
|
|||||||
val reading = SensorReading(System.currentTimeMillis(), temp, hum)
|
val reading = SensorReading(System.currentTimeMillis(), temp, hum)
|
||||||
sensorHistory.add(reading)
|
sensorHistory.add(reading)
|
||||||
Log.d(tag, "Store: $reading")
|
Log.d(tag, "Store: $reading")
|
||||||
|
|
||||||
|
runOnUiThread {
|
||||||
|
updateChartData()
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Log.w(tag, "Invalid message format: $message")
|
Log.w(tag, "Invalid message format: $message")
|
||||||
}
|
}
|
||||||
@ -124,4 +144,67 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun setupChart() {
|
||||||
|
lineChart.apply {
|
||||||
|
description.isEnabled = false
|
||||||
|
setTouchEnabled(true)
|
||||||
|
isDragEnabled = true
|
||||||
|
setScaleEnabled(true)
|
||||||
|
setDrawGridBackground(false)
|
||||||
|
setPinchZoom(true)
|
||||||
|
|
||||||
|
xAxis.apply {
|
||||||
|
position = XAxis.XAxisPosition.BOTTOM
|
||||||
|
valueFormatter = object : ValueFormatter() {
|
||||||
|
override fun getFormattedValue(value: Float): String? {
|
||||||
|
val time = Date(value.toLong())
|
||||||
|
val format = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
|
||||||
|
return format.format(time)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
granularity = 1f
|
||||||
|
setDrawGridLines(false)
|
||||||
|
}
|
||||||
|
axisRight.isEnabled = false
|
||||||
|
|
||||||
|
axisLeft.apply {
|
||||||
|
setDrawGridLines(true)
|
||||||
|
axisMaximum = 0f
|
||||||
|
}
|
||||||
|
legend.isEnabled = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updateChartData() {
|
||||||
|
val tempEntries = ArrayList<Entry>()
|
||||||
|
val humEntries = ArrayList<Entry>()
|
||||||
|
|
||||||
|
for (reading in sensorHistory) {
|
||||||
|
tempEntries.add(Entry(reading.secSinceStart.toFloat(), reading.temperature))
|
||||||
|
humEntries.add(Entry(reading.secSinceStart.toFloat(), reading.humidity))
|
||||||
|
}
|
||||||
|
|
||||||
|
val tempSet = LineDataSet(tempEntries, "Temperature (°C)").apply {
|
||||||
|
color = Color.RED
|
||||||
|
lineWidth = 2f
|
||||||
|
setDrawCircles(false)
|
||||||
|
mode = LineDataSet.Mode.CUBIC_BEZIER
|
||||||
|
setDrawFilled(true)
|
||||||
|
fillDrawable = ContextCompat.getDrawable(this@MainActivity, R.drawable.gradiant_temp)
|
||||||
|
}
|
||||||
|
|
||||||
|
val humSet = LineDataSet(humEntries, "Humidity (%)").apply {
|
||||||
|
color = Color.BLUE
|
||||||
|
lineWidth = 2f
|
||||||
|
setDrawCircles(false)
|
||||||
|
mode = LineDataSet.Mode.CUBIC_BEZIER
|
||||||
|
setDrawFilled(true)
|
||||||
|
fillDrawable = ContextCompat.getDrawable(this@MainActivity, R.drawable.gradiant_hum)
|
||||||
|
}
|
||||||
|
|
||||||
|
val data = LineData(tempSet, humSet)
|
||||||
|
lineChart.data = data
|
||||||
|
lineChart.invalidate()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
6
app/src/main/res/drawable/gradiant_hum.xml
Normal file
6
app/src/main/res/drawable/gradiant_hum.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
|
||||||
|
<gradient
|
||||||
|
android:startColor="#660000FF"
|
||||||
|
android:endColor="#000000FF"
|
||||||
|
android:angle="-90"/>
|
||||||
|
</shape>
|
||||||
6
app/src/main/res/drawable/gradiant_temp.xml
Normal file
6
app/src/main/res/drawable/gradiant_temp.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
|
||||||
|
<gradient
|
||||||
|
android:startColor="#66FF0000"
|
||||||
|
android:endColor="#00FF0000"
|
||||||
|
android:angle="-90"/>
|
||||||
|
</shape>
|
||||||
@ -17,4 +17,17 @@
|
|||||||
app:layout_constraintHorizontal_bias="0.5"
|
app:layout_constraintHorizontal_bias="0.5"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
|
<com.github.mikephil.charting.charts.LineChart
|
||||||
|
android:id="@+id/Chart"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_marginTop="16dp"
|
||||||
|
android:layout_marginStart="16dp"
|
||||||
|
android:layout_marginEnd="16dp"
|
||||||
|
android:background="@android:color/white"
|
||||||
|
app:layout_constraintTop_toBottomOf="@id/connect_btn"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent" />
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
@ -16,6 +16,7 @@ dependencyResolutionManagement {
|
|||||||
repositories {
|
repositories {
|
||||||
google()
|
google()
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
maven { url = uri("https://jitpack.io") }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user