본문 바로가기

통신 예제 (앱)

1. transmit_data : 데이터 전송

public void transmit_data(BluetoothSPP ba, byte[] data)
{
    Log.d("send", "transmit data!!");

    short crc;
    short dataLen = (short)data.length;
    byte[] packet = new byte[5 + dataLen + 3];

    packet[0] = (STX);
    packet[1] = (byte)(dataLen);
    packet[2] = ((byte)(dataLen >> 8));
    packet[3] = ((byte)(dataLen));
    packet[4] = ((byte)(dataLen >> 8));

    System.arraycopy(data, 0, packet, 5, dataLen);

    crc = crc16_ccitt(data);

    packet[dataLen + 5] = (byte)(crc >> 8);
    packet[dataLen + 6] = (byte)(crc);
    packet[dataLen + 7] = ETX;

    ba.send(packet, false);
}

 

2. SET_TEXT

public void mSendText(String textBoxData, byte cmd) { // cmd == SET_TEXT
    short dataLen = 0;
    int inx = 0;
    byte[] sendData;
    List dataList = new ArrayList();
    byte[] textData = textBoxData.getBytes();
    RadioButton rbAction0 = (RadioButton) findViewById(R.id.rb_action_0);
    RadioButton rbAction1 = (RadioButton) findViewById(R.id.rb_action_1);
    RadioButton rbAction2 = (RadioButton) findViewById(R.id.rb_action_2);
    RadioButton rbAction3 = (RadioButton) findViewById(R.id.rb_action_3);
    RadioButton rbAction4 = (RadioButton) findViewById(R.id.rb_action_4);
    RadioButton rbAction5 = (RadioButton) findViewById(R.id.rb_action_5);
    TextView tv_action_time = (TextView) findViewById(R.id.tv_action_time);
    TextView tvFontSize = (TextView) findViewById(R.id.tvFontSize);

    byte textGroupCount = 3;
    if (rbAction0.isChecked() == true)
        dataLen = (short) ((textData.length + 1 + 3) * textGroupCount + 1 + 3 + 1 + 1); // page num(1) + Screen Color(3) + Text Size(1) + Font Color(3), Font Size(1) + Text Group Count(1)
    else
        dataLen = (short) ((textData.length + 1 + 3) * textGroupCount + 1 + 3 + 1 + 1 + 3); // page num(1) + Screen Color(3) + Text Size(1) + Font Color(3), Font Size(1) + Text Group Count(1) + Action(3 : action, time)

    sendData = new byte[dataLen + 1];

    byte page_num = 1;
    sendData[inx++] = cmd;  // cmd == SET_TEXT
    sendData[inx++] = 0;    //  옵션 값
    sendData[inx++] = page_num;
    sendData[inx++] = (byte) (textData.length >> 8);
    sendData[inx++] = (byte) (textData.length);
    System.arraycopy(textData, 0, sendData, inx, textData.length);
    inx = inx + textData.length;
    byte fontSize = (byte) Integer.parseInt(tvFontSize.getText().toString());
    sendData[inx++] = (byte) fontSize;

    // Font Color
    sendData[inx++] = (byte) (((colorFontSel & 0x00ff0000) >> 16) / 8);
    sendData[inx++] = (byte) (((colorFontSel & 0x0000ff00) >> 8) / 8);
    sendData[inx++] = (byte) (((colorFontSel & 0x000000ff) >> 0) / 8);

    // BG Color
    if (colorScrSel == "Red") {
        sendData[inx++] = (byte) 0xff; // RED
        sendData[inx++] = (byte) 0x00; // GREEN
        sendData[inx++] = (byte) 0x00; // BLUE
    } else if (colorScrSel == "Green") {
        sendData[inx++] = (byte) 0x00; // RED
        sendData[inx++] = (byte) 0xff; // GREEN
        sendData[inx++] = (byte) 0x00; // BLUE
    } else if (colorScrSel == "Blue") {
        sendData[inx++] = (byte) 0x00; // RED
        sendData[inx++] = (byte) 0x00; // GREEN
        sendData[inx++] = (byte) 0xff; // BLUE
    } else if (colorScrSel == "Black") {
        sendData[inx++] = (byte) 0x00; // RED
        sendData[inx++] = (byte) 0x00; // GREEN
        sendData[inx++] = (byte) 0x00; // BLUE
    }

    //action
    if (rbAction1.isChecked() == true)
        sendData[inx++] = (byte) ((MainActivity) MainActivity.context).CMD_ACT_LEFT_RIGHT;
    else if (rbAction2.isChecked() == true)
        sendData[inx++] = (byte) ((MainActivity) MainActivity.context).CMD_ACT_RIGHT_LEFT;
    else if (rbAction3.isChecked() == true)
        sendData[inx++] = (byte) ((MainActivity) MainActivity.context).CMD_ACT_TOP_DOWN;
    else if (rbAction4.isChecked() == true)
        sendData[inx++] = (byte) ((MainActivity) MainActivity.context).CMD_ACT_DOWN_TOP;
    else if (rbAction5.isChecked() == true)
        sendData[inx++] = (byte) ((MainActivity) MainActivity.context).CMD_ACT_BLINK;

    //action time
    int action_time = Integer.parseInt(tv_action_time.getText().toString());
    sendData[inx++] = (byte) ((action_time & 0x0000ff00) >> 8);
    sendData[inx++] = (byte) (action_time & 0x000000ff);

    sendData[inx++] = 0;   //효과
    sendData[inx++] = 0;   //효과 시간1
    sendData[inx++] = 0;   //효과 시간2

    sendData[inx++] = 0;   // 테두리 색상 r
    sendData[inx++] = 0;   // 테두리 색상 g
    sendData[inx++] = 0;   // 테두리 색상 b

    sendData[inx++] = 0;   // 효과 색상 변경 속도
    sendData[inx++] = 0;   // 눈꽃 시간 1
    sendData[inx++] = 0;   // 눈꽃 시간 2


    ((MainActivity) MainActivity.context).transmit_data(((MainActivity) MainActivity.context).bt, sendData);
}

 

 

