背景:从学姐接了项目打比赛,android项目蓝牙连接stm32红外传感器,最后显示数据。因为前期没安排好,最后压力和动力下两天改项目。
1/看了b站网课:37-Activity 启动与结束_哔哩哔哩_bilibili
比较系统,但是无法满足我两天内快速入门项目,解决实战项目问题。从这里挑着学了些基础知识。
2/直接边问边做边学。遇到基础知识,直接问,或者csdn补充。
3/app一连接蓝牙就闪退/无法显示页面
问gpt过程中,发现似乎是android版本的问题,需要添加权限。学姐手机似乎可以正常运行,我和小伙伴的不行。
逐次逼近法
但是前期因为懒得对比代码差异,直接apply,copy c+v;但是后期发现自带的apply不好使,检查不完全出来,还得自己对比修改代码。或者让其重新生成。
4/模拟器连接问题
模拟机上无法测试蓝牙
5/版本兼容问题
6/补充:期间通过csdn学习如何连接模拟器/真机
开发者模式-usb调试
7/logcat来看报错信息。有具体问题的报错,结局起来比较快速容易
8/暂时结束战斗。解决了蓝牙连接问题,显示了柱状图。
留一手代码。
package com.example.btcontroller;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
public class ChartActivity extends AppCompatActivity {
private StringBuilder receivedDataBuffer = new StringBuilder();
private TextView tv_in; //接收显示句柄
private BarChart barChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chart);
barChart = findViewById(R.id.ChartTest); //这里我们定义一下我们界面的控件
initBarChart(barChart);//这里调用方法初始化柱状图
Button showchartButton=findViewById(R.id.showchartButton);
showchartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 调用处理数据并更新图表的函数
displayBarChart();
}
});
Button dataButton=findViewById(R.id.dataButton);
dataButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
restoreDataFromFile();
}
});
tv_in = findViewById(R.id.data);
Button deleteButton=findViewById(R.id.deleteButton);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteHistoricalData();
}
});
}
private void restoreDataFromFile() {
try {
// 清空数据缓冲区
receivedDataBuffer.setLength(0);
// 从文件中读取历史数据
File file = new File(getFilesDir(), "received_data.txt");
if (file.exists()) {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
receivedDataBuffer.append(line);
}
reader.close();
tv_in.setText(receivedDataBuffer.toString()); // 将历史数据显示在 TextView
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "恢复数据失败", Toast.LENGTH_SHORT).show();
}
}
private void displayBarChart() {
try {
// 清空数据缓冲区
receivedDataBuffer.setLength(0);
// 读取历史数据文件
File file = new File(getFilesDir(), "received_data.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
StringBuilder dataBuilder = new StringBuilder();
while ((line = reader.readLine()) != null) {
dataBuilder.append(line);
}
reader.close();
String dataString = dataBuilder.toString();
// 解析数据
String[] dataArray = dataString.split(",");
if (dataArray.length > 0) {
List<BarEntry> entries = new ArrayList<>();
for (int i = 0; i < dataArray.length; i++) {
if (!dataArray[i].trim().isEmpty()) { // 检查数据是否为空
try {
float value = Float.parseFloat(dataArray[i].trim());
entries.add(new BarEntry(i, value));
} catch (NumberFormatException e) {
Toast.makeText(this, "数据格式不正确: " + dataArray[i], Toast.LENGTH_SHORT).show();
}
}
}
if (!entries.isEmpty()) {
// 创建柱状图数据集
BarDataSet dataSet = new BarDataSet(entries, "Data");
// 添加数据集到柱状图
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add(dataSet);
// 创建柱状图数据对象并设置数据集
BarData data = new BarData(dataSets);
// 设置柱状图的一些属性
barChart.setData(data);
barChart.setFitBars(true);
barChart.invalidate(); // 刷新图表显示
} else {
Toast.makeText(this, "没有有效的数据来显示图表", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "读取数据失败", Toast.LENGTH_SHORT).show();
}
}
public BarChart initBarChart(BarChart barchart){
barchart.setDescription(null);
barchart.setDrawBarShadow(false);
barchart.setDrawValueAboveBar(true);
XAxis XAxis=barchart.getXAxis();
YAxis YAxisLeft=barchart.getAxisLeft();
YAxis YAxisRight=barchart.getAxisRight();
setAxis(XAxis,YAxisLeft,YAxisRight);
return barchart;
}
public void setAxis(XAxis XAxis,YAxis YAxisLeft,YAxis YAxisRight){
XAxis.setPosition(com.github.mikephil.charting.components.XAxis.XAxisPosition.BOTTOM);
XAxis.setAxisLineWidth(1);
XAxis.setAxisMinimum(0);
XAxis.setDrawAxisLine(true);
XAxis.setDrawGridLines(false);
XAxis.setEnabled(true);
YAxisLeft.setAxisMinimum(0);
YAxisLeft.setDrawGridLines(false);
YAxisLeft.setDrawAxisLine(true);
YAxisLeft.setAxisLineWidth(1);
YAxisLeft.setEnabled(true);
YAxisRight.setAxisMinimum(0);
YAxisRight.setDrawGridLines(false);
YAxisRight.setDrawAxisLine(true);
YAxisRight.setAxisLineWidth(1);
YAxisRight.setEnabled(false);
}
private void deleteHistoricalData() {
try {
// 获取历史数据文件
File file = new File(getFilesDir(), "received_data.txt");
// 检查文件是否存在
if (file.exists()) {
// 删除文件
if (file.delete()) {
Toast.makeText(this, "删除历史数据成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "删除历史数据失败", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "历史数据文件不存在", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "删除历史数据失败", Toast.LENGTH_SHORT).show();
}
}
}
9/但是数据不对有问题。。。。本篇暂时到这里