接收java中的数据后无法将字符串转换为int
我有一个arduino,它发送2个int,int之间用“:”隔开 例如,我的arduino发送数据=12315:15123 在我使用蓝牙发送数据后,我在智能手机中接收数据,我需要将接收到的2值再次变为int 这是我在android上的部件代码接收java中的数据后无法将字符串转换为int,java,android,android-bluetooth,Java,Android,Android Bluetooth,我有一个arduino,它发送2个int,int之间用“:”隔开 例如,我的arduino发送数据=12315:15123 在我使用蓝牙发送数据后,我在智能手机中接收数据,我需要将接收到的2值再次变为int 这是我在android上的部件代码 void beginListenForData() { final Handler handler = new Handler(); final byte delimiter = 10; //This is the
void beginListenForData()
{
final Handler handler = new Handler();
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable()
{
public void run()
{
String[] tmp;
String lol,dam;
tmp = data.split(":");
try
{
lol = String.valueOf(crypt[0])+":"+String.valueOf(crypt[1]);
derajat.setText(lol);
}
catch (NumberFormatException nfe)
{
// bad data - set to sentinel
crypt[0] = Integer.MIN_VALUE;
crypt[1] = Integer.MIN_VALUE;
derajat.setText("none");
}
dam = tmp[0]+":"+tmp[1];
myLabel.setText(dam);
//derajat.setText(data);
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});
workerThread.start();
}
void beginListenForData()
{
最终处理程序=新处理程序();
final byte delimiter=10;//这是换行符的ASCII码
stopWorker=false;
readBufferPosition=0;
readBuffer=新字节[1024];
workerThread=新线程(new Runnable())
{
公开募捐
{
而(!Thread.currentThread().isInterrupted()&&!stopWorker)
{
尝试
{
int bytesavable=mmInputStream.available();
如果(字节可用>0)
{
byte[]packetBytes=新字节[bytesAvailable];
mmInputStream.read(packetBytes);
对于(int i=0;i,如果从Arduino接收到的数据实际上与您指定的相同:
"12315: 15123"
然后发生的事情是,你得到了一个数字形式的感觉,你做了捕捉,但除了德拉贾特之外真的没有办法知道它将什么都不包含(或“无”)……这正是你正在经历的
之所以会出现NumberFormatException,是因为冒号(:)分隔符后面有一个空格,因此当您尝试执行以下操作时:
crypt[1] = Integer.parseInt(tmp[1]);
(tmp[1]实际上包含“15123”)将抛出NumberFormatException。Integer.parseInt()方法不会处理数字字符串中的空格。要纠正此问题,请修剪分割的字符串,例如:
crypt[0] = Integer.parseInt(tmp[0].trim());
crypt[1] = Integer.parseInt(tmp[1].trim());
或拆分为:
tmp = data.split(": ");
如果从Arduino接收到的数据实际上与您指定的相同:
"12315: 15123"
然后发生的事情是,你得到了一个数字形式的感觉,你做了捕捉,但除了德拉贾特之外真的没有办法知道它将什么都不包含(或“无”)……这正是你正在经历的
之所以会出现NumberFormatException,是因为冒号(:)分隔符后面有一个空格,因此当您尝试执行以下操作时:
crypt[1] = Integer.parseInt(tmp[1]);
(tmp[1]实际上包含“15123”)将抛出NumberFormatException。Integer.parseInt()方法不会处理数字字符串中的空格。要纠正此问题,请修剪分割的字符串,例如:
crypt[0] = Integer.parseInt(tmp[0].trim());
crypt[1] = Integer.parseInt(tmp[1].trim());
或拆分为:
tmp = data.split(": ");
哪里定义了crypt
呢?你不应该在那里也使用temp
吗?我使用int[]crypt=new int[10],我在全局b中使用它,因为我需要在其他类中使用该值,哪里定义了crypt
呢?你不应该在那里也使用temp
吗?我使用int[]crypt=new int[10],我在全局B中使用它,因为我需要在其他类中使用该值