我正在编写一个与SPP蓝牙设备通信的应用程序。
但我面临一个意想不到的问题:有时输入流会再次返回旧的已读取流。
这是一个示例流。在我的设备上,它应该在第二个字节上发送带有循环计数器的常规19字节数据包。每秒500次。
例如,
预期流:
ff00xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx81
ff01xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx81
ff02xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx81
ff03xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx81
...
实际流:(空白空间只是用于清晰可视化的填充。数据由Android开发人员模式中的HCI snoop日志记录功能收集)
02a0428c7c8252e28afac80200000081
ff6e8aea883e74228036a2f8f0100200000081
ff6f8aea9a3af6f4829ea486182c0200000081
ff708ae29436901a82eeaa9066a00200000081
ff718adc203a9ac884040a98bab20200000081
ff728af4523632908624188838040200000081
ff738af2a03ccc3c803ea88a7c580200000081
ff748ae45e4878667ed05a9622b60200000081
ff758aeafa40f862820858a0f6aa0200000081 <<
ff768aec643cb02880c8569296860200000081 <<
ff778aec6a408068843c6290daa80200000081 <<
ff788af02c3acae0865ede90c46c0200000081 <<
ff798af698408a9a80bc5c8e32b80200000081 <<
ff7a8af2f842a6a28438688cd4020200000081 <<
ff7b8ae2c63812c680c8088c9ed40200000081 <<
ff7f8ae2c63812c680c8088c9ed40200000081 <<
ff7f8aeae644b2907ef44a88282e0200000081 <<
ff008aec4e46202e8216b89226ce0200000081 <<
ff018ae6e442500080d2ec962ec60200000081 <<
ff028afade3e5ec87edc6296a0180200000081 <<
ff038c1ee64ad84c8622b89cfc620200000081 <<
ff048c1004429c4e82a82e9a0c440200000081 <<
ff058aeec8445c5280e47aa266c60200000081 <<
ff068afaa44a9c1880e02894f86c0200000081 <<
ff078c1858443280841874921ece0200000081 <<
ff088c0ad0442c3c8492b49aba980200000081 <<
ff098afec246046884a00a8c36d20200000081 <<
ff0a8af070466e807adaae82cc2c0200000081 <<
ff0b8aeeb840eeb27cca9afe24000200000081 <<
ff0c8afc104268708256fa86882a0200000081 <<
ff0d8c082a4034008ac6f89ab4760200000081 <<
ff0e8c0c784468708622f29c36ee0200000081 <<
ff0f8c04443ea2dc867c54a8ba900200000081 <<
ff108c0816446c1684629496928e0200000081 <<
ff118c101844dc607ed82a8a745c0200000081 <<
ff128afc364c5e9084c28a82f2f40200000081 <<
ff138ae87a3eb6ee82cace84286a0200000081
ff148af6983a8af486944e80a8260200000081
ff158c02ec443c7c80a6fa96c00a0200000081
ff168c06ac4252a2820eb2ac52b80200000081
ff178c005e3cf01488c4209886d20200000081
ff188af2da3a32e888e6e48cc03e0200000081
ff198aeebc4262c086e83a983c120200000081
ff1a8aec604ebebc846874866af40200000081
ff1b8af444462af47cea96944e9c0200000081
ff1c8af860449064800408a0d68c0200000081
ff1d8af42c3e4c3c84c42692c0120200000081
ff1e8c12a036d28e8e32e2981ae60200000081
ff1f8afef840b07e888000a63c4a0200000081
ff208af28e4e5c00881eaaa0f8680200000081
ff218aead040b6e8886ef89a1aae0200000081
ff228aec7442a8368a9ec496be9a0200000081
ff238ae078447ee08442c48ca6cc0200000081 <<
ff248af6f242f2d88a4eee92f2e60200000081 <<
ff258af8663c768c8aaaf69676da0200000081 <<
ff268af2b43c2852825c288e08660200000081 <<
ff278af6ce3cfefe846c0a92c6820200000081 <<
ff288af8624266f882085e92829a0200000081 <<
ff298aec76409a9482ec629a6c0e0200000081
ff2a8af6f04a26de8656a49864780200000081
ff2b8c1a8a406c08840c7ca208660200000081
ff2c8c16b83c5efe8a2c4690582e0200000081
ff2d8afeb83ef6a486e41296d6800200000081
ff2e8c0acc402ee680f42c98f0260200000081
ff2f8c140040f2c8809ef08e1c320200000081
ff308afc4e44e6fe82306c8e86ea0200000081
ff318c06ac42d69c84ec5894fea40200000081
ff328c104e3c8452844454a042820200000081
ff338c06dc4268107c669090a8620200000081
ff348afc0c420442803aea980c340200000081
ff358c00243c542888cea4a070780200000081
ff368af47e38100482905e96b2ea0200000081
ff378af6ce4412ec7e542290c68e0200000081
ff388c0ac444945680daea8cd6800200000081
ff398c0ea040a87880684c881ea40200000081
ff3a8c0694441a9880605894883a0200000081
ff3b8af82a3c96a280fadc8686880200000081
ff3c8c044a3e5c748636c296be8e0200000081
ff3d8c02824880348292908e7af20200000081
ff3e8c0c3a467c5a7adad0966aee0200000081
ff3f8c0816401e847c72e49650440200000081
ff408c042a461ed8805444964c440200000081
ff418af0062ea89648060200000081 **
ff568c087a46203c8636c88eee960200000081 **
ff578c0c3e4c9ec280def6901c040200000081 **
ff588aeab6445ecc828c609434600200000081 **
ff598af426426c4e8e34769cfc2a0200000081 **
ff5a8af2aa48bc428cc2aa8896bc0200000081 **
ff5b8afeda409c043a3c16fc84ec2e8ec2b80200000081 **
ff488afc0c40e020848af88cf4260200000081
ff498ae8004cd2c8867aaa909efc0200000081
ff4a8afed2423aec8288709e80060200000081
ff4b8c002e42000c7ed42c98bc200200000081
ff4c8c0c604ce838825854feb2f60200000081
ff4d8c042c56cef87ea6b48e12820200000081
ff4e8c00284872e4806afa96aeb40200000081
ff4f8afea03a32d2868c7090fc560200000081
ff508aee903cdefe8462fe8c2ef60200000081
ff518aec0e421ee2847c3e860c6c0200000081
484058648c52c090cc340200000081 **
ff678c04444052ba84d28c92b6da0200000081 **
ff688af47a3cf4288816d08a1ea20200000081 **
ff698c069c3af878840e8c8a60720200000081 **
ff6a8ae2964402c684e022965abc0200000081 **
ff6b8af46c3e4600044e5aa08486ce84e4780200000081 **
ff588c1474463abc867c6a902a960200000081
ff598c0ae84ad2c286246286cc180200000081
ff5a8c0afe44785e84c2f690d6e60200000081
ff5b8af6ba3e8aea864ac49e6e9e0200000081
ff5c8c085840f40e84340a9460080200000081
ff5d8c141c44f01684eade82986e0200000081
ff5e8afc344898128a06d8849a860200000081
ff5f8c06ae46c83a8662a890cee20200000081
ff608c0ef84a8c3c807c1c9a06ce0200000081
ff618af438400ed684802898
000081 <<**
ff768aec643cb02880c8569296860200000081 <<**
ff778aec6a408068843c6290daa80200000081 <<**
ff788af02c3acae0865ede90c46c0200000081 <<**
ff798af698408a9a80bc5c8e32b80200000081 <<**
ff7a8af2f842a6a28438688cd4020200000081 <<**
ff7b8ae414405c48865c0ea006880200000081 <<**
ff7c8adeb23826f282b0508e3c080200000081 <<**
ff7d8ae0143aeea4846ad692eafc0200000081 <<**
ff7e8ae2c63812c680c8088c9ed40200000081 <<**
ff7f8aeae644b2907ef44a88282e0200000081 <<**
ff008aec4e46202e8216b89226ce0200000081 <<**
ff018ae6e442500080d2ec962ec60200000081 <<**
ff028afade3e5ec87edc6296a0180200000081 <<**
ff038c1ee64ad84c8622b89cfc620200000081 <<**
ff048c1004429c4e82a82e9a0c440200000081 <<**
ff058aeec8445c5280e47aa266c60200000081 <<**
ff068afaa44a9c1880e02894f86c0200000081 <<**
ff078c1858443280841874921ece0200000081 <<**
ff088c0ad0442c3c8492b49aba980200000081 <<**
ff098afec246046884a00a8c36d20200000081 <<**
ff0a8af070466e807adaae82cc2c0200000081 <<**
ff0b8aeeb840eeb27cca9afe24000200000081 <<**
ff0c8afc104268708256fa86882a0200000081 <<**
ff0d8c082a4034008ac6f89ab4760200000081 <<**
ff0e8c0c784468708622f29c36ee0200000081 <<**
ff0f8c04443ea2dc867c54a8ba900200000081 <<**
ff108c0816446c1684629496928e0200000081 <<**
ff118c101844dc607ed82a8a745c0200000081 <<**
ff128afc364c5e <<**
fc463c282a8602a0a0f28e0200000081
ff7f8c00723a2420823ae0ac84700200000081
ff008c007c3cfaa68aaad2aa60020200000081
ff018c10523c829882ac769806d60200000081
ff028afacc406aa6846450966ec40200000081
ff038af832369a8088c6d896d2d60200000081
ff048af6a43ad28084c6be9c0ed20200000081
ff058af43a3866a8843a9294046a0200000081
ff068c044e3484568256cefc58300200000081
ff078c044038bc4e828e9c82c47c0200000081
ff088af406449eec860c06825afa0200000081
ff098af8063c847a88004694e8480200000081
ff0a8af444425c36862e8a94d4760200000081
ff0b8ae2b048c6c682529e8804500200000081
ff0c8aeef846ec7e820a949a5ee80200000081
ff0d8af40c403c7c84246896eade0200000081
ff0e8af07e3e5c2a862c2e8a8e8c0200000081
78447ee08442c48ca6cc0200000081 <<**
ff248af6f242f2d88a4eee92f2e60200000081 <<**
ff258af8663c768c8aaaf69676da0200000081 <<**
ff268af2b43c2852825c288e08660200000081 <<**
ff278af6ce3cfefe846c0a92c6820200000081 <<**
ff288af8624266 <<**
e6d23ec85a829aa68090600200000081
ff158afede46bace866c4492e8060200000081
ff168af43242d0448afc2698fa800200000081
ff178afc5244dec68892f2a2982c0200000081
ff188af44044042682d07a8ca6ae0200000081
ff198af80a3e4804826c5c8ceec20200000081
ff1a8aee8e3e1c0e82c04e94aad80200000081
ff1b8af2ea44980e7e16f08830000200000081
ff1c8af40c46781a80bc4e9226dc0200000081
ff1d8afaf04252c47cca8c8eb68a0200000081
ff1e8c087a40c03080743296f6ec0200000081
ff1f8c06a840b684825812a41c340200000081
ff208af6bc40881c8462e0961c180200000081
ff218af46c467c5e7ef4288a14440200000081
ff228af8603c103082f828843eda0200000081
ff238ae4743a0ebe86581e86a2a00200000081
ff248ae6aa3c06e87e96d48466fe0200000081
ff258af0763ec83e7c32dc80beb20200000081
ff268aec6c4268487e56c884bef00200000081
ff278a
**表示损坏的部分。
正如您所看到的,我无法按预期获得流。
但奇怪的是,标记的(&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
所以,我的假设是,输入数据的某些部分没有写入缓冲区,只有输入字节计数器增加。因此,read函数返回缓冲区中的旧字节而不写入数据(因此返回先前写入的字节而不是新数据的字节)。
更奇怪的是,这个问题只发生在一些设备上。
仅供我在目标板上用于蓝牙通信的芯片是Microchip bm78spps5mc2。
我的读取字节的代码如下(通过删除不相关的代码进行总结)
bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord( UUID.fromString("00001101-0000-1000-8000-00805F9B34FB" ) )
bluetoothSocket.connect()
var sis = BufferedInputStream( bluetoothSocket.inputStream, 1048576 )
var b = ByteArray( 16384 )
while ( !doStop ) {
var skipcnt = 0
do {
skipcnt++
var readbyte = sis.read()
if( readbyte != 255 )
Log.d( javaClass.name, readbyte.toString() + " - skipping...")
}while( readbyte != 255 && !doStop )
if( skipcnt != 1 )
Log.d( javaClass.name, "$skipcnt bytes skipped")
var i = 0
do {
sis.read( b, i, 1 )
i++
}while ( b[ i - 1 ] != 129.toByte() )
}
无论是直接使用BufferedInputStream还是bluetoothSocket.inputStream,结果都是一样的。
有谁知道原因和解决方案?