1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-20 23:41:45 +02:00

Perform explicit bond to classic devices

and refactor handling of connection to classic devices.
This commit is contained in:
Erik Johansson
2018-04-14 20:25:48 +02:00
parent be330c7141
commit 1bdec6a164
4 changed files with 82 additions and 89 deletions

View File

@@ -69,46 +69,40 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication {
return; return;
} }
for (BluetoothDevice device : btAdapter.getBondedDevices()) { btDevice = btAdapter.getRemoteDevice(hwAddress);
if (device != null && device.getAddress().equals(hwAddress)) { try {
btDevice = device; // Get a BluetoothSocket to connect with the given BluetoothDevice
btSocket = btDevice.createRfcommSocketToServiceRecord(uuid);
try { } catch (IOException e) {
// Get a BluetoothSocket to connect with the given BluetoothDevice setBtStatus(BT_STATUS_CODE.BT_UNEXPECTED_ERROR, "Can't get a bluetooth socket");
btSocket = btDevice.createRfcommSocketToServiceRecord(uuid); btDevice = null;
} catch (IOException e) { return;
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;
}
} }
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 @Override
@@ -128,6 +122,8 @@ public class BluetoothCustomOpenScale extends BluetoothCommunication {
btConnectThread.cancel(); btConnectThread.cancel();
btConnectThread = null; btConnectThread = null;
} }
btDevice = null;
} }
public void clearEEPROM() public void clearEEPROM()

View File

@@ -75,58 +75,45 @@ public class BluetoothIhealthHS3 extends BluetoothCommunication {
@Override @Override
public void connect(String hwAddress) { public void connect(String hwAddress) {
// Log.w("openscale","iHealth HS3 - connect "+hwAddress);
if (btAdapter == null) { if (btAdapter == null) {
setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND); setBtStatus(BT_STATUS_CODE.BT_NO_DEVICE_FOUND);
return; return;
} }
// Log.w("openscale","about to start searching paired devices"); btDevice = btAdapter.getRemoteDevice(hwAddress);
try {
for (BluetoothDevice device : btAdapter.getBondedDevices()) { // Get a BluetoothSocket to connect with the given BluetoothDevice
// check if we can found bluetooth device name in the pairing list btSocket = btDevice.createRfcommSocketToServiceRecord(uuid);
// if (device != null ) { Log.w("openscale","Looking at device "+device.getName()); } ; } catch (IOException e) {
setBtStatus(BT_STATUS_CODE.BT_UNEXPECTED_ERROR, "Can't get a bluetooth socket");
if (device != null && device.getAddress().equals(hwAddress)) { btDevice = null;
btDevice = device; return;
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;
}
} }
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 @Override
@@ -148,6 +135,8 @@ public class BluetoothIhealthHS3 extends BluetoothCommunication {
btConnectThread.cancel(); btConnectThread.cancel();
btConnectThread = null; btConnectThread = null;
} }
btDevice = null;
} }

View File

@@ -454,11 +454,11 @@ public class MainActivity extends BaseAppCompatActivity
} }
String deviceName = prefs.getString( String deviceName = prefs.getString(
BluetoothPreferences.PREFERENCE_KEY_BLUETOOTH_DEVICE_NAME, "-"); BluetoothPreferences.PREFERENCE_KEY_BLUETOOTH_DEVICE_NAME, "");
String hwAddress = prefs.getString( String hwAddress = prefs.getString(
BluetoothPreferences.PREFERENCE_KEY_BLUETOOTH_HW_ADDRESS, ""); 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(); Toast.makeText(getApplicationContext(), R.string.info_bluetooth_no_device_set, Toast.LENGTH_SHORT).show();
return; return;
} }

View File

@@ -55,7 +55,7 @@ public class BluetoothPreferences extends PreferenceFragment {
private PreferenceScreen btScanner; private PreferenceScreen btScanner;
private BluetoothAdapter btAdapter = null; private BluetoothAdapter btAdapter = null;
private Handler handler = null; private Handler handler = null;
private Map<String, String> foundDevices = new HashMap<>(); private Map<String, BluetoothDevice> foundDevices = new HashMap<>();
private void startBluetoothDiscovery() { private void startBluetoothDiscovery() {
foundDevices.clear(); foundDevices.clear();
@@ -169,7 +169,7 @@ public class BluetoothPreferences extends PreferenceFragment {
prefBtDevice.setEnabled(false); prefBtDevice.setEnabled(false);
} }
foundDevices.put(device.getAddress(), device.getName()); foundDevices.put(device.getAddress(), prefBtDevice.isEnabled() ? device : null);
btScanner.addPreference(prefBtDevice); btScanner.addPreference(prefBtDevice);
} }
@@ -274,9 +274,11 @@ public class BluetoothPreferences extends PreferenceFragment {
private class onClickListenerDeviceSelect implements Preference.OnPreferenceClickListener { private class onClickListenerDeviceSelect implements Preference.OnPreferenceClickListener {
@Override @Override
public boolean onPreferenceClick(final Preference preference) { public boolean onPreferenceClick(final Preference preference) {
BluetoothDevice device = foundDevices.get(preference.getKey());
preference.getSharedPreferences().edit() preference.getSharedPreferences().edit()
.putString(PREFERENCE_KEY_BLUETOOTH_HW_ADDRESS, preference.getKey()) .putString(PREFERENCE_KEY_BLUETOOTH_HW_ADDRESS, device.getAddress())
.putString(PREFERENCE_KEY_BLUETOOTH_DEVICE_NAME, foundDevices.get(preference.getKey())) .putString(PREFERENCE_KEY_BLUETOOTH_DEVICE_NAME, device.getName())
.apply(); .apply();
// Set summary text and trigger data set changed to make UI update // 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(); ((BaseAdapter)getPreferenceScreen().getRootAdapter()).notifyDataSetChanged();
btScanner.getDialog().dismiss(); 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; return true;
} }
} }