Android蓝牙输入/输出流:发送~1400字节,接收1008

Posted

技术标签:

【中文标题】Android蓝牙输入/输出流:发送~1400字节,接收1008【英文标题】:Android Bluetooth Input/Outputstream: Sent ~1400 bytes, received 1008 【发布时间】:2012-02-08 15:59:33 【问题描述】:

我正在尝试在两部 android 4.0.1 手机之间发送数据,当我发送数据时,我没有收到另一端的完整数组。

我的代码:

byte[] buffer = new byte[4096];
int bytes;

bytes = inputStream.read(buffer);
// bytes = 1008
// buffer[1008..4095] = 0 
// =================== other side =============================
byte[] message = Tools.concatByteArray(response, authorizationMessage);

// Debug tells me that the array has 1400 bytes
outputStream.write(message);
outputStream.flush();

调试中的消息是:

[102, -32, 27, 23, 31, 111, -43, 124, 105, -122, 89, 13, 80, 62, 107, -21, 44, -93, -24, 100, -28, 18, -10, -55, 8, -7, 95, -82, -127, -125, -13, -14, 82, 93, -112, 33, 5, -43, 82, -89, -60, 111, 61, 5, 65, 1, -105, -65, -77, -119, 126, -118, -67, 32, -115, 106, -10, 75, 44, 101, 60, -110, -71, -27, 44, -111, 83, 83, 99, -25, -71, 12, -101, -35, -79, -115, 99, 107, 21, 118, -52, -107, -51, -122, 106, 12, 43, 23, 111, -60, -79, -20, 57, -28, 99, -106, -87, 74, -18, 71, 44, -26, -6, 80, -124, 34, -69, 27, 46, -15, -97, -71, 84, 30, -121, -36, 65, -46, 66, -34, 69, -127, -16, 100, 114, 38, -110, -24, -34, 82, 69, -75, 108, 42, 83, 103, -14, -54, 10, 85, 107, -1, 42, 41, 106, 4, 65, 122, -101, -45, 19, 32, -110, 61, 53, 73, -97, -119, -63, 96, 55, -103, -59, -84, -35, -64, 47, -58, -38, 8, 9, 3, 82, -109, -78, 124, 69, -2, -74, -55, 72, -26, 33, -110, 92, -48, 87, 81, 39, 95, -45, 121, -82, -75, -79, -104, 93, 37, -118, 108, 20, -49, -96, 74, -120, -89, 110, -36, 115, 69, 98, 99, -21, 48, 112, -42, -124, 7, 18, 32, -126, -47, 87, -33, -5, -83, -2, 61, 85, -31, -18, 109, -75, 75, -16, 57, 67, 90, 98, 54, -27, -79, 111, -119, 67, 106, -3, 43, 56, -56, -115, -118, 15, -80, -83, -83, 101, 116, 46, -89, 113, -38, 33, -49, -114, -90, -20, 59, -50, 27, -98, 41, 116, -73, 48, 92, -104, -5, 74, -91, 75, 49, 35, 9, 65, -47, 35, 17, 27, -112, -64, -80, -104, 0, -109, 55, 89, 11, 42, -22, 86, -125, 127, 1, -27, -64, 21, -119, 15, 79, 27, -2, -18, 36, 77, 47, -32, 81, -117, -107, 112, 52, 125, -116, -83, -34, -15, 31, -37, 19, -65, 28, -93, -102, 12, 83, 108, 88, -5, 121, -107, -71, -3, 65, 18, -99, -92, 103, -9, 57, 90, 93, 29, 13, -14, 122, 126, -32, 54, 90, -126, 10, -123, -94, 46, -90, -11, -93, -75, 23, -18, 36, 51, 8, 115, -122, -44, -59, -99, -127, 33, -107, -116, 11, -94, -94, 92, -51, 94, -15, -72, 78, 82, 10, -14, -81, -121, -57, -55, 4, -64, 89, -69, -26, 24, 10, -27, -44, 80, 3, 11, -7, 127, 109, 3, -20, -52, 92, 31, -80, -13, -127, -73, -61, -11, -1, 110, 52, 100, -72, -73, 29, -98, -55, -39, 14, 48, 71, 79, -60, -29, -61, 97, -48, -59, -61, -117, 89, -73, 95, -4, -8, -57, 21, -26, 31, 37, 32, -72, 77, -41, -111, 99, 62, 98, 117, -63, 103, -124, -96, -111, -84, -1, 7, -20, 47, 57, 15, -59, 66, 53, 47, 97, -94, 7, -56, 124, 83, 72, 81, 101, 77, 96, -40, 67, 112, 112, 112, 16, -127, 72, -127, -98, -73, -125, 78, 84, 107, 125, 12, 121, 57, -17, 68, -41, -75, -28, -26, -3, 125, -97, 82, -53, 8, -124, -118, -1, 47, 19, 64, -18, 38, 45, 84, 85, -52, 105, 109, -61, 1, -32, 72, -107, 20, -102, -40, -47, 67, 111, -72, -92, 88, -30, 61, -82, 116, -2, -127, 35, 72, 35, -51, 24, 21, -87, 21, 63, -82, -2, 70, -70, -118, -64, 15, -44, 93, 108, 62, -51, 84, -15, 72, 7, 102, -107, -112, -7, -72, 84, -24, -22, 53, 68, 68, 38, -64, 71, 27, 73, 40, -86, 111, -56, 22, 49, 88, -119, -42, 9, 121, 82, 22, -35, 113, 5, -112, 12, -71, 17, -31, 92, -4, 48, -40, -44, -41, -60, -82, -116, 5, 117, -95, -28, -54, 45, -32, 39, -119, -25, 126, -40, 95, -97, -88, -58, -3, -81, -84, -82, -122, 122, -20, 11, 74, 85, 117, -75, 77, 39, -81, -72, 108, 123, 104, 65, -60, 81, -104, 41, 63, -62, 65, 43, -84, -64, 35, -63, 78, -111, 55, -1, 123, 73, 125, -92, -76, -81, -106, 103, -125, 105, -102, 112, -57, 69, 30, 13, 41, 65, -47, 121, -111, 94, -21, -16, -87, -39, 87, 91, 48, 116, -80, 100, 25, -64, 87, 63, -7, -6, -104, 41, -17, -13, 32, 106, -35, -35, -119, 107, 66, 116, 119, -68, -94, 116, 40, 117, -36, 89, 69, -47, -47, -78, 89, -16, -15, -25, -29, -53, -72, -105, -83, -67, 110, 52, 76, -92, 90, 93, -34, -18, 105, -92, -76, -55, -80, 117, -122, -94, -59, 100, 96, -85, -16, 116, 84, 55, -72, -55, -63, 54, 111, -1, 116, 6, 5, 23, -62, -109, 93, -127, 113, 96, 66, -16, 29, -95, -77, -13, 74, -90, 122, -37, -109, -40, 46, -108, -73, 45, 91, -23, -117, 85, 111, -23, 83, 3, -75, -51, -68, 102, 81, -122, -38, 12, -115, -34, 91, -29, -114, 124, -31, 93, -92, -124, -31, -17, 23, 65, -27, -6, 105, 0, -127, -110, 66, 59, -107, -95, 38, 69, -58, 106, 55, 105, 23, -19, -38, 118, 85, -5, -14, 126, -42, 84, -11, 121, 75, 72, -88, 29, 20, -107, -128, -109, -124, 120, -109, -91, -102, -56, -45, -103, 61, -71, 123, -66, -75, 58, -84, -61, 59, 12, 74, 78, -58, -73, -59, -14, -24, 78, 37, 123, -56, -98, 120, -25, -49, -115, 41, 66, -90, -12, -88, -62, 44, 81, -59, 67, 78, -57, -128, -123, -108, -94, -34, 108, 115, 78, 112, -95, 48, 80, 94, 6, -84, 46, -6, 31, -35, 7, 75, -57, -41, -3, 3, -67, -89, 15, -36, -111, 115, -126, -111, 61, 42, 84, -107, -90, -103, 44, -29, -21, 75, 96, 42, 34, 74, 117, -10, -63, 122, -119, -78, -50, -78, 39, 112, 10, 29, -3, 79, -119, 50, 121, 29, -96, -87, 114, 86, -65, -104, -80, -43, -123, 41, 106, -22, 2, -2, -38, -30, 97, 44, -51, -115, 75, 86, -117, 68, -69, 57, 1, 79, 12, 95, -112, 92, 75, 84, -116, -74, 57, 73, 46, -71, 61, 97, 115, -71, 112, -96, 33, 29, -105, -18, -89, -27, 35, 42, -113, -22, -78, -27, 22, 5, 45, -56, -70, -82, -18, -40, -80, -30, -91, -8, -77, -18, 115, -81, 101, -46, 82, -90, 30, -105, -99, -25, -103, -110, -50, -66, 84, -57, -5, -22, -128, -18, 69, 52, -11, -111, -24, -85, -1, 12, 72, -31, -97, 81, -29, 68, -80, 73, 7, -55, 45, -33, -73, 127, 2, 4, 56, 74, -19, 78, -73, 106, 58, 10, 10, -75, -60, -16, 98, 46, 82, -45, 15, 79, 90, 43, -53, 37, -25, -29, 50, -72, 120, 73, -20, 110, 96, -48, -38, -5, 39, 18, -47, -109, 89, -45, 74, 60, -30, 85, -16, -57, -24, 118, 44, -67, -38, 72, -124, -76, -124, -78, -18, 18, -55, -7, -23, -63, -82, -17, -58, 36, 103, 100, -116, -73, 85, -22, 43, 24, 41, 1, -123, 52, 125, 68, 5, -80, 21, -124, 72, -82, 121, 66, 103, 123, 82, -37, -79, -124, -67, 66, 15, -29, 59, -90, -48, -37, -12, 40, 20, 66, 28, -95, 28, 68, -26, 80, 74, 64, 122, 58, 2, 91, -45, 115, 87, 77, 107, 5, -1, 68, 119, 109, -113, 94, 48, -67, 6, -120, -76, -34, -49, -121, 103, -25, 60, 119, -11, -99, -74, -104, 123, 21, -100, 77, -51, -122, -32, 16, 74, -90, -21, -9, 20, 82, -112, -1, -116, -119, 68, -21, 3, -65, 101, 30, 14, 49, -34, 38, -13, 105, 21, -98, 23, 114, 119, -109, 26, 91, -41, 73, 90, -34, -119, -66, 47, 38, 57, 111, 34, -77, -74, -100, 56, 30, -74, 79, 100, 73, -69, 28, -100, 10, 96, 68, -109, -87, -53, -44, -13, 48, -16, 79, -52, -60, -105, 23, 73, 32, -60, -47, -29, -17, 32, 52, -105, 118, -66, 82, 0, -96, 14, 74, -61, -107, 13, 14, -30, -5, 34, 78, 112, 0, -114, 109, 71, -7, 33, 110, -76, -66, 19, -117, 0, -124, -93, -116, -94, 12, 86, 4, 106, -77, -31, 64, -37, -105, 2, -99, -37, 42, 9, 103, 12, -44, -59, -23, -121, 31, 2, 60, 28, -96, -77, 16, -78, -75, 43, 1, 108, 52, 11, -18, 11, -79, -2, 106, 111, -51, 16, 12, 108, 79, 18, 49, 112, -67, 6, 66, 124, 98, 34, -94, 10, 17, 125, 93, -57, -58, 34, 86, -100, 101, -94, -97, 117, 48, 68, 35, 14, -46, -75, 45]