3. SAVE_TEXT

public void mSendText(String textBoxData, byte cmd) { // cmd == SAVE_TEXT
    short dataLen = 0;
    int inx = 0;
    byte[] sendData;
    List dataList = new ArrayList();
    byte[] textData = textBoxData.getBytes();
    RadioButton rbAction0 = (RadioButton) findViewById(R.id.rb_action_0);
    RadioButton rbAction1 = (RadioButton) findViewById(R.id.rb_action_1);
    RadioButton rbAction2 = (RadioButton) findViewById(R.id.rb_action_2);
    RadioButton rbAction3 = (RadioButton) findViewById(R.id.rb_action_3);
    RadioButton rbAction4 = (RadioButton) findViewById(R.id.rb_action_4);
    RadioButton rbAction5 = (RadioButton) findViewById(R.id.rb_action_5);
    TextView tv_action_time = (TextView) findViewById(R.id.tv_action_time);
    TextView tvFontSize = (TextView) findViewById(R.id.tvFontSize);

    byte textGroupCount = 3;
    if (rbAction0.isChecked() == true)
        dataLen = (short) ((textData.length + 1 + 3) * textGroupCount + 1 + 3 + 1 + 1); // page num(1) + Screen Color(3) + Text Size(1) + Font Color(3), Font Size(1) + Text Group Count(1)
    else
        dataLen = (short) ((textData.length + 1 + 3) * textGroupCount + 1 + 3 + 1 + 1 + 3); // page num(1) + Screen Color(3) + Text Size(1) + Font Color(3), Font Size(1) + Text Group Count(1) + Action(3 : action, time)

    sendData = new byte[dataLen + 1];

    byte page_num = 1;
    sendData[inx++] = cmd;  // cmd == SAVE_TEXT
    sendData[inx++] = 0;    //  옵션 값
    sendData[inx++] = page_num;
    sendData[inx++] = (byte) (textData.length >> 8);
    sendData[inx++] = (byte) (textData.length);
    System.arraycopy(textData, 0, sendData, inx, textData.length);
    inx = inx + textData.length;
    byte fontSize = (byte) Integer.parseInt(tvFontSize.getText().toString());
    sendData[inx++] = (byte) fontSize;

    // Font Color
    sendData[inx++] = (byte) (((colorFontSel & 0x00ff0000) >> 16) / 8);
    sendData[inx++] = (byte) (((colorFontSel & 0x0000ff00) >> 8) / 8);
    sendData[inx++] = (byte) (((colorFontSel & 0x000000ff) >> 0) / 8);

    // BG Color
    if (colorScrSel == "Red") {
        sendData[inx++] = (byte) 0xff; // RED
        sendData[inx++] = (byte) 0x00; // GREEN
        sendData[inx++] = (byte) 0x00; // BLUE
    } else if (colorScrSel == "Green") {
        sendData[inx++] = (byte) 0x00; // RED
        sendData[inx++] = (byte) 0xff; // GREEN
        sendData[inx++] = (byte) 0x00; // BLUE
    } else if (colorScrSel == "Blue") {
        sendData[inx++] = (byte) 0x00; // RED
        sendData[inx++] = (byte) 0x00; // GREEN
        sendData[inx++] = (byte) 0xff; // BLUE
    } else if (colorScrSel == "Black") {
        sendData[inx++] = (byte) 0x00; // RED
        sendData[inx++] = (byte) 0x00; // GREEN
        sendData[inx++] = (byte) 0x00; // BLUE
    }

    //action
    if (rbAction1.isChecked() == true)
        sendData[inx++] = (byte) ((MainActivity) MainActivity.context).CMD_ACT_LEFT_RIGHT;
    else if (rbAction2.isChecked() == true)
        sendData[inx++] = (byte) ((MainActivity) MainActivity.context).CMD_ACT_RIGHT_LEFT;
    else if (rbAction3.isChecked() == true)
        sendData[inx++] = (byte) ((MainActivity) MainActivity.context).CMD_ACT_TOP_DOWN;
    else if (rbAction4.isChecked() == true)
        sendData[inx++] = (byte) ((MainActivity) MainActivity.context).CMD_ACT_DOWN_TOP;
    else if (rbAction5.isChecked() == true)
        sendData[inx++] = (byte) ((MainActivity) MainActivity.context).CMD_ACT_BLINK;

    //action time
    int action_time = Integer.parseInt(tv_action_time.getText().toString());
    sendData[inx++] = (byte) ((action_time & 0x0000ff00) >> 8);
    sendData[inx++] = (byte) (action_time & 0x000000ff);

    sendData[inx++] = 0;   //효과
    sendData[inx++] = 0;   //효과 시간1
    sendData[inx++] = 0;   //효과 시간2

    sendData[inx++] = 0;   // 테두리 색상 r
    sendData[inx++] = 0;   // 테두리 색상 g
    sendData[inx++] = 0;   // 테두리 색상 b

    sendData[inx++] = 0;   // 효과 색상 변경 속도
    sendData[inx++] = 0;   // 눈꽃 시간 1
    sendData[inx++] = 0;   // 눈꽃 시간 2


    ((MainActivity) MainActivity.context).transmit_data(((MainActivity) MainActivity.context).bt, sendData);
}

 

 

