如何将接收到的数字一分为二
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将接收到的数字一分为二相关的知识,希望对你有一定的参考价值。
您好,亲爱的stackoverflow社区,我正在研究一个从arduino模块接收信息并将其显示为图表的项目。问题是我有5个元素(温度,湿度等),并且我拥有的代码一次只能接收一个数字(例如:2838752458),如示例中所示,该数字有10位数字来自arduino,我想将它们两两分开,所以每两个元素都需要一个元素。您可能会问为什么我没有设置处理程序,以便我可以在一个分开的时间接收每个两个数字,但是我已经尝试过了,这给我带来了封闭的应用程序错误,因为我一次只能收到一个数字。 >
如何将10位数字二乘二分开并将它们加到分开的整数中,以便可以将它们传递到图表活动中?请举一个“ 1234567890”这个数字的例子。public class Analysies extends AppCompatActivity { Handler h; String tekrar = ""; String dama = ""; String qaza = ""; String faaliat = ""; String rotobat = ""; private OutputStream outStream = null; private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); private static String address = "00:21:13:00:02:5B"; final int RECIEVE_MESSAGE = 1; // Status for Handler private BluetoothAdapter btAdapter = null; private BluetoothSocket btSocket = null; private StringBuilder sb = new StringBuilder(); private Analysies.ConnectedThread mConnectedThread; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_analysies); gifImageView = (GifImageView) findViewById(R.id.gifBro); txt1 = (GifImageView) findViewById(R.id.afterAutotxt); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { txt1.animate().alpha(1f).setDuration(2000); } }, 1000); h = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case RECIEVE_MESSAGE: // 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(" "); // 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()); sbprint.getBytes().toString(); ////////////////////////////// HERE IS WHERE I CAN RECEIVE INFORMATION FROM ARDUINO /////////////////////////////// } //Log.d(TAG, "...String:"+ sb.toString() + "Byte:" + msg.arg1 + "..."); // Toast.makeText(CommunicationAuto.this, "String:" + sb.toString() , Toast.LENGTH_LONG).show(); break; } } ; }; btAdapter = BluetoothAdapter.getDefaultAdapter(); // get Bluetooth adapter checkBTState(); } private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException { if(Build.VERSION.SDK_INT >= 10){ try { final Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class }); return (BluetoothSocket) m.invoke(device, MY_UUID); } catch (Exception e) { // Toast.makeText(getApplicationContext(), "Could not Insecure", Toast.LENGTH_SHORT).show(); } } return device.createRfcommSocketToServiceRecord(MY_UUID); } @Override public void onResume() { super.onResume(); // Set up a pointer to the remote node using it's address. BluetoothDevice device = btAdapter.getRemoteDevice(address); // Two things are needed to make a connection: // A MAC address, which we got above. // A Service ID or UUID. In this case we are using the // UUID for SPP. try { btSocket = createBluetoothSocket(device); } catch (IOException e) { // Toast.makeText(getApplicationContext(), "Socket failed", Toast.LENGTH_SHORT).show(); } // Discovery is resource intensive. Make sure it isn't going on // when you attempt to connect and pass your message. btAdapter.cancelDiscovery(); // Establish the connection. This will block until it connects. // Toast.makeText(getApplicationContext(), "Connecting", Toast.LENGTH_SHORT).show(); try { btSocket.connect(); // Toast.makeText(getApplicationContext(), "Connecting ok!", Toast.LENGTH_SHORT).show(); } catch (IOException e) { try { btSocket.close(); } catch (IOException e2) { // errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + "."); } } // Create a data stream so we can talk to server. mConnectedThread = new Analysies.ConnectedThread(btSocket); mConnectedThread.start(); } @Override public void onPause() { super.onPause(); try { btSocket.close(); } catch (IOException e2) { // errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + "."); } } private void checkBTState() { // Check for Bluetooth support and then check to make sure it is turned on // Emulator doesn't support Bluetooth and will return null if(btAdapter==null) { Toast.makeText(getApplicationContext(), " Bluetooth is not supported. ", Toast.LENGTH_SHORT).show(); } else { if (btAdapter.isEnabled()) { } else { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, 1); } } } private void errorExit(String title, String message){ Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show(); finish(); } 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 { // 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; } } } /* Call this from the main activity to send data to the remote device */ public void write(String message) { byte[] msgBuffer = message.getBytes(); try { mmOutStream.write(msgBuffer); } catch (IOException e) { } } } }
问题:
here是我到目前为止创建的图表的输出。
谢谢!
您好,亲爱的stackoverflow社区,我正在一个项目,该项目从arduino模块接收信息并将其显示为图表。问题是我有5个元素(温度,湿度等...)...
答案
首先请确保您的数字位数(例如:1234567890)始终可被2整除。嗯,那么我为您准备了一些技巧。
另一答案
这似乎可以满足您的要求:
以上是关于如何将接收到的数字一分为二的主要内容,如果未能解决你的问题,请参考以下文章
如何将从 json 接收到的 ko.observable 字符串解析为整数(数字)值
java Httpserver 传递过来的json里面的汉字 接收到的每个汉字对应一个问号“?” 请问该如何解决这个问题