Java 无法通过蓝牙接收数据

Java 无法通过蓝牙接收数据,java,android,bluetooth,Java,Android,Bluetooth,我是android新手。我的应用程序使用蓝牙与嵌入式电路板进行通信。 我正在使用android蓝牙聊天示例打开蓝牙套接字并启动线程 private class ConnectedThread extends Thread { public ConnectedThread(BluetoothSocket socket) { mmSocket = socket; InputStream tmpIn = null; Outpu

我是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应用程序接收数据。连接时,服务器肯定在发送数据。

您需要使用

connectedThread.start(); 
成功与否