通过蓝牙从Android向其他设备发送字符串的问题

时间:2016-12-22 06:16:02

标签: android string bluetooth

我正在尝试从我的Android设备向其他设备发送字符串。在我的代码中,我试图获取日期和时间并存储在字符串中,然后通过蓝牙将该字符串发送到设备。

问题是当我发送字符串时,其他设备收到的某些字符会丢失。

我的代码是:

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 time2 = "A"+mm+HH+"0"+day+dd+MM+yy+"B";
        sendMessage(time2);
}

我通过调用下面给出的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();

        while (!mChatService.write(send))
        {
            try{
                Thread.sleep(10);
            }
            catch (InterruptedException ex){
                //Log.d(TAG,"Interrupted Exception");
                //break;
            }
        }

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

但是如果我将我的字符串设置为 字符串t =“A000000000000B或”A111111111111B“(长度= 14),然后其他设备接收该字符串,我可以进一步操作。

代码:

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 time2 = "A000000000000B";    //*just to check out assigning this value*//
        sendMessage(time2);
}

如果我发送长度为14的字符串“A000000000000B”,则接受。但是当像上面那样进行计算时,字符串的起始位置会缺少值。

请帮忙!谢谢!

1 个答案:

答案 0 :(得分:0)

String time2 = "A"+mm+HH+"0"+day+dd+MM+yy+"B";中,您的所有字段均为int,这意味着第3分钟字符串为“3”而不是“03”。

如果您坚持使用int,请尝试

String min = String.format("%02d", min);

或者使用之前用过的SimpleDateFormat来获取星期几。

Calendar cal = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("mmHH");
dateFormat.setCalendar(cal); 
String mmHH = dateFormat.format(cal.getTime()));