and for buffer:
[102, -32, 27, 23, 31, 111, -43, 124, 105, -122, 89, 13, 80, 62, 107, -21, 44, -93, -24, 100, -28, 18, -10, -55, 8, -7, 95, -82, -127, -125, -13, -14, 82, 93, -112, 33, 5, -43, 82, -89, -60, 111, 61, 5, 65, 1, -105, -65, -77, -119, 126, -118, -67, 32, -115, 106, -10, 75, 44, 101, 60, -110, -71, -27, 44, -111, 83, 83, 99, -25, -71, 12, -101, -35, -79, -115, 99, 107, 21, 118, -52, -107, -51, -122, 106, 12, 43, 23, 111, -60, -79, -20, 57, -28, 99, -106, -87, 74, -18, 71, 44, -26, -6, 80, -124, 34, -69, 27, 46, -15, -97, -71, 84, 30, -121, -36, 65, -46, 66, -34, 69, -127, -16, 100, 114, 38, -110, -24, -34, 82, 69, -75, 108, 42, 83, 103, -14, -54, 10, 85, 107, -1, 42, 41, 106, 4, 65, 122, -101, -45, 19, 32, -110, 61, 53, 73, -97, -119, -63, 96, 55, -103, -59, -84, -35, -64, 47, -58, -38, 8, 9, 3, 82, -109, -78, 124, 69, -2, -74, -55, 72, -26, 33, -110, 92, -48, 87, 81, 39, 95, -45, 121, -82, -75, -79, -104, 93, 37, -118, 108, 20, -49, -96, 74, -120, -89, 110, -36, 115, 69, 98, 99, -21, 48, 112, -42, -124, 7, 18, 32, -126, -47, 87, -33, -5, -83, -2, 61, 85, -31, -18, 109, -75, 75, -16, 57, 67, 90, 98, 54, -27, -79, 111, -119, 67, 106, -3, 43, 56, -56, -115, -118, 15, -80, -83, -83, 101, 116, 46, -89, 113, -38, 33, -49, -114, -90, -20, 59, -50, 27, -98, 41, 116, -73, 48, 92, -104, -5, 74, -91, 75, 49, 35, 9, 65, -47, 35, 17, 27, -112, -64, -80, -104, 0, -109, 55, 89, 11, 42, -22, 86, -125, 127, 1, -27, -64, 21, -119, 15, 79, 27, -2, -18, 36, 77, 47, -32, 81, -117, -107, 112, 52, 125, -116, -83, -34, -15, 31, -37, 19, -65, 28, -93, -102, 12, 83, 108, 88, -5, 121, -107, -71, -3, 65, 18, -99, -92, 103, -9, 57, 90, 93, 29, 13, -14, 122, 126, -32, 54, 90, -126, 10, -123, -94, 46, -90, -11, -93, -75, 23, -18, 36, 51, 8, 115, -122, -44, -59, -99, -127, 33, -107, -116, 11, -94, -94, 92, -51, 94, -15, -72, 78, 82, 10, -14, -81, -121, -57, -55, 4, -64, 89, -69, -26, 24, 10, -27, -44, 80, 3, 11, -7, 127, 109, 3, -20, -52, 92, 31, -80, -13, -127, -73, -61, -11, -1, 110, 52, 100, -72, -73, 29, -98, -55, -39, 14, 48, 71, 79, -60, -29, -61, 97, -48, -59, -61, -117, 89, -73, 95, -4, -8, -57, 21, -26, 31, 37, 32, -72, 77, -41, -111, 99, 62, 98, 117, -63, 103, -124, -96, -111, -84, -1, 7, -20, 47, 57, 15, -59, 66, 53, 47, 97, -94, 7, -56, 124, 83, 72, 81, 101, 77, 96, -40, 67, 112, 112, 112, 16, -127, 72, -127, -98, -73, -125, 78, 84, 107, 125, 12, 121, 57, -17, 68, -41, -75, -28, -26, -3, 125, -97, 82, -53, 8, -124, -118, -1, 47, 19, 64, -18, 38, 45, 84, 85, -52, 105, 109, -61, 1, -32, 72, -107, 20, -102, -40, -47, 67, 111, -72, -92, 88, -30, 61, -82, 116, -2, -127, 35, 72, 35, -51, 24, 21, -87, 21, 63, -82, -2, 70, -70, -118, -64, 15, -44, 93, 108, 62, -51, 84, -15, 72, 7, 102, -107, -112, -7, -72, 84, -24, -22, 53, 68, 68, 38, -64, 71, 27, 73, 40, -86, 111, -56, 22, 49, 88, -119, -42, 9, 121, 82, 22, -35, 113, 5, -112, 12, -71, 17, -31, 92, -4, 48, -40, -44, -41, -60, -82, -116, 5, 117, -95, -28, -54, 45, -32, 39, -119, -25, 126, -40, 95, -97, -88, -58, -3, -81, -84, -82, -122, 122, -20, 11, 74, 85, 117, -75, 77, 39, -81, -72, 108, 123, 104, 65, -60, 81, -104, 41, 63, -62, 65, 43, -84, -64, 35, -63, 78, -111, 55, -1, 123, 73, 125, -92, -76, -81, -106, 103, -125, 105, -102, 112, -57, 69, 30, 13, 41, 65, -47, 121, -111, 94, -21, -16, -87, -39, 87, 91, 48, 116, -80, 100, 25, -64, 87, 63, -7, -6, -104, 41, -17, -13, 32, 106, -35, -35, -119, 107, 66, 116, 119, -68, -94, 116, 40, 117, -36, 89, 69, -47, -47, -78, 89, -16, -15, -25, -29, -53, -72, -105, -83, -67, 110, 52, 76, -92, 90, 93, -34, -18, 105, -92, -76, -55, -80, 117, -122, -94, -59, 100, 96, -85, -16, 116, 84, 55, -72, -55, -63, 54, 111, -1, 116, 6, 5, 23, -62, -109, 93, -127, 113, 96, 66, -16, 29, -95, -77, -13, 74, -90, 122, -37, -109, -40, 46, -108, -73, 45, 91, -23, -117, 85, 111, -23, 83, 3, -75, -51, -68, 102, 81, -122, -38, 12, -115, -34, 91, -29, -114, 124, -31, 93, -92, -124, -31, -17, 23, 65, -27, -6, 105, 0, -127, -110, 66, 59, -107, -95, 38, 69, -58, 106, 55, 105, 23, -19, -38, 118, 85, -5, -14, 126, -42, 84, -11, 121, 75, 72, -88, 29, 20, -107, -128, -109, -124, 120, -109, -91, -102, -56, -45, -103, 61, -71, 123, -66, -75, 58, -84, -61, 59, 12, 74, 78, -58, -73, -59, -14, -24, 78, 37, 123, -56, -98, 120, -25, -49, -115, 41, 66, -90, -12, -88, -62, 44, 81, -59, 67, 78, -57, -128, -123, -108, -94, -34, 108, 115, 78, 112, -95, 48, 80, 94, 6, -84, 46, -6, 31, -35, 7, 75, -57, -41, -3, 3, -67, -89, 15, -36, -111, 115, -126, -111, 61, 42, 84, -107, -90, -103, 44, -29, -21, 75, 96, 42, 34, 74, 117, -10, -63, 122, -119, -78, -50, -78, 39, 112, 10, 29, -3, 79, -119, 50, 121, 29, -96, -87, 114, 86, -65, -104, -80, -43, -123, 41, 106, -22, 2, -2, -38, -30, 97, 44, -51, -115, 75, 86, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...

所以我得到了前 1008 个字节的相同数据,之后,我什么都没有了。

知道如何解决这个问题吗?

【问题讨论】:

【参考方案1】:

您是否设置了任何缓冲区大小? 通常你会一直读到没有更多数据为止。

【讨论】:

是的,缓冲区初始化为 4096 字节【参考方案2】:

我不得不解决一个类似的错误。唯一相关的问题是在这个线程中,仍然没有解决。所以不得不自己寻找原因和解决方案:)。

