1. Are you ready for the Galaxy S20? Here is everything we know so far!

Bluetooth app

Discussion in 'Android Apps & Games' started by hantescu, Jan 21, 2016.

  1. hantescu

    hantescu Lurker
    Thread Starter

    Hi!
    I have some problems with my bluetooth app. I made the setup for a bluetooth server on a raspberry pi and i was able to send data to it with my app, but I don't know how to receive data from the server. The server works properly, I tried with the BlueTerm 2 app and I can receive data from it. Can someone help me to implement the receiver in my app?
    My code below:
    Code (Text):
    1. package com.example.bluetooth1;
    2.  
    3. import java.io.IOException;
    4. import java.io.OutputStream;
    5. import java.lang.reflect.Method;
    6. import java.util.UUID;
    7. import com.example.bluetooth1.R;
    8. import android.app.Activity;
    9. import android.bluetooth.BluetoothAdapter;
    10. import android.bluetooth.BluetoothDevice;
    11. import android.bluetooth.BluetoothSocket;
    12. import android.content.Intent;
    13. import android.os.Build;
    14. import android.os.Bundle;
    15. import android.util.Log;
    16. import android.view.View;
    17. import android.view.View.OnClickListener;
    18. import android.widget.Button;
    19. import android.widget.Toast;
    20. public class MainActivity extends Activity {
    21.   private static final String TAG = "bluetooth1";
    22.  
    23.   Button btnOn, btnOff;
    24.  
    25.   private BluetoothAdapter btAdapter = null;
    26.   private BluetoothSocket btSocket = null;
    27.   private OutputStream outStream = null;
    28.  
    29.   // SPP UUID service
    30.   private static final UUID MY_UUID = UUID.fromString("94f39d29-7d6d-437d-973b-fba39e49d4ee");
    31.   // MAC-address of Bluetooth module (you must edit this line)
    32.   private static String address = "00:1A:7D:DA:71:14";
    33.  
    34.   /** Called when the activity is first created. */
    35.   @Override
    36.   public void onCreate(Bundle savedInstanceState) {
    37.     super.onCreate(savedInstanceState);
    38.     setContentView(R.layout.activity_main);
    39.     btnOn = (Button) findViewById(R.id.btnOn);
    40.     btnOff = (Button) findViewById(R.id.btnOff);
    41.    
    42.     btAdapter = BluetoothAdapter.getDefaultAdapter();
    43.     checkBTState();
    44.     btnOn.setOnClickListener(new OnClickListener() {
    45.       public void onClick(View v) {
    46.         sendData("esti un prost");
    47.         //Toast.makeText(getBaseContext(), "Turn on LED", Toast.LENGTH_SHORT).show();
    48.       }
    49.     });
    50.     btnOff.setOnClickListener(new OnClickListener() {
    51.       public void onClick(View v) {
    52.         sendData("cel mai prost");
    53.        // Toast.makeText(getBaseContext(), "Turn off LED", Toast.LENGTH_SHORT).show();
    54.       }
    55.     });
    56.   }
    57.  
    58.   private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
    59.       if(Build.VERSION.SDK_INT >= 10){
    60.           try {
    61.               final Method  m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
    62.               return (BluetoothSocket) m.invoke(device, MY_UUID);
    63.           } catch (Exception e) {
    64.               Log.e(TAG, "Could not create Insecure RFComm Connection",e);
    65.           }
    66.       }
    67.       return  device.createRfcommSocketToServiceRecord(MY_UUID);
    68.   }
    69.  
    70.   @Override
    71.   public void onResume() {
    72.     super.onResume();
    73.  
    74.     // Set up a pointer to the remote node using it's address.
    75.     BluetoothDevice device = btAdapter.getRemoteDevice(address);
    76.     try {
    77.         btSocket = createBluetoothSocket(device);
    78.     } catch (IOException e1) {
    79.         errorExit("Fatal Error", "In onResume() and socket create failed: " + e1.getMessage() + ".");
    80.     }
    81.    
    82.  
    83.     btAdapter.cancelDiscovery();
    84.     try {
    85.       btSocket.connect();
    86.     } catch (IOException e) {
    87.       try {
    88.         btSocket.close();
    89.       } catch (IOException e2) {
    90.       }
    91.     }
    92.  
    93.     try {
    94.       outStream = btSocket.getOutputStream();
    95.     } catch (IOException e) {
    96.       errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
    97.     }
    98.   }
    99.  
    100.  
    101.   private void checkBTState() {
    102.     // Check for Bluetooth support and then check to make sure it is turned on
    103.     // Emulator doesn't support Bluetooth and will return null
    104.     if(btAdapter==null) {
    105.       errorExit("Fatal Error", "Bluetooth not support");
    106.     } else {
    107.       if (btAdapter.isEnabled()) {
    108.         Log.d(TAG, "...Bluetooth ON...");
    109.       } else {
    110.         //Prompt user to turn on Bluetooth
    111.         Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    112.         startActivityForResult(enableBtIntent, 1);
    113.       }
    114.     }
    115.   }
    116.   private void errorExit(String title, String message){
    117.     Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show();
    118.     finish();
    119.   }
    120.   private void sendData(String message) {
    121.     byte[] msgBuffer = message.getBytes();
    122.  
    123.     try {
    124.       outStream.write(msgBuffer);
    125.     } catch (IOException e) {
    126.       String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
    127.       if (address.equals("00:00:00:00:00:00"))
    128.         msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 35 in the java code";
    129.           msg = msg +  ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";
    130.      
    131.           errorExit("Fatal Error", msg);      
    132.     }
    133.   }
    134. }
    135.  
    136.  
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Bluetooth app
  1. LouisT0629
    Replies:
    3
    Views:
    733
  2. CFresh
    Replies:
    5
    Views:
    672
  3. Barry396
    Replies:
    2
    Views:
    663
  4. FairyTalePink
    Replies:
    0
    Views:
    489
  5. Tizfaver
    Replies:
    3
    Views:
    1,061
  6. Sunny Rio
    Replies:
    17
    Views:
    957
  7. Sunny Rio
    Replies:
    3
    Views:
    626
  8. John Klug
    Replies:
    2
    Views:
    1,270
  9. App Update
    Replies:
    0
    Views:
    618
  10. dorlow
    Replies:
    3
    Views:
    978

Share This Page

Loading...