Android藍芽讀取資料的問題

2021-07-25 17:36:59 字數 1860 閱讀 3195

在接收藍芽資料的執行緒裡我不停他的接收資料,當接收到資料的時候我就向mainactivity傳送乙個message,我把接收到的資料存放在bundle裡邊,然後在mainactivity中有乙個handler來處理我的message,把資料讀取出來,但是讀取出來的資料順序是亂的,而且好像讀取了兩次,因為我一次只讀取8個位元組。

下面是關鍵部分的**:

while (true)

catch (ioexception e) 

}

mainactivtiy:

case message_read:

bundle bundle=msg.getdata();

byte array=bundle.getbytearray("info");

for (int i=0;i

輸出的結果是這樣的:

10-24 17:07:46.151 21591-21591/com.example.lsj.bluetoothtest i/system.out: 66

10-24 17:07:46.151 21591-21591/com.example.lsj.bluetoothtest i/system.out: 39

10-24 17:07:46.151 21591-21591/com.example.lsj.bluetoothtest i/system.out: -16

10-24 17:07:46.161 21591-21591/com.example.lsj.bluetoothtest i/system.out: 65

10-24 17:07:46.161 21591-21591/com.example.lsj.bluetoothtest i/system.out: -24

10-24 17:07:46.161 21591-21591/com.example.lsj.bluetoothtest i/system.out: -4

10-24 17:07:46.171 21591-21591/com.example.lsj.bluetoothtest i/system.out: -42

10-24 17:07:46.171 21591-21591/com.example.lsj.bluetoothtest i/system.out: -42

10-24 17:07:46.241 21591-21591/com.example.lsj.bluetoothtest i/system.out: 39

10-24 17:07:46.241 21591-21591/com.example.lsj.bluetoothtest i/system.out: 39

10-24 17:07:46.251 21591-21591/com.example.lsj.bluetoothtest i/system.out: -16

10-24 17:07:46.251 21591-21591/com.example.lsj.bluetoothtest i/system.out: 65

10-24 17:07:46.251 21591-21591/com.example.lsj.bluetoothtest i/system.out: -24

10-24 17:07:46.251 21591-21591/com.example.lsj.bluetoothtest i/system.out: -4

10-24 17:07:46.251 21591-21591/com.example.lsj.bluetoothtest i/system.out: -42

10-24 17:07:46.251 21591-21591/com.example.lsj.bluetoothtest i/system.out: -42

完全亂了。。。

首先確定不是大小端的問題

Android藍芽接收分包資料

最近在開發乙個安卓和藍芽光譜儀裝置通訊的程式,用的是傳統藍芽,下位機藍芽傳送的光譜資料比較大,分成了15個包傳送。其中14個包大小290個byte,最後1個包大於100個byte,而且一整個資料報由資料頭 資料長度 資料 資料尾這幾個部分組成,所以接受的時候做了乙個判斷,定義了乙個大容量陣列,用笨方...

Android 的藍芽簡介

24 june 2015 on android 本文主要是翻譯了 google 的官方文件。android 提供預設的藍芽協議棧是 bluedroid,分為兩層 藍芽嵌入式系統 bte 和藍芽應用層 bta bte 層主要實現藍芽的核心功能,bta 層則主要負責和 anroid 框架通訊。andro...

Android藍芽低功耗傳送資料的方法

有關藍芽低功耗的開發指南網上示例很多,除了谷歌官方的demo 見此 很多大神也都分享過自己的 但是看了很多都是和官方demo一樣,只有接收資料,沒有傳送資料的方法,而有些低功耗裝置是需要手機端先傳送資料,才能接收裝置返回的資料,在此就簡單記錄下。如同讀取 接收 資料一樣,寫入 傳送 資料也要用到藍芽...