diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCustomOpenScale.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCustomOpenScale.java index adad451e..dd7c4c92 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCustomOpenScale.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothCustomOpenScale.java @@ -69,46 +69,40 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication { return; } - for (BluetoothDevice device : btAdapter.getBondedDevices()) { - if (device != null && device.getAddress().equals(hwAddress)) { - btDevice = device; - - try { - // Get a BluetoothSocket to connect with the given BluetoothDevice - btSocket = btDevice.createRfcommSocketToServiceRecord(uuid); - } catch (IOException e) { - setBtStatus(BT_STATUS_CODE.BT_UNEXPECTED_ERROR, "Can't get a bluetooth socket"); - } - - Thread socketThread = new Thread() { - @Override - public void run() { - try { - if (!btSocket.isConnected()) { - // Connect the device through the socket. This will block - // until it succeeds or throws an exception - btSocket.connect(); - - // Bluetooth connection was successful - setBtStatus(BT_STATUS_CODE.BT_CONNECTION_ESTABLISHED); - - btConnectThread = new BluetoothConnectedThread(); - btConnectThread.start(); - } - } catch (IOException connectException) { - // Unable to connect; close the socket and get out - disconnect(false); - setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND); - } - } - }; - - socketThread.start(); - return; - } + btDevice = btAdapter.getRemoteDevice(hwAddress); + try { + // Get a BluetoothSocket to connect with the given BluetoothDevice + btSocket = btDevice.createRfcommSocketToServiceRecord(uuid); + } catch (IOException e) { + setBtStatus(BT_STATUS_CODE.BT_UNEXPECTED_ERROR, "Can't get a bluetooth socket"); + btDevice = null; + return; } - setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND); + Thread socketThread = new Thread() { + @Override + public void run() { + try { + if (!btSocket.isConnected()) { + // Connect the device through the socket. This will block + // until it succeeds or throws an exception + btSocket.connect(); + + // Bluetooth connection was successful + setBtStatus(BT_STATUS_CODE.BT_CONNECTION_ESTABLISHED); + + btConnectThread = new BluetoothConnectedThread(); + btConnectThread.start(); + } + } catch (IOException connectException) { + // Unable to connect; close the socket and get out + disconnect(false); + setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND); + } + } + }; + + socketThread.start(); } @Override @@ -128,6 +122,8 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication { btConnectThread.cancel(); btConnectThread = null; } + + btDevice = null; } public void clearEEPROM() diff --git a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothIhealthHS3.java b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothIhealthHS3.java index 470a628f..637af4de 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothIhealthHS3.java +++ b/android_app/app/src/main/java/com/health/openscale/core/bluetooth/BluetoothIhealthHS3.java @@ -75,58 +75,45 @@ public class BluetoothIhealthHS3 extends BluetoothCommunication { @Override public void connect(String hwAddress) { -// Log.w("openscale","iHealth HS3 - connect "+hwAddress); - if (btAdapter == null) { setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND); return; } -// Log.w("openscale","about to start searching paired devices"); - - for (BluetoothDevice device : btAdapter.getBondedDevices()) { - // check if we can found bluetooth device name in the pairing list -// if (device != null ) { Log.w("openscale","Looking at device "+device.getName()); } ; - - if (device != null && device.getAddress().equals(hwAddress)) { - btDevice = device; - - try { - // Get a BluetoothSocket to connect with the given BluetoothDevice - btSocket = btDevice.createRfcommSocketToServiceRecord(uuid); - } catch (IOException e) { - setBtStatus(BT_STATUS_CODE.BT_UNEXPECTED_ERROR, "Can't get a bluetooth socket"); - } - - Thread socketThread = new Thread() { - @Override - public void run() { - try { - if (!btSocket.isConnected()) { - // Connect the device through the socket. This will block - // until it succeeds or throws an exception - btSocket.connect(); - - // Bluetooth connection was successful - setBtStatus(BT_STATUS_CODE.BT_CONNECTION_ESTABLISHED); - - btConnectThread = new BluetoothConnectedThread(); - btConnectThread.start(); - } - } catch (IOException connectException) { - // Unable to connect; close the socket and get out - disconnect(false); - setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND); - } - } - }; - - socketThread.start(); - return; - } + btDevice = btAdapter.getRemoteDevice(hwAddress); + try { + // Get a BluetoothSocket to connect with the given BluetoothDevice + btSocket = btDevice.createRfcommSocketToServiceRecord(uuid); + } catch (IOException e) { + setBtStatus(BT_STATUS_CODE.BT_UNEXPECTED_ERROR, "Can't get a bluetooth socket"); + btDevice = null; + return; } - setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND); + Thread socketThread = new Thread() { + @Override + public void run() { + try { + if (!btSocket.isConnected()) { + // Connect the device through the socket. This will block + // until it succeeds or throws an exception + btSocket.connect(); + + // Bluetooth connection was successful + setBtStatus(BT_STATUS_CODE.BT_CONNECTION_ESTABLISHED); + + btConnectThread = new BluetoothConnectedThread(); + btConnectThread.start(); + } + } catch (IOException connectException) { + // Unable to connect; close the socket and get out + disconnect(false); + setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND); + } + } + }; + + socketThread.start(); } @Override @@ -148,6 +135,8 @@ public class BluetoothIhealthHS3 extends BluetoothCommunication { btConnectThread.cancel(); btConnectThread = null; } + + btDevice = null; } diff --git a/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java b/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java index 8076f805..2e7d2939 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/MainActivity.java @@ -454,11 +454,11 @@ public class MainActivity extends BaseAppCompatActivity } String deviceName = prefs.getString( - BluetoothPreferences.PREFERENCE_KEY_BLUETOOTH_DEVICE_NAME, "-"); + BluetoothPreferences.PREFERENCE_KEY_BLUETOOTH_DEVICE_NAME, ""); String hwAddress = prefs.getString( BluetoothPreferences.PREFERENCE_KEY_BLUETOOTH_HW_ADDRESS, ""); - if (deviceName.equals("-")) { + if (!BluetoothAdapter.checkBluetoothAddress(hwAddress)) { Toast.makeText(getApplicationContext(), R.string.info_bluetooth_no_device_set, Toast.LENGTH_SHORT).show(); return; } diff --git a/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothPreferences.java b/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothPreferences.java index deded03d..8efb0f13 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothPreferences.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/preferences/BluetoothPreferences.java @@ -55,7 +55,7 @@ public class BluetoothPreferences extends PreferenceFragment { private PreferenceScreen btScanner; private BluetoothAdapter btAdapter = null; private Handler handler = null; - private Map foundDevices = new HashMap<>(); + private Map foundDevices = new HashMap<>(); private void startBluetoothDiscovery() { foundDevices.clear(); @@ -169,7 +169,7 @@ public class BluetoothPreferences extends PreferenceFragment { prefBtDevice.setEnabled(false); } - foundDevices.put(device.getAddress(), device.getName()); + foundDevices.put(device.getAddress(), prefBtDevice.isEnabled() ? device : null); btScanner.addPreference(prefBtDevice); } @@ -274,9 +274,11 @@ public class BluetoothPreferences extends PreferenceFragment { private class onClickListenerDeviceSelect implements Preference.OnPreferenceClickListener { @Override public boolean onPreferenceClick(final Preference preference) { + BluetoothDevice device = foundDevices.get(preference.getKey()); + preference.getSharedPreferences().edit() - .putString(PREFERENCE_KEY_BLUETOOTH_HW_ADDRESS, preference.getKey()) - .putString(PREFERENCE_KEY_BLUETOOTH_DEVICE_NAME, foundDevices.get(preference.getKey())) + .putString(PREFERENCE_KEY_BLUETOOTH_HW_ADDRESS, device.getAddress()) + .putString(PREFERENCE_KEY_BLUETOOTH_DEVICE_NAME, device.getName()) .apply(); // Set summary text and trigger data set changed to make UI update @@ -284,6 +286,12 @@ public class BluetoothPreferences extends PreferenceFragment { ((BaseAdapter)getPreferenceScreen().getRootAdapter()).notifyDataSetChanged(); btScanner.getDialog().dismiss(); + + // Perform an explicit bonding with classic devices + if (device.getType() == BluetoothDevice.DEVICE_TYPE_CLASSIC + && device.getBondState() == BluetoothDevice.BOND_NONE) { + device.createBond(); + } return true; } }