【问题标题】:Writing bytes from Android to remote Bluetooth device using Handler使用 Handler 将字节从 Android 写入远程蓝牙设备
【发布时间】:2014-03-06 17:19:34
【问题描述】:

大家知道为什么当我尝试运行下面的代码时出现“无法在未调用 Looper.prepare() 的线程内创建处理程序”的错误吗?我添加了处理程序,以便在程序位于 while(true) 循环内时将消息从手机写入远程蓝牙设备。

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

        private volatile Looper MyLooper;

        public static final int message1 = 1;
        public static final int message2 = 0;

        //The Handler that gets information back from the Socket            
        public Handler mHandler = new Handler(){

            @Override
            public void handleMessage(Message msg){
                            Looper.prepare();

                switch(msg.what){
                case message1:
                                 byte[] writeBuf = (byte[]) msg.obj;
                                 String writeMessage = new String(writeBuf);
                                 write(writeMessage);
                                 break;

                case message2:
                         byte[] readBuf = (byte[])msg.obj;
                                 String readMessage = new String(readBuf);
                                 write(readMessage);
                     break;
                     }


             MyLooper = Looper.myLooper();
                     Looper.loop();
                     MyLooper.quit();
        }
    };

        public ConnectedThread(BluetoothSocket socket){
            super();
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            try{
                tmpIn = socket.getInputStream();
                tmpOut=socket.getOutputStream();
            }catch(IOException e){
                e.printStackTrace();
            }
            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run(){
            byte[] buffer = new byte[1024]; // buffer store for the stream
            int bytes;// bytes returned from read()

            //Keep listening to the InputStream until an exception occurs
            while (true) {
                 try {
                    if(mmInStream.available()>0)
                     {
                         try {

                             bytes = mmInStream.read(buffer);
                         } catch (IOException e) {
                             System.out.println("IO Exception occurred");
                             e.printStackTrace();
                             break;
                         }
                     }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

            //write to OutputStream
            public void write(String message){
                byte[] msgBuffer = message.getBytes();
                try {
                    mmOutStream.write(msgBuffer);
                    mmOutStream.flush();
                } catch (IOException e) {
                    Log.d("Connected Thread", "...Error data send: " + e.getMessage() + "...");
                    e.printStackTrace();
                }
            }

在另一个活动中,我使用了“mHandler.sendMessage(m2);”向处理程序发送消息以写入远程蓝牙设备。但是,它不起作用,并且发生了上述错误。

【问题讨论】:

    标签: android bluetooth android-handler


    【解决方案1】:

    我看到你试图写字节,但你的函数write()String message,先修复它。

    【讨论】:

    • 你是什么意思?即使我已转换为字节,仍然无法正常工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2013-07-10
    • 2019-08-02
    • 1970-01-01
    • 2012-03-14
    相关资源
    最近更新 更多