原因是 Android 蓝牙似乎以 1008 字节为一组发送数据。 因此,您需要在 while 循环中从流中读取并连接接收到的字节。

以下是您的代码应该如何重写:

byte[] buffer = new byte[4096];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bytes;

while ((bytes = inputStream.read(buffer, 0, buffer.length)) > -1) 
          baos.write(buffer, 0, bytes);
          baos.flush();

byte[] message = baos.toByteArray();

// =================== other side =============================
byte[] message = Tools.concatByteArray(response, authorizationMessage);
outputStream.write(message);
outputStream.flush();
outputStream.close(); // <-- important! so the receiver knows that it is the end of data

【讨论】:

【参考方案3】:

请查看Managing a Connection 部分。如您所见;

您应该使用 专用线程 进行所有流读取和 写作。这很重要,因为 read(byte[])write(byte[]) 方法是阻塞调用read(byte[]) 将阻塞 直到有 从流中读取的东西write(byte[]) 通常不会 阻止,但如果远程设备不是,则可以阻止流量控制 足够快地调用 read(byte[]) 并且中间缓冲区是 满的。所以,你在线程中的主循环应该专门用于阅读 来自 InputStream。

当我开始编写我的 Reader Thread 时,InputStream 的 read(byte[]) 方法将阻塞执行,直到有所需的字节数。但事实并非如此。我发现它确实会阻塞,直到从流中读取“某些东西”(我之前错过的部分)。

