使用
创建字符串后String data = String.valueOf(seekBar1.getProgress()) + ":"
+ String.valueOf(seekBar2.getProgress()) + ":"
+ String.valueOf(seekBar3.getProgress()) + ":"
+ String.valueOf(seekBar4.getProgress()) + ":"
+ String.valueOf(seekBar5.getProgress());
通过蓝牙将此字符串发送到另一台设备,然后使用
byte[] rBuf = (byte[]) msg.obj;
String rMessage = new String(rBuf);
String[] split = rMessage.split(":");
seekBar1.setProgress(Integer.parseInt(split[0]));
seekBar2.setProgress(Integer.parseInt(split[1]));
seekBar3.setProgress(Integer.parseInt(split[2]));
seekBar4.setProgress(Integer.parseInt(split[3]));
seekBar5.setProgress(Integer.parseInt(split[4]));
我得到了
java.lang.NumberFormatException: Invalid int: "0�������������"...
我知道异常是什么,但我不完全确定它被抛出的原因,我已经将收到的数字打印为字符串,据我所知仅整数存在分裂[4] ..
有什么想法在这里发生了什么?可能与InputStream / OutputStream ...
有关处理蓝牙连接I / O的线程
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket, String socketType) {
Log.d(TAG, "create ConnectedThread: " + socketType);
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(ControlActivity.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
/**
* Write to the connected OutStream.
* @param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
mHandler.obtainMessage(ControlActivity.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
答案 0 :(得分:0)
可能rMessage最后会有一些字符像EOL字符或其他东西。
如果您不想花时间调试you could add a ":" to the end of "data"
,
String data = String.valueOf(seekBar1.getProgress()) + ":"
+ String.valueOf(seekBar2.getProgress()) + ":"
+ String.valueOf(seekBar3.getProgress()) + ":"
+ String.valueOf(seekBar4.getProgress()) + ":"
+ String.valueOf(seekBar5.getProgress()) + ":";
这样,您的split[4]
仍然合适,只有split[5]
无效,无论如何都不会使用。