android – 蓝牙SPP接收一些包框架可以丢失或?

android – 蓝牙SPP接收一些包框架可以丢失或?,第1张

概述我使用 android示例代码进行修改.只想收到包裹 但是,我的代码只在这里修改 private final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case MESSAGE_ST 我使用 android示例代码进行修改.只想收到包裹
但是,我的代码只在这里修改

private final Handler mHandler = new Handler() {    @OverrIDe    public voID handleMessage(Message msg) {        switch (msg.what) {        case MESSAGE_STATE_CHANGE:            if(D) Log.i(TAG,"MESSAGE_STATE_CHANGE: " + msg.arg1);            switch (msg.arg1) {            case BluetoothChatService.STATE_CONNECTED:                mTitle.setText(R.string.Title_connected_to);                mTitle.append(mConnectedDevicename);                mConversationArrayAdapter.clear();                break;            case BluetoothChatService.STATE_CONNECTING:                mTitle.setText(R.string.Title_connecting);                break;            case BluetoothChatService.STATE_ListEN:            case BluetoothChatService.STATE_NONE:                mTitle.setText(R.string.Title_not_connected);                break;            }            break;        case MESSAGE_WRITE:            byte[] writeBuf = (byte[]) msg.obj;            // construct a string from the buffer            String writeMessage = new String(writeBuf);            mConversationArrayAdapter.add(writeMessage);            break;        case MESSAGE_READ:            byte[] readBuf = (byte[]) msg.obj;            // construct a string from the valID bytes in the buffer            //String readMessage = new String(readBuf,msg.arg1);            //String readMessage = BytesTrans_fill.bytesToHexString(readBuf);            Log.d("read",BytesTrans.bytes2HexString(readBuf,msg.arg1));            String readMessage = BytesTrans.bytes2HexString(readBuf,msg.arg1);            ppV.setText(ppV.getText().toString() + readMessage + "★");            break;        case MESSAGE_DEVICE_name:            // save the connected device's name            mConnectedDevicename = msg.getData().getString(                    DEVICE_name);            Toast.makeText(getApplicationContext(),"Connected to "                           + mConnectedDevicename,Toast.LENGTH_SHORT).show();            break;        case MESSAGE_TOAST:            Toast.makeText(getApplicationContext(),msg.getData().getString(TOAST),Toast.LENGTH_SHORT).show();            break;        }    }};

和BluetoothChatService

public voID run() {        Log.i(TAG,"BEGIN mConnectedThread");        byte[] buffer = new byte[1024];        int bytes;        // Keep Listening to the inputStream while connected        while (true) {            try {                // Read from the inputStream                bytes = mmInStream.read(buffer);                // Send the obtained bytes to the UI Activity                mHandler.obtainMessage(BluetoothChat.MESSAGE_READ,bytes,-1,buffer)                        .sendToTarget();            } catch (IOException e) {                Log.e(TAG,"disconnected",e);                connectionLost();                break;            }        }    }

并添加此功能

package com.example.androID.BluetoothChat;public class BytesTrans {    public static String byte2HexString(byte b) {        String ret = "";            String hex = Integer.toHexString(b & 0xFF);            if (hex.length() == 1) {                hex = '0' + hex;            }            ret += hex.toupperCase()  + " ";        return ret;    }    public static String bytes2HexString(byte[] b,int count) {        String ret = "";        for (int i = 0; i < count; i++) {            String hex = Integer.toHexString(b[i] & 0xFF);            if (hex.length() == 1) {                hex = '0' + hex;            }            ret += hex.toupperCase() + " ";        }        return ret;    }    /*public static String String2byte(String b)    {        String[] ttt;        for (int i = 0; i < b.length(); i++)        {            for (int j= i; j<=i+1; j++)            {                ttt[i] = b;            }        }                   String ttmp = "";        String tmp = "";        ret += tmp;    }*/    public static int hexToTen(String b) {        int D2 = Integer.parseInt(b,16);        return D2;        }    }

但是这个程序有时甚至不能显示我的发送包的框架

我发送这样的包:

aa07210820001202140934390000000000000000000000000000000000000000000297c0fe6b

但有时收到包裹:

aa000297c0fe6b02131452470000000000000000000000000000000000000000000297c0fe6b

如何更改我的代码以接收完整包的框架

解决方法 您尚未显示任何蓝牙接口代码.但是,如果这主要基于BluetoothChat示例,那么BluetoothChat示例存在一个简单的问题:基本上,当从蓝牙套接字发出read()并放入字节数组时,该数组引用是正如您所做的那样,使用Handler发送到UI.实际的问题是,如果使用BluetoothChat示例以比输入速率更快的速度接收数据,那么您开始看到字符丢失或变得混乱,因为后续的read()在UI仍然覆盖时覆盖了数组读取数组以提取收到的最后一串字符.

因此,如果您的MESSAGE_WRITE对象包含对数组的引用,那么您将调用socket read(),这可能就是您丢失字符的原因.因此,尝试在Message中使用Arrays.copyOf()发送数组的副本.或者,也许你可以使用循环缓冲区安排.

当我使用BluetoothChat示例作为我的项目的基础时,我遇到了这个问题.我亲自解决问题(并消除了复制缓冲区等的需要)是实现一种机制,我将告诉蓝牙连接线程(包含阻塞套接字.read()的线程),方法调用的方法,我期望响应的字节数(幸运的是,我正在处理的协议允许知道响应长度).然后,连接线程仅在收到完整响应时发送消息,而不是向UI发送带有响应片段的多个消息.

总结

以上是内存溢出为你收集整理的android – 蓝牙SPP接收一些包框架可以丢失或?全部内容,希望文章能够帮你解决android – 蓝牙SPP接收一些包框架可以丢失或?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1126511.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存