public long addOrUpdateBluetooth(Bluetooth bluetooth) {
   ContentValues content = new ContentValues();
   content.put("status", bluetooth.getStatus());
   content.put("name", bluetooth.getName());
   content.put("address", bluetooth.getAddress());
   content.put("used", bluetooth.getUsed());
   return addOrUpdate(bluetooth.getId(), Bluetooth.NAME, content);
 }
 private void importBluetooth(SQLiteDatabase db) {
   SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
   Map<String, ?> map = prefs.getAll();
   for (Map.Entry<String, ?> entry : map.entrySet()) {
     if (entry.getKey().startsWith("bt.devices.")) {
       Bluetooth bluetooth = new Bluetooth((String) entry.getValue(), "");
       ContentValues content = new ContentValues();
       content.put("name", bluetooth.getName());
       db.insert(Bluetooth.NAME, null, content);
       prefs.edit().remove(entry.getKey()).apply();
     }
     if (entry.getKey().startsWith("bt.last.connect")) {
       prefs.edit().remove(entry.getKey()).apply();
     }
   }
 }
  // Main thread
  public void run() {
    String stringBuffer = "";
    byte[] byteBuffer = new byte[1024];

    connection = Bluetooth.waitForConnection();
    connected = true;
    is = connection.openDataInputStream();
    os = connection.openDataOutputStream();
    terminateFlag = false;

    while (!terminateFlag) {
      // read into byte[] buffer
      int bytesRead;
      try {
        bytesRead = is.read(byteBuffer);
      } catch (IOException e) {
        bytesRead = 0;
      }
      if (bytesRead > 0) {
        // transfer from byte[] into StringBuffer
        stringBuffer += new String(byteBuffer, 0, bytesRead);
        // check for }
        // if found, this suggests that we just finished receiving a full message
        int endChar = stringBuffer.indexOf("}");
        if (endChar != -1) {
          // check for matching {
          int startChar = stringBuffer.indexOf("{");
          if (startChar != -1 && startChar < endChar) {
            // parse the message and add it to the queue
            Message messageRead = new Message(stringBuffer.substring(startChar, endChar + 1));
            messageQueue.push(messageRead);
            Message ack = new Message(messageRead.getSeqNum());
            ack.pairs.add(new String[] {"ack", null});
            sendMessage(ack);
          }

          // clean command up to } off stringBuffer
          if (endChar == stringBuffer.length() - 1) {
            stringBuffer = "";
          } else {
            stringBuffer = stringBuffer.substring(endChar + 1);
          }
        }
      }
    }

    // disconnect
    try {
      is.close();
      os.close();
    } catch (IOException e) {
      // TODO: not fail silently
    }
    connection.close();
    connected = false;
  }
 public List<Bluetooth> readBluetooth() {
   List<Bluetooth> list;
   Cursor cursor = null;
   try {
     cursor =
         getReadableDatabase()
             .rawQuery("SELECT id, name, address FROM BLUETOOTH order by used desc", null);
     list = new ArrayList<>(cursor.getCount());
     if (cursor.getCount() > 0) {
       cursor.moveToFirst();
       do {
         Bluetooth p = new Bluetooth(cursor.getString(1), cursor.getString(2));
         p.setId(cursor.getInt(0));
         list.add(p);
       } while (cursor.moveToNext());
     }
   } finally {
     if (cursor != null) {
       cursor.close();
     }
   }
   return list;
 }