Android蓝牙读取数据的问题

App接收下位机发送过来的数据,数据格式是16进制,比方说我要发送65 -24 -4 -42 66 39 39 -16这个序列.但是接收端出现了问题,
在接收蓝牙数据的线程里我不停他的接收数据,当接收到数据的时候我就向MainActivity发送一个Message,我把接收到的数据存放在Bundle里边,然后在MainActivity中有一个Handler来处理我的Message,把数据读取出来,但是读取出来的数据顺序是乱的,而且好像读取了两次,因为我一次只读取8个字节。
下面是关键部分的代码:

while (true)
            {
                try {

                    bytes = mmInStream.read(buffer);



                        Message msg = mHandler.obtainMessage();
                        msg.what = MainActivity.MESSAGE_READ;

                        Bundle bundle = new Bundle();

                        bundle.putByteArray("info", buffer);
                        msg.setData(bundle);
                        mHandler.sendMessage(msg);



                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    break;
                }
            }

MainActivtiy:

case MESSAGE_READ:
 Bundle bundle=msg.getData();
  byte[] array=bundle.getByteArray("info");
for (int i=0;i<array.length;i++){
            System.out.println(array[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
完全乱了。。。
首先确定不是大小端的问题
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android平台上,要获取蓝牙数据,首先需要进行以下几个步骤: 1. 权限配置:在AndroidManifest.xml文件中添加蓝牙权限的申请,例如: ```xml <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> ``` 2. 初始化蓝牙适配器:通过调用BluetoothAdapter的getDefaultAdapter()方法获取设备的蓝牙适配器实例,例如: ```java BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); ``` 3. 检查蓝牙状态:可以通过调用BluetoothAdapter的isEnabled()方法检查蓝牙是否已经开启,如未开启则通过调用enable()方法进行开启操作,例如: ```java if (!bluetoothAdapter.isEnabled()) { bluetoothAdapter.enable(); } ``` 4. 搜索蓝牙设备:通过调用BluetoothAdapter的startDiscovery()方法开始搜索周围的蓝牙设备,同时需要注册BroadcastReceiver接收搜索结果,例如: ```java BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter.startDiscovery()) { BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // 处理找到的蓝牙设备数据 } } }; IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(receiver, filter); } ``` 5. 连接和通信:通过BluetoothDevice对象可以获取设备的MAC地址和名称进行连接,连接成功后可以使用BluetoothSocket进行数据传输,例如: ```java BluetoothDevice device = bluetoothAdapter.getRemoteDevice(macAddress); BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid); socket.connect(); // 进行数据读取和写入操作 ``` 以上是获取蓝牙数据的基本步骤,其中涉及到的细节可以根据具体需求来进行调整和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值