java - 通过蓝牙从 Android 发送时,字符串中缺少前三个字符

标签 java android string bluetooth buffer

当我将“M”字符串发送到设备时,我从制作字符串的地方调用时间函数。

代码:

` mManButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            man = 1;

            clearScreen();

            mManButton.setBackgroundResource(R.color.button_pressed);
            mStartButton.setBackgroundResource(R.color.button_default);
            mCalButton.setBackgroundResource(R.color.button_default);
            mTestButton.setBackgroundResource(R.color.button_default);
            mLinearButtton.setBackgroundResource(R.color.button_default);
            mAutoButton.setBackgroundResource(R.color.button_default);
            // Send a message using content of the edit text widget

            sendMessage("M");
            time();
        }

    });`

然后调用 time() 函数。 在这里,如果我的日期是星期一,则变量日期设置为 1。 这意味着在这个函数中我正在创建一个字符串,其中包含日期格式值。该字符串从“A”开始,以“B”结束。

代码:

 private void time()
{
    int day = 0;
    Date now = new Date();
    String sdf = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(now);

    switch(sdf){
        case ("Monday"):
            day = 1;
            break;
        case("Tuesday"):
            day = 2;
            break;
        case ("Wednesday"):
            day = 3;
            break;
        case ("Thursday"):
            day = 4;
            break;
        case("Friday"):
            day = 5;
            break;
        case ("Saturday"):
            day = 6;
            break;
        case("Sunday"):
            day = 7;
            break;
    }

    int mm = Calendar.getInstance().get(Calendar.MINUTE);
    int HH = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    int dd = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    int MM = Calendar.getInstance().get(Calendar.MONTH)+1;
    int yy = Calendar.getInstance().get(Calendar.YEAR)%100;

    if(mm<10) {
        String time1 = "A" + "0" + mm + HH + "0" + day + dd + MM + yy + "B"; //suppose time1 = A041303211216B
        tv7.setText("Please Wait..");
        int p = 0;
        while (p < time1.length())
        {
            char zx = time1.charAt(p);
            String xz = String.valueOf(zx);
            sendMessage(xz);
            p++;
        }
    }
    else if(mm>=10) {
        String time2 = "A" + mm + HH + "0" + day + dd + MM + yy + "B"; **//suppose time2 = A151303211216B**
        tv7.setText("Please Wait..");
        int k = 0;
        while (k < time2.length())
        {
            char zx = time2.charAt(k);
            String xz = String.valueOf(zx);
            sendMessage(xz);
            k++;
        }
    }
}

创建字符串后,我将字符串的每个字符发送到 sendMessage()。

代码:

private void sendMessage(String message) {
    // Check that we're actually connected before trying anything
    if (mChatService.getState() !=
            com.example.hasani.bluetoothterminal.BluetoothChatService.STATE_CONNECTED) {
        Toast.makeText(getActivity(), R.string.not_connected, Toast.LENGTH_SHORT).show();
        mStartButton.setBackgroundResource(R.color.button_default);
        mCalButton.setBackgroundResource(R.color.button_default);
        mTestButton.setBackgroundResource(R.color.button_default);
        mManButton.setBackgroundResource(R.color.button_default);
        mAutoButton.setBackgroundResource(R.color.button_default);
        return;
    }

    // Check that there's actually something to send
    if (message.length() > 0) {
        // Get the message bytes and tell the BluetoothChatService to write
        byte[] send = message.getBytes();
        mChatService.write(send);

        // Reset out string buffer to zero and clear the edit text field
        mOutStringBuffer.setLength(0);
    }
}

写入功能。

代码:

public void write(byte[] out) {
    // Create temporary object
    ConnectedThread r;
    // Synchronize a copy of the ConnectedThread
    synchronized (this) {
        if (mState != STATE_CONNECTED) return;
        r = mConnectedThread;
    }
    // Perform the write unsynchronized
    r.write(out);
}

ConnectedThread 中的 wite 代码:

public void write(byte[] buffer) {
        try {
            mmOutStream.write(buffer);

            // Share the sent message back to the UI Activity
            mHandler.obtainMessage(com.example.hasani.bluetoothterminal.Constants.MESSAGE_WRITE, -1, -1, buffer)
                    .sendToTarget();
        } catch (IOException e) {
            Log.e(TAG, "Exception during write", e);
        }
    }

因为里面有Handler的作用。 问题是在逐步调试时,每个字符都发送到另一个设备,并且该设备接收从“A”到“B”的每个字符串,因此没有问题。

但是当我运行我的android应用程序时,发送“M”后,调用 time() 函数并发送字符串,但发送字符串的前三个字符,即;设备未收到“Amm”。 我仍然不明白是什么导致了这个问题。 请帮忙!。将不胜感激。谢谢!

最佳答案

哦,等等!我得到了解决方案。万一有人遇到同样的情况。 在我的 onClickListener 中,我使用第二个处理程序在 5 秒延迟后调用 time() 函数。

我的 onClickListener 代码是:

mManButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            man = 1;
            linear = 0;
            auto = 0;
            cal = 0;
            test = 0;
            linear = 0;
            clearScreen();

            mManButton.setBackgroundResource(R.color.button_pressed);
            mStartButton.setBackgroundResource(R.color.button_default);
            mCalButton.setBackgroundResource(R.color.button_default);
            mTestButton.setBackgroundResource(R.color.button_default);
            mLinearButtton.setBackgroundResource(R.color.button_default);
            mAutoButton.setBackgroundResource(R.color.button_default);
            // Send a message using content of the edit text widget

            sendMessage("M");
            tv7.setText("Please wait....");
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    time();
                }
            },5000);
        }

    });

我的 time() 函数是:

private void time() {
    int day = 0;
    Date now = new Date();
    String sdf = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(now);

    switch (sdf) {

        case ("Monday"):
            day = 1;
            break;
        case ("Tuesday"):
            day = 2;
            break;
        case ("Wednesday"):
            day = 3;
            break;
        case ("Thursday"):
            day = 4;
            break;
        case ("Friday"):
            day = 5;
            break;
        case ("Saturday"):
            day = 6;
            break;
        case ("Sunday"):
            day = 7;
            break;
    }

    int mm = Calendar.getInstance().get(Calendar.MINUTE);
    int HH = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    int dd = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    int MM = Calendar.getInstance().get(Calendar.MONTH)+1;
    int yy = Calendar.getInstance().get(Calendar.YEAR)%100;


    String A = "A";
    String min = String.format("%02d",mm);
    String hour = String.format("%02d",HH);
    String d = String.format("%02d",day);
    String date = String.format("%02d",dd);
    String month = String.format("%02d",MM);
    String year = String.format("%02d",yy);
    String B = "B";

    String time2 = A+min+hour+d+date+month+year+B;
    sendMessage(time2);
}

现在我可以根据需要接收正确的数据。我的应用程序运行得非常顺利。

关于java - 通过蓝牙从 Android 发送时,字符串中缺少前三个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41255530/

相关文章:

java - 如何在 ListView 适配器线性布局中添加项目

java - 近似搜索字符串列表

java - 将字符串与数组列表中的字符串的一部分进行比较

java - 如何在 Java 中设置 icc 颜色配置文件和更改颜色空间

java - 具有解码器问题的 Kafka Avro 消费者

java - 从 FileInputStream 获取字符

java - 选择ListPreference时播放声音

Android AlertDialog.Builder 对齐文本

c++ - 是否有用于 Windows 资源加载字符串的有用 C++ 包装器类?

java - J2me应用程序多次播放文件内存不足异常