0

この Bluetooth コードは別のブログから入手しました。接続をセットアップし、Bluetooth経由でデータを送信できました。受け取れません。私はしばらくの間、この同じコードを使用してきましたが、困惑しました。私は新しいので、助けが必要です。このアプリは、単純に 2 バイトを送信し、2 バイトを受信します。

   void beginListenForData()
        {
            final Handler handler = new Handler(); 
            final byte delimiter = 10; //This is the ASCII code for a newline character
            rcdata.setText("DataRC");           //NEW
            stopWorker = false;
            readBufferPosition = 0;
            readBuffer = new byte[1024];
            workerThread = new Thread(new Runnable()

            {
                public void run()
                {    
                rcdata.setText("DataRC0");          // Test NEW
                   while(!Thread.currentThread().isInterrupted() && !stopWorker)
                   {
                        try 
                        {
                            int bytesAvailable = mmInputStream.available();  

                            if(bytesAvailable > 0)
                            {
                                byte[] packetBytes = new byte[bytesAvailable];
                                mmInputStream.read(packetBytes);
                                rcdata.setText("DataRC1");          // Test NEW
                                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);
                                        final String data = new String(encodedBytes, "US-ASCII");
                                        readBufferPosition = 0;

                                        handler.post(new Runnable()
                                        {
                                            public void run()
                                            {
                                                rcdata.setText(data);
                                            }
                                        });
                                    }
                                    else
                                    {
                                        readBuffer[readBufferPosition++] = b;
                                    }
                                }
                            }
                        } 
                        catch (IOException ex) 
                        {
                            stopWorker = true;
                        }
                   }
                }
            });

            workerThread.start();
        }
4

1 に答える 1

0

非UIスレッドでリッスンすると思います.rcdataは編集またはテキストビューであると思います . 29

すべての rcdata.setText 呼び出しに対して

于 2013-08-14T15:57:27.267 に答える