title: Bluetooth data transfer between two devices not working
body: <p>I'm trying to make bluetooth connection between two devices but so far I haven't been able to make it work.<br>
  ATM I have one app on two phones, and depending on bluetooth address one should send information and the other should receive it.  </p>
  
  <p>I want it to work like this: open app on receiver phone that will wait and listen until something happens, then open app on transmitter phone which will connect to receiver and transmit the message.</p>
  
  <p>NOTICE: The app is currently running on Android 5 on one phone and 4.3 on the other.</p>
  
  <p>UPDATE: I've tried something new, with help from SeahawksRdaBest and am now getting new errors, so here is new code and logs:</p>
  
  <pre><code>package com.example.xxxxx;
  
  
  import java.io.IOException;
  import java.lang.reflect.InvocationTargetException;
  import java.util.UUID;
  
  import android.annotation.TargetApi;
  import android.bluetooth.BluetoothAdapter;
  import android.bluetooth.BluetoothDevice;
  import android.bluetooth.BluetoothSocket;
  import android.os.Build;
  import android.os.Bundle;
  import android.os.Handler;
  import android.os.Message;
  import android.support.v4.app.Fragment;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  
  @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
  public class BluetoothClass extends Fragment{
      BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      BluetoothDevice selectedDevice;
  
      protected static final int SUCCESS_CONNECT = 0;
      protected static final int MESSAGE_READ = 1;
  
      final int STATE_CONNECTED = 2;
      final int STATE_CONNECTING = 1;
      final int STATE_DISCONNECTED = 0;
  
      private final UUID MY_UUID = UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
      public byte[] completeData;
      double totalDistance = 0;
      int wheelRotations=0;
  
      private static final String address = "00:EE:BD:D1:66:45"; // Phone 1
      private static final String address2 = "18:E2:C2:31:08:AC"; // Phone 2
      private static final String TAG = "BTLOG";
  
      Handler mHandler = new Handler(){           
          public void handleMessage(Message msg){
              super.handleMessage(msg);
              switch(msg.what){
                  case SUCCESS_CONNECT:
                      Log.i(TAG, "CONNECTED");
                      break;
  
                  case MESSAGE_READ:
                      byte[] readBuf = (byte[])msg.obj;
                      Log.v(TAG, "READING: "+readBuf.toString());
              }
          }       
      };
  
      public void onCreate(Bundle savedInstance){
          super.onCreate(savedInstance);
      }
  
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
          View rootview = inflater.inflate(R.layout.bluetooth, container,false);      
  
          rootview.findViewById(R.id.connectDevice).setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  BluetoothDevice selectedDevice = null;
  
                  // If hardcoded device 1, then connect to device 2, or other way around
                  if (mBluetoothAdapter.getAddress().equals(address)) {
                      selectedDevice = selectedDevice(address2);
                  }
                  else if (mBluetoothAdapter.getAddress().equals(address2)) {
                      selectedDevice = selectedDevice(address);
                  }
                  mBluetoothAdapter.cancelDiscovery();
                  ConnectThread ct = new ConnectThread(selectedDevice);
                  ct.start();
              }
          });     
          return rootview;
      }   
  
      public void onActivityCreared(Bundle savedInstanceState){
          super.onActivityCreated(savedInstanceState);
      }
  
      public void onStart(){
          super.onStart();
      }       
  
      public void onResume(){
          super.onStart();    
      }
  
      public void onStop(){
          super.onStart();
      }
  
      // Get bluetooth device from known address
      public BluetoothDevice selectedDevice(String deviceAddress){
          mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
          BluetoothDevice device;     
          device = mBluetoothAdapter.getRemoteDevice(deviceAddress);
          return device;
      }
  
      public class ConnectThread extends Thread {
          private final BluetoothSocket mmSocket;
          private BluetoothSocket tmp;
          private final BluetoothDevice mmDevice;
  
          public ConnectThread(BluetoothDevice device) {
              mmDevice = device;
  
              try {
                  tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
              } catch (IOException e) {
                  Log.e(TAG, "Failed to create temporary socket");
                  e.printStackTrace();
              }
  
              mmSocket = tmp;
              Log.i(TAG, "Finished creating socket");
          }
  
          public void run() {
              mBluetoothAdapter.cancelDiscovery();
  
              Log.i(TAG, "Connecting through socket");
              try {
                  mmSocket.connect();
              } catch (IOException connectException) {
                  Log.e(TAG, "Connection through socket failed: "+connectException);
                  Log.i(TAG, "Trying fallback method");
  
                  try {
                      // fallback method for android &gt;= 4.2
                      tmp = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(mmDevice,1);
                  } catch (IllegalAccessException e) {
                      Log.e(TAG, "Failed to create fallback Illegal Access: "+e);
                      return;
                  } catch (IllegalArgumentException e) {
                      Log.e(TAG, "Failed to create fallback Illegal Argument: "+e);
                      return;
                  } catch (InvocationTargetException e) {
                      Log.e(TAG, "Failed to create fallback Invocation Target"+e);
                      return;
                  } catch (NoSuchMethodException e) {
                      Log.e(TAG, "Failed to create fallback No Such Method"+e);
                      return;
                  }
  
                  try {
                      // linked to tmp, so basicly a new socket
                      mmSocket.connect();
                  } catch (IOException e) {
                      Log.e(TAG, "Failed to connect with fallback socket: "+e);
                      return;
                  }
  
                  Log.i(TAG, "Succesfully connected with fallback socket");
                  mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
                  return;
              }
              Log.i(TAG, "Succesfully connected with original socket");
                  mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
  
          }       
      }
  
  }
  </code></pre>
  
  <p>LogCat logs:</p>
  
  <pre><code>11-23 14:03:56.281: I/BTLOG(12107): Finished creating socket
  11-23 14:03:56.281: I/BTLOG(12107): Connecting through socket
  11-23 14:03:56.286: D/BluetoothUtils(12107): isSocketAllowedBySecurityPolicy start : device null
  11-23 14:03:56.286: W/BluetoothAdapter(12107): getBluetoothService() called with no BluetoothManagerCallback
  11-23 14:03:59.696: E/BTLOG(12107): Connection through socket failed: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
  11-23 14:03:59.696: I/BTLOG(12107): Trying fallback method
  11-23 14:03:59.701: D/BluetoothUtils(12107): isSocketAllowedBySecurityPolicy start : device null
  11-23 14:03:59.701: W/BluetoothAdapter(12107): getBluetoothService() called with no BluetoothManagerCallback
  11-23 14:03:59.761: E/BTLOG(12107): Failed to connect with fallback socket: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
  </code></pre>