考虑你的包裹是; 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09,您通过 OutputStream 写入发送它并刷新。您可能仍会在一次 read() 调用中获得 其中的一部分(甚至 1 字节 -> 0x01),并在下一次 read() 调用中获得其余部分(0x02 和其余部分) .在这种情况下,read() 的返回值将等于 1,incomingBytes[0] 将为 0x01,其他元素将为 0

【讨论】:

以及如何确定我们期望的消息何时完成?!如果我等待 9 个字节,我得到 1,然后是 8,但下一次是 9,下一次是 4,然后是 6,然后是 8,然后是正确的 9,依此类推......?!?!在我使用的协议中很有趣,我可以使用 crc 验证,但是当我收到如此切割的消息时,crc 没有意义,直到我确定我有一条完整的消息......所以我需要实现切割、移动、加入字节的逻辑数组和消息每秒发送 200 多次... :( 有什么帮助吗? @Ewoks 没错。我们使用的包大小超过 8 字节,后来,出于效率考虑,它扩展得更多。因此,除非您的包大小为 1 字节,否则您将永远不会后悔实现所需的逻辑。我实现了一个类似于功能类的流,收集所有字节,在收到所有包时将它们转换为固定大小的包,然后抛出完整的包来清理相关字节。这样,即使您在单个 inputStream.read() 中收到 1 字节或 2 个包(使用 8 字节的包大小很有可能),您也会得到 2 个包或根本没有......

以上是关于Android蓝牙输入/输出流:发送~1400字节,接收1008的主要内容,如果未能解决你的问题,请参考以下文章

外部附件框架中的输入和输出流是不是有最大大小?

Android Studio/Java - 具有小输入/输出大小的 RSA 加密

字节输入流/输出流-----PipedInputStream/PipedOutputStream

InputStream 没有收到 EOF

如何在android中将输入流转换为字节数组到字符串

Android BLE 广播定制