Skip to content Skip to sidebar Skip to footer

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 termi

Solution 1:

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:

privateclassConnectThreadextendsThread {
   privatefinal BluetoothSocket mmSocket;
   privatefinal BluetoothDevice mmDevice;

   publicConnectThread(BluetoothDevice device) {
       // Use a temporary object that is later assigned to mmSocket,// because mmSocket is finalBluetoothSockettmp=null;
       mmDevice = device;

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

   publicvoidrun() {
       // 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 outtry {
               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 */publicvoidcancel() {
       try {
           mmSocket.close();
       } catch (IOException e) { }
   }

}

and a thread to listen and do the work:

privateclassConnectedThreadextendsThread {
   privatefinal BluetoothSocket mmSocket;
   privatefinal InputStream mmInStream;
   privatefinal OutputStream mmOutStream;

   publicConnectedThread(BluetoothSocket socket) {
       mmSocket = socket;
       InputStreamtmpIn=null;
       OutputStreamtmpOut=null;

       // Get the input and output streams, using temp objects because// member streams are finaltry {
           tmpIn = socket.getInputStream();
           tmpOut = socket.getOutputStream();
       } catch (IOException e) { }

       mmInStream = tmpIn;
       mmOutStream = tmpOut;
   }

   publicvoidrun() {
       byte[] buffer = newbyte[1024];  // buffer store for the streamint bytes; // bytes returned from read()// Keep listening to the InputStream until an exception occurswhile (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 */publicvoidwrite(byte[] bytes) {
       try {
           mmOutStream.write(bytes);
       } catch (IOException e) { }
   }

   /* Call this from the main activity to shutdown the connection */publicvoidcancel() {
       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 = newConnectThread(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

Post a Comment for "Receiving Data From Raspberry Pi In Android Via Bluetooth"