2016-05-09 10 views
0

現在、2つのペアになっているデバイスを接続し、互いにデータを送信できるアプリを作成しています。私はAndroidの開発には非常に新しいですし、私はいくつかの問題を抱えています、私はデータ伝送を働かせるように見えません。AndroidスタジオでBluetooth経由でデータを読み書きする

私はこのチュートリアルを次されています:

-The -The ConnectedThread

btnSendData.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String message = editTxtMsg.getText().toString(); 

      if (connectedThread != null) { 
       connectedThread.write(message.getBytes()); 
      } 
     } 
    }); 

の上に文字列を送信することを意図し、 "送信" ボタンを: http://developer.android.com/guide/topics/connectivity/bluetooth.html

は、ここに私のコードの一部です

private class ConnectedThread extends Thread { 
    private final BluetoothSocket mmSocket; 
    private final InputStream mmInStream; 
    private final OutputStream mmOutStream; 

    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 = new byte[1024]; // 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 
       bytes = mmInStream.read(buffer); 
       // Send the obtained bytes to the UI activity 
       mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget(); 

       /*Bundle data =new Bundle(); 
       //data.p 
       Message m =mHandler.obtainMessage(); 

       m.setData(data); 
       mHandler.sendMessage(m);*/ 

      } catch (IOException e) { 
       break; 
      } 
     } 
    } 

    /* Call this from the main activity to send data to the remote device */ 
    public void write(byte[] bytes) { 
     try { 
      mmOutStream.write(bytes); 

      mHandler.obtainMessage(MESSAGE_WRITE, -1, -1, buffer).sendToTarget(); 
     } catch (IOException e) { } 
    } 

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

-ハンドラ

private final Handler mHandler = new Handler() 
{ 
    @Override 
    public void handleMessage(Message msg) 
    { 
     switch (msg.what) 
     { 
      case MESSAGE_READ: 
       byte[] readBuffer = (byte[]) msg.obj; 
       String readMessage = new String(readBuffer, 0, msg.arg1); 
       //arrayAdapterMsg.add(btDevice.getName() + ": " + readMessage); 
       aryLstMsg.add(btDevice.getName() + ": " + readMessage); 

       arrayAdapterMsg.notifyDataSetChanged(); 

       break; 

      case MESSAGE_WRITE: 
       byte[] writeBuffer = (byte[]) msg.obj; 
       String writeMessage = new String(writeBuffer); 
       //arrayAdapterMsg.add("Me: " + "test"); 
       aryLstMsg.add("Me: " + writeMessage); 

       arrayAdapterMsg.notifyDataSetChanged(); 

       break; 
     } 
    } 
}; 

私はなぜそれが機能していないのかわかりません、他の誰もがアイデアを持っていますか?

答えて

0

チェックブルートゥース許可 は、それはあなたが()、ライト()関数からバッファにアクセスしようとしているが、それが実行内で定義されて

<uses-permission android:name="android.permission.BLUETOOTH" /> 


<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
0

でなければなりません。それをグローバルに定義しようとするか、適切なパラメータを渡してwrite()を実行から呼び出してください。

関連する問題