比较蓝牙收到的字符串

时间:2017-02-20 11:51:41

标签: android bluetooth

我正在使用一种方法从蓝牙接收值。

当我使用Log.e打印接收的值时,可以查看数据,但是当我尝试比较数据时,条件失败。

我正在尝试将当前数据与1和0进行比较,即使接收到的数据从0变为1,我也会继续接收0。我试图从“==”更改为“相等”,但它仍无法正常工作

我做错了什么?

public void  beginListenForData()
{


    final Handler handler = new Handler();
    final byte delimiter = 10; //This is the ASCII code for a newline character

    stopWorker = false;
    readBufferPosition = 0;
    readBuffer = new byte[200];
    workerThread = new Thread(new Runnable()
    {
        public void run()
        {
            while(!Thread.currentThread().isInterrupted() && !stopWorker)
            {
                try
                {
                    int bytesAvailable = mmInputStream.available();
                    if(bytesAvailable > 0)
                    {
                        byte[] packetBytes = new byte[bytesAvailable];
                        mmInputStream.read(packetBytes);
                        for(int i=0;i<bytesAvailable;i++)
                        {
                            byte b = packetBytes[i];
                            if(b == delimiter)
                            {
                                byte[] encodedBytes = new byte[readBufferPosition];
                                System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                                data = new String(encodedBytes, "US-ASCII");
                                readBufferPosition = 0;

                                handler.post(new Runnable()
                                {
                                    public void run()
                                    {
                                        if (data == "1") {
                                            anyObject = true;
                                            Log.e("Val 1", data);
                                        } else if(data == "0"){
                                            anyObject = false;
                                            Log.e("Val 0", data);
                                        }

                                    }
                                });
                            }
                            else
                            {
                                readBuffer[readBufferPosition++] = b;
                            }
                        }
                    }
                }
                catch (IOException ex)
                {
                    stopWorker = true;
                }
            }
        }
    });

    workerThread.start();
}

谢谢。

0 个答案:

没有答案