1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-19 23:12:12 +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;
}
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()

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -55,7 +55,7 @@ public class BluetoothPreferences extends PreferenceFragment {
private PreferenceScreen btScanner;
private BluetoothAdapter btAdapter = null;
private Handler handler = null;
private Map<String, String> foundDevices = new HashMap<>();
private Map<String, BluetoothDevice> 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;
}
}