4. SET_DRAW

public void send_single_dot(byte xPos, byte yPos, int colorValue)
{
    byte red_col;
    byte green_col;
    byte blue_col;
    byte[] sendData = new byte[9];
    int inx = 0;
    byte page_num = 1;
    short dotLen = 1;

    red_col = (byte)(((colorValue & 0x00ff0000) >> 16) / 8);
    green_col = (byte)(((colorValue & 0x0000ff00) >> 8) / 8);
    blue_col = (byte)(((colorValue & 0x000000ff) >> 0) / 8);

    sendData[inx++] = SET_DRAW;
    sendData[inx++] = page_num;
    sendData[inx++] = (byte)(dotLen >> 8);
    sendData[inx++] = (byte)dotLen;

    sendData[inx++] = xPos;
    sendData[inx++] = yPos;
    sendData[inx++] = red_col;
    sendData[inx++] = green_col;
    sendData[inx++] = blue_col;
    transmit_data(bt, sendData);
}

 

5. SET_SCR_CLEAR

public void screen_clear()
{
    Button btnScrClear = findViewById(R.id.btnScrClear); //데이터 전송
    btnScrClear.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            byte[] data = new byte[1];
            data[0] = SET_SCR_CLEAR;
            transmit_data(bt, data);
            Drawable color_draw;

            for (int i=0; i<tb_row; i++){
                for(int j=0; j<tb_col; j++) {

                        color_draw = getResources().getDrawable(R.drawable.table_inside_gray);
                        tvCols[i][j].setBackground(color_draw);
                        matrix_array[i][j] = false;
                }
            }
        }
    });
}