簡體   English   中英

無法通過藍牙接收數據

[英]unable to receive data over 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應用接收數據。 連接時,服務器肯定在發送數據。

您需要使用

connectedThread.start(); 

在SUCCESS_CONNECT中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM