为同一个.getBytes()分配不同的消息

时间:2014-07-24 08:32:11

标签: java

现在,我的用户界面有 4 EditText对象第1行第2行第3行第4行

我开始发送数据:

第1行上,字符串为 4569

第2行上,字符串为 369

当我尝试使用msg.getBytes().length获取长度时,我只获得4,这是第1行的长度= 4569。但实际上,我发送了2行数据,即2 EditText(FILLED with数据)。

但是如何获得第2行第3行第4行的长度?

buttonConnect.setOnClickListener(将数据发送到蓝牙设备)

buttonConnect.setOnClickListener(new View.OnClickListener() {

        @SuppressLint("NewApi")
        @Override
        public void onClick(View arg0) {
            if (count == 0) {
                Toast.makeText(bluetoothtest.this,
                        "Please enable bluetooth first.",
                        Toast.LENGTH_LONG).show();
            } 
            // Need API=14
            else if (!socket.isConnected()) {
                    Toast.makeText(bluetoothtest.this,
                            "Connecting! Please wait.",
                            Toast.LENGTH_LONG).show();
                }
            else {
                try {
                    sendMessageHandle(contentRow1.getText().toString());
                    sendMessageHandle(contentRow2.getText().toString());

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    });

sendMessageHandle方法:

private void sendMessageHandle(String msg) throws InterruptedException {

    final byte ANIMATION_MODE=1;
    final byte PICTURE_MODE=2;
    final byte TEXT_MODE=3;
    //final String PAUSE = "1";

    int temp,row;

    sPrefs = getSharedPreferences("storeData",MODE_PRIVATE);

    byte mode = (byte) sPrefs.getInt("Mode",1);
    byte [] pictureData = new byte [129];
    byte [] textSetting =new byte [5];


    if (socket == null) {
        Toast.makeText(mContext, "No connection", Toast.LENGTH_SHORT)
                .show();
        return;
    }
    try {
        OutputStream outputStream = socket.getOutputStream();
        if(mode==TEXT_MODE){
            int l;
            textSetting[0]= 4;
            Thread.sleep(500);
            l = msg.getBytes().length;
            System.out.println("Length is "+ l);


            outputStream.write(textSetting);    
            outputStream.write(msg.getBytes());


        }
                                                //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        //outputStream.write(PAUSE.getBytes());

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    list.add(new deviceListItem(msg, false));
    mAdapter.notifyDataSetChanged();
    chatList.setSelection(list.size() - 1);
}

0 个答案:

没有答案