Skip to content

Commit

Permalink
connection success with distoxble
Browse files Browse the repository at this point in the history
  • Loading branch information
lz1asl committed Sep 18, 2023
1 parent 6000064 commit 1ecce65
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ protected void onPause() {

@Override
public void refresh() {
runOnUiThread(() -> updateDeviceStatus());
runOnUiThread(() -> {
refreshDevicesList();
updateDeviceStatus();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,10 @@ public static synchronized void selectDevice(final DiscoveredBluetoothDevice aDe
Log.i(LOG_TAG_BT, "No device selected");
return;
}

mSelectedDevice = aDevice;
BluetoothDevice deviceRef = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(aDevice.address);
aDevice.device = deviceRef;
mSelectedDevice = aDevice;

Log.i(LOG_TAG_BT, "Selected " + aDevice.address + " : " + mSelectedDevice + " of type " + aDevice.definition.getDescription());

Expand Down Expand Up @@ -319,6 +320,7 @@ public static synchronized void selectDevice(final DiscoveredBluetoothDevice aDe
// check if we need to connect from scratch or just reconnect to previous device
if (mBluetoothGatt != null) {// && aDeviceAddress.equals(mBluetoothGatt.getDevice().getAddress())) {
Log.i(LOG_TAG_BT, "Reset LE");
stopDiscoverBluetoothLEDevices();
mBluetoothGatt.close();
}
Log.i(LOG_TAG_BT, "Connecting LE");
Expand Down Expand Up @@ -347,7 +349,7 @@ public static AbstractBluetoothDevice getSupportedDevice(BluetoothDevice aDevice
}
}
}
Log.d(LOG_TAG_BT, "Search supported LE device for " + name + " and services " + leServices);
Log.d(LOG_TAG_BT, "Search supported LE device for name " + name + " and services " + leServices);
for (AbstractBluetoothLEDevice device : SUPPORTED_BLUETOOTH_LE_DEVICES) {
if (device.isTypeCompatible(aDevice) &&
((StringUtils.isNotEmpty(name) && device.isNameSupported(name)) || device.isServiceSupported(leServices))) {
Expand Down Expand Up @@ -394,7 +396,7 @@ public static String getCurrDeviceStatus() {
return mCurrContext.getString(mLeDeviceState);
}

if (mSelectedDevice.definition instanceof AbstractBluetoothRFCOMMDevice) {
if (mSelectedDevice != null && mSelectedDevice.definition instanceof AbstractBluetoothRFCOMMDevice) {
switch (mSelectedDevice.device.getBondState()) {
case BluetoothDevice.BOND_BONDED:
return mCurrContext.getString(R.string.bt_state_bonded);
Expand Down Expand Up @@ -518,11 +520,7 @@ public void onScanFailed(int errorCode) {
};

final BluetoothManager btManager = (BluetoothManager) ConfigUtil.getContext().getSystemService(BLUETOOTH_SERVICE);
// List<ScanFilter> filters = Arrays.asList(new ScanFilter.Builder().setServiceUuid(ParcelUuid.fromString(DistoXBleDevice.READ_SERVICE_UUID.toString())).build());
// ScanSettings settings = new ScanSettings.Builder()
// .setScanMode(SCAN_MODE_BALANCED).setMatchMode(MATCH_MODE_STICKY).setCallbackType(CALLBACK_TYPE_FIRST_MATCH).build();
btManager.getAdapter().getBluetoothLeScanner().startScan(callback);
// btManager.getAdapter().getBluetoothLeScanner().startScan(filters, settings, callback);
return callback;
}

Expand All @@ -534,7 +532,7 @@ private static void handleDeviceDiscovered(BluetoothDevice device, int rssi, Sca
AbstractBluetoothDevice deviceSpec = BluetoothService.getSupportedDevice(device, aScanRecord.getServiceUuids());

if (deviceSpec != null && deviceSpec instanceof AbstractBluetoothLEDevice) {
Log.i(LOG_TAG_BT, "Discovered LE device " + rssi + " : " + name);
Log.i(LOG_TAG_BT, "Discovered LE device " + rssi + " : " + name + " : " + aScanRecord.getServiceUuids());

mLastLEDevice = new DiscoveredBluetoothDevice(deviceSpec, name, device.getAddress());
mLastLEDevice.device = device;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public DiscoveredBluetoothDevice(AbstractBluetoothDevice aDefinition, String aNa
}

public String getDisplayName() {
return name + " : " + address;
if (name != null) {
return name + " : " + address;
} else {
return definition.getDescription() + " : " + address;
}
}
}

0 comments on commit 1ecce65

Please sign in to comment.