无法通过蓝牙接收数据

时间:2013-08-20 08:09:29

标签: java android bluetooth

我是android新手。我的应用程序使用蓝牙与嵌入式主板通信。 我正在使用android蓝牙聊天示例打开蓝牙套接字并启动一个线程。

    private class ConnectedThread extends Thread 
{
    public ConnectedThread(BluetoothSocket socket) 
    {
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

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

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                buffer = new byte[1024];
                bytes = mmInStream.read(buffer);
                Log.d("MR", "input stream :"+(new String(buffer)));
                // Send the obtained bytes to the UI activity
                mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
            } catch (IOException e) {
                break;
            }
        }
    }

    /* Call this from the main activity to send data to the remote device */
    public void write(byte[] bytes) {
        try {
            //a delay of 20ms occurs after each flush...
            mmOutStream.write(bytes);
            mmOutStream.flush();
        } catch (IOException e) { }
    }

    /* Call this from the main activity to shutdown the connection */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}   

我有一个消息处理程序

    Handler mHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) 
    {
        // TODO Auto-generated method stub
        Log.i(tag, "in handler");
        super.handleMessage(msg);    
        switch(msg.what){
        case SUCCESS_CONNECT:
            // DO something
            ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
            //Toast.makeText(getApplicationContext(), "CONNECT", 0).show();
            String s = "successfully connected";
            //connectedThread.write(s.getBytes());
            connectedThread.write(s.getBytes());
            Log.i(tag, "connected");
            break;
        case MESSAGE_READ:
            byte[] readBuf = (byte[])msg.obj;
            //String string = new String(readBuf, 0, msg.arg1);
            Toast.makeText(getApplicationContext(), "Test", 0).show();
            // Create the text view
            //TextView textView = (TextView)findViewById(R.id.rcvedMsg);
            //textView.setTextSize(40);
            //textView.setText(string);     
            break;
        case RECIEVE_MESSAGE:
            byte[] readmsgBuf = (byte[])msg.obj;
            String string = new String(readmsgBuf, 0, msg.arg1);
            Toast.makeText(getApplicationContext(), "Test", 0).show();
            // Create the text view
            //TextView textView = (TextView)findViewById(R.id.rcvedMsg);
            //textView.setTextSize(40);
            //textView.setText(string);     
            break;              
        }
    }

我无法从嵌入式设备接收任何数据。嵌入式设备正在运行rfcomm服务器,它可以从我的Android应用程序接收数据。在连接时,服务器肯定会发送数据。

1 个答案:

答案 0 :(得分:0)

你需要使用,

connectedThread.start(); 

在SUCCESS_CONNECT