我所拥有的:
HC-05蓝牙模块
Arduino promini
BPM传感器
Android Studio 3.0
我做了什么:
使用串行连接Arduino,传感器和HC-05蓝牙
从Arduino脚本发送数据
使用第三方商店从Playstore接收并获取数据
我想做什么:
我当前的代码:
Arduino脚本:
loop(){
Serial.println("*"+String(BPM)+"#"); //BPM from sensor
delay(200)
}
还有一些用于使用Android蓝牙适配器进行连接和配对的代码,在这里我无法完整显示,因为仍然是硬编码,但是重点是:
我创建了一个处理程序,该处理程序从序列中接收传入的输入,并至少将它们显示在Toast组件上,但是仍然无法使其正常工作。
h = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case RECIEVE_MESSAGE:
Toast.makeText(getApplicationContext(),"Data in",Toast.LENGTH_SHORT
).show();// if receive massage
byte[] readBuf = (byte[]) msg.obj;
String strIncom = new String(readBuf, 0, msg.arg1); // create string from bytes array
sb.append(strIncom); // append string
int endOfLineIndex = sb.indexOf("\r\n"); // determine the end-of-line
if (endOfLineIndex > 0) { // if end-of-line,
String sbprint = sb.substring(0, endOfLineIndex); // extract string
sb.delete(0, sb.length()); // and clear
sensorView0.setText("Data from Arduino: " + sbprint); // update TextView
btnOff.setEnabled(true);
btnOn.setEnabled(true);
}
//Log.d(TAG, "...String:"+ sb.toString() + "Byte:" + msg.arg1 + "...");
break;
}
};
};
我也有一个课程:
private class ConnectedThread extends Thread {
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[256]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
Toast.makeText(getApplicationContext(),"Masuk ke handler private class",Toast.LENGTH_SHORT
).show();
// Read from the InputStream
bytes = mmInStream.read(buffer); // Get number of bytes and message in "buffer"
h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget(); // Send to message queue Handler
} catch (IOException e) {
break;
}
}
}
}
我认为这要求处理程序读取消息,但没有设法使它起作用。
我想至少将消息显示给Toast或TextView,但在最近三周内还没有明确的答案。 因此,任何可以提供帮助的人都会受到赞赏。