我正在尝试在Arduino Uno上处理模拟压力传感器信号,并通过蓝牙将输出字符串发送到我的Android应用程序UI。
我已经在应用程序和HC-05模块之间建立了BT连接,并且可以通过将字符串写入到Arduino来获取UI上的inputStream,并作为响应返回一个字符串。
一旦从Arduino收到信号,我就试图触发对话框警报,将按钮b1配置为setOnclickListner以写入Arduino,并作为响应,Arduino发送inputStream。
问题是应用程序在活动打开后立即读取输入流,但此后停止接收,这对我来说是个问题,因为我的UI的设计假定基于来自实时输入数据的信号发送信号传感器,而不是何时由setOnClickListener触发。
我试图找到一种无需单击按钮即可写入Arduino的方法,然后一旦应用程序读取了输入流,我就需要它继续监听传入的数据并每次都调用对话框函数,在我可以开始?
public class Bluetooth_Activity extends AppCompatActivity {
//widgets
Button b1; Button b2; Button b3;
TextView t1; TextView t2;
// Bluetooth:
String address = null, name = null;
BluetoothAdapter myBluetooth = null;
BluetoothServerSocket serverSocket;
BluetoothSocket btSocket = null;
Set<BluetoothDevice> pairedDevices;
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
Handler bluetoothIn;
BluetoothDevice dispositivo;
private StringBuilder recDataString = new StringBuilder();
InputStream tmpIn = null;
OutputStream tmpOut = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth_);
b1 = (Button) findViewById(R.id.button1);
b3 = (Button) findViewById(R.id.str_dialog);
try {
bluetooth_connect_device();
} catch (IOException e) {
e.printStackTrace();
}
}
private void alertSystem () throws IOException {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(Bluetooth_Activity.this);
View mView = getLayoutInflater().inflate(R.layout.alert_dialog, null);
Button mClose = (Button) mView.findViewById(R.id.btn_close);
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
mClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
// BLUETOOTH FUNCTIONS:
private class someThread extends Thread{
public void run() {
abc();
}
}
private void bluetooth_connect_device() throws IOException {
try {
myBluetooth = BluetoothAdapter.getDefaultAdapter();
address = myBluetooth.getAddress();
pairedDevices = myBluetooth.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice bt : pairedDevices) {
address = bt.getAddress().toString();
name = bt.getName().toString();
Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception we) {
}
myBluetooth = BluetoothAdapter.getDefaultAdapter();//get the mobile bluetooth device
BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);//connects to the device's address and checks if it's available
btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);//create a RFCOMM (SPP) connection
btSocket.connect();
try
{
Toast.makeText(getApplicationContext(), ("BT Name: " + name + "\nBT Address: " + address), Toast.LENGTH_SHORT).show();
} catch (Exception e) {}
}
public void abc() {
try {
byte[] buffer = new byte[256]; // buffer store for the stream
int bytes; // bytes returned from read()
tmpIn = btSocket.getInputStream();
DataInputStream mmInStream = new DataInputStream(tmpIn);
bytes = mmInStream.read(buffer);
String readMessage = new String(buffer, 0, bytes);
Toast.makeText(getApplicationContext(),
"OutPut Recived From Bluetooth : \n" + readMessage,
Toast.LENGTH_SHORT).show();
alertSystem();
} catch (Exception e) {
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.button1)
{
try
{
String i="f"; //here i'm sending a single char f and when arduino recived it it will
// send a response
btSocket.getOutputStream().write(i.getBytes());
Thread.sleep(1500);
abc();
} catch (Exception e) {}
}
}
答案 0 :(得分:1)
很清楚为什么会这样,因为您是用这种方式编码的!您将执行Bluetooth io的代码片段放在onclick侦听器中,因此只有在单击该按钮时才能运行该代码;
如果您希望在收到某个蓝牙信号后限制android应用执行一段代码,则需要在另一个thread中无限期地监听该信号(并且不要阻塞ui)。 (不仅是单击按钮时),如果要更新用户界面,请调用handler;因此您的代码应如下所示:
new Thread(new Runnable() {
@Override
public void run() {
while (true){//an infinite loop
//read signals
//process them
//call some handler to deal with the ui
}
}
})