所以我试图使用android studio应用程序通过蓝牙读取arduino距离传感器的数据并显示它,但是该应用程序没有显示我在串行监视器和其他串行监视器应用程序中看到的第一位数字,例如未连接有线的lite。 (例如13厘米显示为3,145显示为45) (蓝牙连接似乎工作正常)
我实际上从这里获取的代码:http://android-er.blogspot.com/2015/07/android-example-to-communicate-with.html
这是代码
private final BluetoothSocket connectedBluetoothSocket;
private final InputStream connectedInputStream;
public ThreadConnected(BluetoothSocket socket) {
connectedBluetoothSocket = socket;
InputStream in = null;
try {
in = socket.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connectedInputStream = in;
}
@Override
public void run() {
byte[] buffer = new byte[1024];
int bytes;
while (true) {
try {
bytes = connectedInputStream.read(buffer);
String strReceived = new String(buffer, 0, bytes);
final String msgReceived =
strReceived;
runOnUiThread(new Runnable(){
@Override
public void run() {
textStatus.setText(msgReceived);
}});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
final String msgConnectionLost = "Connection lost:\n"
+ e.getMessage();
runOnUiThread(new Runnable(){
@Override
public void run() {
textStatus.setText(msgConnectionLost);
}}
我希望输出为234厘米,但得到34等。