我正在尝试将Arduino HC-05的浮动数据发送到Android。我接收数据的android代码是
format-number()
我无法将字节缓冲区解析为float。我试过谷歌搜索,但没有帮助。谁能告诉我我做错了什么?
我得到的字符串是一些带问号的未知字符。
下面是我的Aurdino代码:
byte buffer[];
buffer = new byte[2048];
int bytes = dataInputStream.read(buffer);
if(bytes > 0) {
String message = new String(buffer, 0, bytes);
Log.i(TAG, "listen: " + message);
}
void loop() {
double duration, distance,FrontSensor,LeftSensor;
}
答案 0 :(得分:0)
我会改用以下代码:
rm(list = ls())
library(shiny)
ui <- fluidPage(
sidebarPanel(
div(style="display: inline-block;vertical-align:top; width: 150px;",selectInput("ddllgra", "Function:",c('mean','median','sd','count','min','max'), selected='mean')),
div(style="display: inline-block;vertical-align:top; width: 100px;",HTML("<br>")),
div(style="display: inline-block;vertical-align:top; width: 150px;",textInput(inputId="xlimitsmax", label="x-max", value = 0.5))),
mainPanel()
)
server <- shinyServer(function(input,output){})
shinyApp(ui, server)
这对我来说是从Arduino获取蓝牙信息的! :)
编辑:要在arduino中使用double to string转换值:
//create new class for connect thread
private class ConnectedThread extends Thread {
private final InputStream mmInStream;
private final OutputStream mmOutStream;
//creation of the connect thread
public ConnectedThread(BluetoothSocket socket) {
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
//Create I/O streams for connection
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[256];
int bytes;
// Keep looping to listen for received messages
while (true) {
try {
bytes = mmInStream.read(buffer); //read bytes from input buffer
String readMessage = new String(buffer, 0, bytes);
// Send the obtained bytes to the UI Activity via handler
bluetoothIn.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget();
} catch (IOException e) {
break;
}
}
}
//write method
public void write(String input) {
byte[] msgBuffer = input.getBytes(); //converts entered String into bytes
try {
mmOutStream.write(msgBuffer); //write bytes over BT connection via outstream
} catch (IOException e) {
//if you cannot write, close the application
Toast.makeText(getBaseContext(), "Connection Failure", Toast.LENGTH_LONG).show();
finish();
}
}
}
答案 1 :(得分:0)
这可能会有所帮助。
int charsRead = 0;
char[] buffer = new char[BUFFER_SIZE];
while (mRun)
{
charsRead = in.read(buffer);
serverMessage = new String(buffer).substring(0, charsRead);
if (serverMessage != null && mMessageListener != null) {
Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + serverMessage + "'");
}
serverMessage = null;
}