title: How to get back UUID from an Activity in Android?
body: <p>I am currently working on an App for Bluetooth communication, the App is basically done but it is not able to connect at all. I suspect it is the UUID. I did a search on internet on how to get a UUID for a device and I found this code: </p>
  
  <pre><code>import android.content.Context;
  import android.content.SharedPreferences;
  import android.provider.Settings.Secure;
  import android.telephony.TelephonyManager;
  
  import java.io.UnsupportedEncodingException;
  import java.util.UUID;
  
  public class DeviceUuidFactory {
  protected static final String PREFS_FILE = "device_id.xml";
  protected static final String PREFS_DEVICE_ID = "device_id";
  
  protected static UUID uuid;
  
  
  
  public DeviceUuidFactory(Context context) {
  
      if( uuid ==null ) {
          synchronized (DeviceUuidFactory.class) {
              if( uuid == null) {
                  final SharedPreferences prefs = context.getSharedPreferences( PREFS_FILE, 0);
                  final String id = prefs.getString(PREFS_DEVICE_ID, null );
  
                  if (id != null) {
                      // Use the ids previously computed and stored in the prefs file
                      uuid = UUID.fromString(id);
  
                  } else {
  
                      final String androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
  
                      // Use the Android ID unless it's broken, in which case fallback on deviceId,
                      // unless it's not available, then fallback on a random number which we store
                      // to a prefs file
                      try {
                          if (!"9774d56d682e549c".equals(androidId)) {
                              uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));
                          } else {
                              final String deviceId = ((TelephonyManager) context.getSystemService( Context.TELEPHONY_SERVICE )).getDeviceId();
                              uuid = deviceId!=null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")) : UUID.randomUUID();
                          }
                      } catch (UnsupportedEncodingException e) {
                          throw new RuntimeException(e);
                      }
  
                      // Write the value out to the prefs file
                      prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString() ).commit();
  
                  }
  
              }
          }
      }
  
  }
  
  
  /**
   * Returns a unique UUID for the current android device.  As with all UUIDs, this unique ID is "very highly likely"
   * to be unique across all Android devices.  Much more so than ANDROID_ID is.
   *
   * The UUID is generated by using ANDROID_ID as the base key if appropriate, falling back on
   * TelephonyManager.getDeviceID() if ANDROID_ID is known to be incorrect, and finally falling back
   * on a random UUID that's persisted to SharedPreferences if getDeviceID() does not return a
   * usable value.
   *
   * In some rare circumstances, this ID may change.  In particular, if the device is factory reset a new device ID
   * may be generated.  In addition, if a user upgrades their phone from certain buggy implementations of Android 2.2
   * to a newer, non-buggy version of Android, the device ID may change.  Or, if a user uninstalls your app on
   * a device that has neither a proper Android ID nor a Device ID, this ID may change on reinstallation.
   *
   * Note that if the code falls back on using TelephonyManager.getDeviceId(), the resulting ID will NOT
   * change after a factory reset.  Something to be aware of.
   *
   * Works around a bug in Android 2.2 for many devices when using ANDROID_ID directly.
   *
   * @see http://code.google.com/p/android/issues/detail?id=10603
   *
   * @return a UUID that may be used to uniquely identify your device for most purposes.
   */
  public UUID getDeviceUuid() {
      return uuid;
  }
  }
  </code></pre>
  
  <p>I want to know how to get the "uuid" back from this code? I created an activity for this code. do I use an Intent?</p>
  
  <p>Adding more info... I am basically modifying the BluetoothChatService Activity from the BluetoothChat Example from Android. and I want to add the UUID to that Activity</p>
  
  <pre><code>private class AcceptThread extends Thread {
      // The local server socket
      private final BluetoothServerSocket mmServerSocket;
      private String mSocketType;
      //Getting UUID
      Context c;
      UUID uuid = new DeviceUuidFactory(c).getDeviceUuid();
  
      public AcceptThread(boolean secure) {
          BluetoothServerSocket tmp = null;
          mSocketType = secure ? "Secure":"Insecure";
  
  
  
          // Create a new listening server socket
          try {
              if (secure) {
                  tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,
                      uuid);
              } else {
                  tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(
                          NAME_INSECURE, uuid);
              }
          } catch (IOException e) {
              Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e);
          }
          mmServerSocket = tmp;
      }
  
      public void run() {
          if (D) Log.d(TAG, "Socket Type: " + mSocketType +
                  "BEGIN mAcceptThread" + this);
          setName("AcceptThread" + mSocketType);
  
          BluetoothSocket socket = null;
  
          // Listen to the server socket if we're not connected
          while (mState != STATE_CONNECTED) {
              try {
                  // This is a blocking call and will only return on a
                  // successful connection or an exception
                  socket = mmServerSocket.accept();
              } catch (IOException e) {
                  Log.e(TAG, "Socket Type: " + mSocketType + "accept() failed", e);
                  break;
              }
  
              // If a connection was accepted
              if (socket != null) {
                  synchronized (Dataservice.this) {
                      switch (mState) {
                      case STATE_LISTEN:
                      case STATE_CONNECTING:
                          // Situation normal. Start the connected thread.
                          connected(socket, socket.getRemoteDevice(),
                                  mSocketType);
                          break;
                      case STATE_NONE:
                      case STATE_CONNECTED:
                          // Either not ready or already connected. Terminate new socket.
                          try {
                              socket.close();
                          } catch (IOException e) {
                              Log.e(TAG, "Could not close unwanted socket", e);
                          }
                          break;
                      }
                  }
              }
          }
          if (D) Log.i(TAG, "END mAcceptThread, socket Type: " + mSocketType);
  
      }
  
      public void cancel() {
          if (D) Log.d(TAG, "Socket Type" + mSocketType + "cancel " + this);
          try {
              mmServerSocket.close();
          } catch (IOException e) {
              Log.e(TAG, "Socket Type" + mSocketType + "close() of server failed", e);
          }
      }
  }
  </code></pre>