Ali_Waris
Ali_Waris

Reputation: 2382

Receiving data from raspberry pi in android via bluetooth

I am creating an android app using which I am going to connect to Raspberry pi over Bluetooth. The issue that I am able to send data to Raspberry pi and it is visible on the terminal (I am using OutputStream in android to send data), but whatever Raspberry pi is sending I am not able to get that in my InputStream.

I have read about using listenrfcomm to get the data sent by another device, but while using createrfcomm also, I have input as well output streams. I am confused as what to use and how to use.

NOTE: Using createrfcomm I am able to send data to Raspberry pi successfully. Only data reception from Rasperry pi is the part that's remaining.

Please advise accordingly.

Upvotes: 0

Views: 8930

Answers (1)

Nathan
Nathan

Reputation: 65

It would be easier to answer specifically with your code, but I found the API guide example helpful although slightly disjointed at first:

Have a thread to connect:

private class ConnectThread extends Thread {
   private final BluetoothSocket mmSocket;
   private final BluetoothDevice mmDevice;

   public ConnectThread(BluetoothDevice device) {
       // Use a temporary object that is later assigned to mmSocket,
       // because mmSocket is final
       BluetoothSocket tmp = null;
       mmDevice = device;

       // Get a BluetoothSocket to connect with the given BluetoothDevice
       try {
           // MY_UUID is the app's UUID string, also used by the server code
           tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
       } catch (IOException e) { }
       mmSocket = tmp;
   }

   public void run() {
       // Cancel discovery because it will slow down the connection
       mBluetoothAdapter.cancelDiscovery();

       try {
           // Connect the device through the socket. This will block
           // until it succeeds or throws an exception
           mmSocket.connect();
       } catch (IOException connectException) {
           // Unable to connect; close the socket and get out
           try {
               mmSocket.close();
           } catch (IOException closeException) { }
           return;
       }

       // Do work to manage the connection (in a separate thread)
       manageConnectedSocket(mmSocket);
   }

   /** Will cancel an in-progress connection, and close the socket */
   public void cancel() {
       try {
           mmSocket.close();
       } catch (IOException e) { }
   }

}

and a thread to listen and do the work:

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();
           } 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);
       } catch (IOException e) { }
   }

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

}

I assume if you can sent that you have BluetoothDevice, and BluetoothAdapter already, and can create and run the connect thread

mConnectThread = new ConnectThread(bluetoothAdapter.getRemoteDevice(deviceAddress));
mConnectThread.start();

In the example bytes is the data read, which is sent to the UI thread with mHandler.obtainMessage. This line can be edited to suit whatever you want to do with the received data.

Example comes from http://developer.android.com/guide/topics/connectivity/bluetooth.html

Upvotes: 1

Related Questions