qq_40407443 2021-11-01 15:38 采纳率: 100%
浏览 29
已结题

安卓怎么显示蓝牙接收的数据


    public class Readtask extends Thread{
        @Override
        public void run() {
            byte[] buffer = new byte[1024];
            int bytes;
            InputStream inputStream;
            while(true){
                try{
                    inputStream = socket.getInputStream();
                    if((bytes = inputStream.read(buffer)) > 0){
                        byte[] buf_data = new byte[bytes];
                        for(int i = 0;i < bytes;i++){
                            buf_data[i] = buffer[i];
                        }
                        String s = new String(buf_data);
                        //TODO
                        //读取的数据进行处理
                        Message msg = mhandler.obtainMessage();
                        msg.what = DATA;
                        msg.obj = s;
                        mhandler.sendMessage(msg);
                    }
                }catch (IOException e){
                    setState(READ_FAILED);
                    Log.d("TAG", e.toString());
                    break;
                }
            }

            if(socket!= null){
                try{
                    socket.close();
                }catch (IOException e){
                    Log.d("TAG", e.toString());
                }
            }
        }
    }

    private final Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Bundle bundle = new Bundle();
            bundle = msg.getData();
            String str = bundle.getString("string");
            textView.setText(str);
            switch (msg.what){
                case BlueToothTool.CONNECT_FAILED:
                    Toast.makeText(MainActivity.this,"连接失败",Toast.LENGTH_SHORT).show();
                    break;
                case BlueToothTool.CONNECT_SUCCESS:
                    Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_SHORT).show();
                    listView.setVisibility(View.GONE);  //连接成功后移除listview 设置其他布局可见
                    status.setText("已连接");
                    address.setText(client.getAddress());
                    relativeLayout.setVisibility(View.VISIBLE);
                    break;
                case BlueToothTool.READ_FAILED:
                    Toast.makeText(MainActivity.this,"读取失败",Toast.LENGTH_SHORT).show();
                    break;
                case BlueToothTool.WRITE_FAILED:
                    Toast.makeText(MainActivity.this,"发送失败",Toast.LENGTH_SHORT).show();
                    break;
                case BlueToothTool.PIPEI_SUCCESS:
                    Toast.makeText(MainActivity.this,"正在连接",Toast.LENGTH_SHORT).show();
                    break;
                case BlueToothTool.PIPEI_FAILED:
                    Toast.makeText(MainActivity.this,"连接失败",Toast.LENGTH_SHORT).show();
                    break;
                case BlueToothTool.DATA:
                    Toast.makeText(MainActivity.this,"接收成功",Toast.LENGTH_SHORT).show();
                    break;


            }
        }
    };

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 11月9日
    • 创建了问题 11月1日

    悬赏问题

    • ¥20 escpos打印到CUPS打印机
    • ¥15 MATLAB读取TXT,并添加度分秒
    • ¥15 excel提示内存不足
    • ¥15 软件安装包用的是openinstall 在普通路由上有一些限速,怎么提速
    • ¥15 msgeq7根据音乐控制电机
    • ¥15 51单片机PN532刷卡原理图代码
    • ¥15 matlab如何不显示绘图而保存为能打开的fig图片?
    • ¥15 oracle数据库备份、
    • ¥15 关于Finetune模型,CUDA error: device-side assert triggered 报错
    • ¥15 能将阿里云上多个设备的信息能上传给小程序吗