时代逆子 2016-08-27 05:52 采纳率: 0%
浏览 1678

安卓蓝牙接收大量信息卡顿

做一个计步器,使用的是单片机加传感器来采集加速度值,采样频率是一秒钟20组数据(一组数据包含加速度三轴值),我
在安卓端使用套接字来监听蓝牙数据:

 private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;


        public ConnectedThread(BluetoothSocket socket) {
            Log.d(TAG, "create ConnectedThread");
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {

            byte[] buffer = new byte[11];
            String readMessage;
            int realCount = 0;
            // Keep listening to the InputStream while connected
            while (true) {
                try {

                    while (realCount < 11) {
                        realCount += mmInStream.read(buffer,realCount,11 - realCount);
                    }

                    Message msg = new Message();
                    Bundle data = new Bundle();
                    readMessage = bytesToHexString(buffer);
                    //data.putByteArray("ByteData",buffer);
                    data.putString("BTdata",readMessage);
                    msg.what = MainActivity.MESSAGE_READ;
                    msg.setData(data);
                    mHandler.sendMessage(msg);
//                    if (buffer[0] == DATA_START_BYTE ) {
//                        if (buffer[1] == ACCELDATA_START_BYTE) {
//
//                        }
//                    }

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

        public String bytesToHexString(byte[] bytes) {
            String result = "";
            for (int i = 0; i < bytes.length; i++) {
                String hexString = Integer.toHexString(bytes[i] & 0xFF);
                if (hexString.length() == 1) {
                    hexString = '0' + hexString;
                }
                result += hexString.toUpperCase();
            }
            return result;
        }

        /**
         * Write to the connected OutStream.
         * @param buffer  The bytes to write
         */
        public void write(byte[] buffer) {
            try {
                mmOutStream.write(buffer);

                // Share the sent message back to the UI Activity
                mHandler.obtainMessage(MainActivity.MESSAGE_WRITE, -1, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "Exception during write", e);
            }
        }

        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of connect socket failed", e);
            }
        }
    }

然后在mainActivity里面有一个handler来处理接收到的数据:

  //处理线程返回信息
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case MESSAGE_STATE_CHANGE://1
                    Toast.makeText(MainActivity.this, "change", Toast.LENGTH_LONG);
                    switch (msg.arg1) {
                        case BluetoothService.STATE_CONNECTED:
                            progressDialog.dismiss();
                            Toast.makeText(getApplication(),"已成功连接设备"+ mConnectedDeviceName,Toast.LENGTH_SHORT).show();
                            break;
                        case BluetoothService.STATE_CONNECTING:
                            progressDialog.setMessage("设备连接中...");
                            progressDialog.setCancelable(true);
                            progressDialog.show();
                            break;
                        case BluetoothService.STATE_LISTEN:
                        case BluetoothService.STATE_NONE:
                            progressDialog.cancel();
                            Toast.makeText(getApplication(),"设备未连接",Toast.LENGTH_SHORT).show();
                            break;
                    }
                    break;
                //发送了信息,显示在本地屏幕上(重写,显示结果即可)
                case MESSAGE_WRITE://2
                    //Toast.makeText(MainActivity.this, "发送成功", Toast.LENGTH_LONG);
                    break;
                //收到了信息,显示在本地屏幕上(重写,加入到通信录中)
                case MESSAGE_READ://3读取数据
                    //reciever = new TransactionReciever();
                    //byte[] readBuffer = new byte[11];
                    Bundle data = msg.getData();

                   // readBuffer = data.getByteArray("ByteData");

                    recieveStr = data.getString("BTdata");
                    Log.e("recieveString == ",recieveStr);

                    break;
                case MESSAGE_DEVICE_NAME://4
                    // save the connected device's name
                    mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
                    break;
                case MESSAGE_TOAST://5
                    Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }

    };

然后问题就来了,这样做程序就变得有些卡顿了,并且接收到的数据我打印出来永远的都一样的,比如:,无论我怎么晃动我的加速度器,都是永远一个值(串口显示器上数据是一直变化的在晃动的时候).
前辈们能指出一下我那儿做错了么,怎么处理卡顿呢?新手求指导。

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2017-02-23 19:15
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面