通过蓝牙从android发送时字符串中缺少前三个字符 [英] First three characters missing from string when send from android via bluetooth

查看:102
本文介绍了通过蓝牙从android发送时字符串中缺少前三个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将 M字符串发送到设备时,我从创建字符串的位置调用时间函数。

When I send "M" String to the Device I call time function from where I make my String.

代码:

` 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();
        }

    });`

调用e()函数。
在这里,如果我的日子是星期一,则将变量day设置为1。
这意味着在此函数中,我正在创建一个具有日期格式值的字符串。该字符串以 A开头,以 B结尾。

Then the time() function is called. Here if my day is Monday then the variable day is set to 1. That means in this function I am creating a String which has Date Format values in it. This string starts from "A" and ends with "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()。

When the string is created I send each characters of the string to 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中的位
代码:

The wite in ConnectedThread Code :

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的每个字符串,因此没有问题。

As there is a role of Handler in it. The issue is when debugging step by step, each character is sent to the other Device and that device receives each and every string from "A" to "B", thus there is no problem.

但是当我运行我的Android应用程序时,发送 M后,将调用time()函数并发送String,但是字符串的前三个字符即;设备未接收到 Amm。
我还是不明白是什么引起了问题。
请帮助!。将不胜感激。谢谢!

But when i run My android app, after sending "M", the time() function is called and the String is sent but the first three characters of the string i.e; "Amm" is not received by the device. I still don't understand what is causing the problem. Please Help!. Will be appreciated. Thank You!

推荐答案

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

Ohkay wait!!! I got the solution. In case if someone go through the same kind of situation. In my onClickListener I call my time() function after a 5 second delay using a second handler.

我的onClickListener代码是:

My onClickListener code is :

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()函数是:

My time() function is :

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);
}

现在我可以根据需要接收正确的数据了。我的应用程序就像一个魅力。

Now i can receive correct data as I required. My application works like a charm.

这篇关于通过蓝牙从android发送时字符串中缺少前三个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