当尝试使用SeekBar的 progress 控制连接到Arduino板的伺服电机时,我的应用程序崩溃了。我将在下面发布代码,也许有人可以抓住我所缺少的东西。
我提到我正在使用BluetoothHelper,可以在以下地址找到它:https://github.com/BasicAirData/BluetoothHelper
Android代码:
public class CarConnection {
BluetoothManager mBluetoothManager;
BluetoothHelper mBluetoothHelper;
MainActivity mMainActivity;
public CarConnection(MainActivity mainActivity) {
mMainActivity = mainActivity;
mBluetoothHelper = new BluetoothHelper();
mBluetoothHelper.setBluetoothHelperListener(new BluetoothHelper.BluetoothHelperListener() {
@Override
public void onBluetoothHelperMessageReceived(BluetoothHelper bluetoothhelper, final String message) {
Toast.makeText(mMainActivity, "Message received: " + message, Toast.LENGTH_SHORT).show();
}
@Override
public void onBluetoothHelperConnectionStateChanged(BluetoothHelper bluetoothhelper, boolean isConnected) {
// Do something, depending on the new connection status
String connectedStatus = isConnected ? "connected" : "disconnected";
Toast.makeText(mMainActivity, "Connection status changed: " + connectedStatus, Toast.LENGTH_SHORT).show();
}
mBluetoothHelper.Connect("VOLAN BT");
}
public void setAngle(int angle) {
mBluetoothHelper.SendMessage(Integer.toString(angle));
Toast.makeText(mMainActivity, "Sent value: " + Integer.toString(angle), Toast.LENGTH_LONG).show();
}
主要活动类别:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start_sending);
angleValue = (SeekBar) findViewById(R.id.simple_sb);
valueSent = (TextView) findViewById(R.id.value_sent);
final MainActivity mainActivityInstance = this;
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CarConnection mCarConnection = new CarConnection(mainActivityInstance);
}
});
angleValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
valueSent.setText("Value sent is: " + Integer.toString(progress));
mCarConnection.setAngle(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mCarConnection.setAngle(seekBar.getProgress());
}
});
}
Arduino代码:
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h> // TX RX software library for bluetooth
#include <Servo.h> // servo library
Servo myservo; // servo name
#define outputA 28
#define outputB 30
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7);
int angle = 0;
int aState;
int aLastState;
int bluetoothTx = 10; // bluetooth tx to 10 pin
int bluetoothRx = 11; // bluetooth rx to 11 pin
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
// initialize the lcd
lcd.setBacklightPin(3, POSITIVE);
lcd.setBacklight(HIGH); //
lcd.begin(16, 2);
lcd.clear();
pinMode(12, OUTPUT);
digitalWrite(12, HIGH);
pinMode(23, OUTPUT);
digitalWrite(23, LOW);
pinMode (outputA, INPUT);
pinMode (outputB, INPUT);
myservo.attach(7); // attach servo signal wire to pin 6
//Setup usb serial connection to computer
Serial.begin(9600);
//Setup Bluetooth serial connection to android
bluetooth.begin(9600);
aLastState = digitalRead(outputA);
}
void loop()
{
//Read from bluetooth and write to usb serial
if(bluetooth.available()> 0 ) // receive number from bluetooth
{
int servopos = bluetooth.read(); // save the received number to servopos
Serial.println(servopos); // serial print servopos current number received from bluetooth
myservo.write(servopos); // roate the servo the angle received from the android app
}
aState = digitalRead(outputA);
if (aState != aLastState) {
if (digitalRead(outputB) != aState) {
angle ++;
}
else {
angle --;
}
float angle_1 = angle * (1023.0 / 360);
float angle2 = angle_1 / (2.84 );
Serial.print("Position: ");
Serial.println(angle2);
lcd.clear();
lcd.print("Position: ");
lcd.print(int(angle2));
lcd.print("");
lcd.setCursor(0, 1);
}
aLastState = aState;
}
当按下“开始”按钮时,手机将以消息“已连接”进行响应!
提前谢谢!
答案 0 :(得分:1)
我认为问题在于UI线程中没有调用onBluetoothHelperConnectionStateChanged()
(为了不改变UI性能),因此您不能在此处调用Toast.makeText()
。
一种可能的解决方案是通过以下方式在您的类CarConnection中实现侦听器:
public void onBluetoothHelperMessageReceived(BluetoothHelper bluetoothhelper, final String message) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(mMainActivity, "Message received: " + message, Toast.LENGTH_SHORT).show();
}
});
}