使用Handler将字节从Android写入远程蓝牙设备

时间:2014-02-09 12:48:45

标签: android bluetooth android-handler

嗨,任何人都知道为什么在我尝试运行下面的代码时出现“无法在线程中创建未调用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);”向处理程序发送消息以写入远程蓝牙设备。但是,它不起作用,并且发生了上述错误。

1 个答案:

答案 0 :(得分:-2)

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