iT邦幫忙

0

收集BLE封包檢查是否有頭尾的小功能

  • 分享至 

  • twitterImage

做個背景描述, 關於上次的BLE封包問題, 對於資料接收最近寫了一個檢查封包頭尾的小功能做測試用, 是根據data Frame獨立撰寫一個的小程式先測試, 並無關於藍芽API傳輸的部分, 想問問這樣的程式邏輯與撰寫方法是否正確, 我找到東西會印出找到什麼的訊息, 也會印出沒找東西的訊息, 此外這方法我有在搭配有ble api傳輸的app上測試, 在此程式測試輸出是正常的,但到ble app就沒正常運作, 所以想上來問問各位大神, 我整個過程是不是有什麼問題, 目前先針對此測試程式的部分發問(已補上程式流程圖 且有將此測資成功判別的訊息畫面座標注,輸出數量是對的)

流程圖
https://ithelp.ithome.com.tw/upload/images/20211109/20143658D4ZujX9c1r.png

data Frame

data frame依序是---> 4 byte header(2,1,4,3) + 4 byte serial number + 4 byte length value(顯示資料多長 為實際資料長度+4) + unfixed length data(真正的資料) + 4 byte tail(-1,-1,-1,-1), 寫了一個簡單的介面

其中一個模擬測資

 /**
     * PATTERN_04 為3段不同長度的, 每段完整的資料, 但切開在 3 個封包內
     */
    final static byte[] PATTERN_04a = {0x02, 0x01, 0x04, 0x03
            , 0x01, 0x00, 0x00, 0x00
            , (byte) 0x0C, 0x00, 0x00, 0x00
            , 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00
    };

    final static byte[] PATTERN_04b = {
            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
            , 0x02, 0x01, 0x04, 0x03
            , 0x02, 0x00, 0x00, 0x00
            , (byte) 0x14, 0x00, 0x00, 0x00
            , 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02
    };

    final static byte[] PATTERN_04c = {
            0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00
            , (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
            , 0x02, 0x01, 0x04, 0x03
            , 0x03, 0x00, 0x00, 0x00
            , (byte) 0x1C, 0x00, 0x00, 0x00
            , 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00
            , (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
    };
    //endregion

下方為程式碼(主程式)

add(PATTERN_04a);
        add(PATTERN_04b);
        add(PATTERN_04c);

        int size = rawDataList.size();

        for (int m = 0; m < size; m++) {
            if (m + 1 < size && m + 2 < size && m + 3 < size) {
                if ((rawDataList.get(m) & 0xFF) == 2 && (rawDataList.get(m+1) & 0xFF) == 1 && (rawDataList.get(m+2) & 0xFF) == 4 && (rawDataList.get(m+3) & 0xFF) == 3)
                {
                    len = (rawDataList.get(m+8) & 0xFF);
                    len2 = (rawDataList.get(m+9) & 0xFF);
                    len3 = (rawDataList.get(m+10) & 0xFF);
                    len4 = (rawDataList.get(m+11) & 0xFF);

                    dataLen = len + len2 + len3 + len4 - 4;

                    if (m+dataLen+12 < size && m+dataLen+13 < size && m+dataLen+14 < size && m+dataLen+15 < size) {
                        if ((rawDataList.get(m+dataLen+12)) == -1 && rawDataList.get(m+dataLen+13) == -1 && rawDataList.get(m+dataLen+14) == -1 && rawDataList.get(m+dataLen+15) == -1)
                        {
                            System.out.println("Yes you found head and tail.");
                        }
                        else{
                            System.out.println("You found head but didn't find tail.");
                        }
                    }

                }
                else {
                    System.out.println("No head no tail.");
                }
            }
        }

輸出1
https://ithelp.ithome.com.tw/upload/images/20211109/20143658GfLqI3MxUi.png

輸出2
https://ithelp.ithome.com.tw/upload/images/20211109/20143658onEcTqQzeW.png

輸出3
https://ithelp.ithome.com.tw/upload/images/20211109/20143658XptCc3vtZR.png

輸出4
https://ithelp.ithome.com.tw/upload/images/20211109/20143658VhlQdYiIiG.png

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答