Skip to content

Commit

Permalink
Merge pull request #279 from NordicSemiconductor/dev
Browse files Browse the repository at this point in the history
nRF Mesh release 2.1.4
  • Loading branch information
roshanrajaratnam authored Nov 21, 2019
2 parents a6ad753 + ed0065a commit b512524
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
Expand All @@ -55,7 +54,7 @@
import no.nordicsemi.android.nrfmeshprovisioner.ble.adapter.DevicesAdapter;
import no.nordicsemi.android.nrfmeshprovisioner.di.Injectable;
import no.nordicsemi.android.nrfmeshprovisioner.utils.Utils;
import no.nordicsemi.android.nrfmeshprovisioner.viewmodels.ScannerLiveData;
import no.nordicsemi.android.nrfmeshprovisioner.viewmodels.ScannerStateLiveData;
import no.nordicsemi.android.nrfmeshprovisioner.viewmodels.ScannerViewModel;

public class ScannerActivity extends AppCompatActivity implements Injectable,
Expand Down Expand Up @@ -117,7 +116,7 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) {
final SimpleItemAnimator itemAnimator = (SimpleItemAnimator) recyclerViewDevices.getItemAnimator();
if (itemAnimator != null) itemAnimator.setSupportsChangeAnimations(false);

final DevicesAdapter adapter = new DevicesAdapter(this, mViewModel.getScannerRepository().getScannerState());
final DevicesAdapter adapter = new DevicesAdapter(this, mViewModel.getScannerRepository().getScannerResults());
adapter.setOnItemClickListener(this);
recyclerViewDevices.setAdapter(adapter);

Expand Down Expand Up @@ -191,7 +190,7 @@ public void onItemClick(final ExtendedBluetoothDevice device) {
public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_ACCESS_FINE_LOCATION) {
mViewModel.getScannerRepository().getScannerState().refresh();
mViewModel.getScannerRepository().getScannerState().startScanning();
}
}

Expand Down Expand Up @@ -223,7 +222,7 @@ public void onPermissionSettingsClicked() {
/**
* Start scanning for Bluetooth devices or displays a message based on the scanner state.
*/
private void startScan(final ScannerLiveData state) {
private void startScan(final ScannerStateLiveData state) {
// First, check the Location permission. This is required on Marshmallow onwards in order to scan for Bluetooth LE devices.
if (Utils.isLocationPermissionsGranted(this)) {
mNoLocationPermissionView.setVisibility(View.GONE);
Expand All @@ -232,13 +231,15 @@ private void startScan(final ScannerLiveData state) {
if (state.isBluetoothEnabled()) {
mNoBluetoothView.setVisibility(View.GONE);

// We are now OK to start scanning
if (mScanWithProxyService) {
mViewModel.getScannerRepository().startScan(BleMeshManager.MESH_PROVISIONING_UUID);
} else {
mViewModel.getScannerRepository().startScan(BleMeshManager.MESH_PROXY_UUID);
if (!state.isScanning()) {
// We are now OK to start scanning
if (mScanWithProxyService) {
mViewModel.getScannerRepository().startScan(BleMeshManager.MESH_PROVISIONING_UUID);
} else {
mViewModel.getScannerRepository().startScan(BleMeshManager.MESH_PROXY_UUID);
}
mScanningView.setVisibility(View.VISIBLE);
}
mScanningView.setVisibility(View.VISIBLE);

if (state.isEmpty()) {
mEmptyView.setVisibility(View.VISIBLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ public void onBondingFailed(@NonNull final BluetoothDevice device) {

@Override
public void onError(final BluetoothDevice device, @NonNull final String message, final int errorCode) {
Log.e(TAG, "Error: " + message + " Error Code: " + errorCode + " Device: " + device.getAddress());
Log.e(TAG, message + " (code: " + errorCode + "), device: " + device.getAddress());
mConnectionState.postValue(message);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,70 +43,8 @@
public class ScannerLiveData extends LiveData<ScannerLiveData> {
private final List<ExtendedBluetoothDevice> mDevices = new ArrayList<>();
private Integer mUpdatedDeviceIndex;
private boolean mScanningStarted;
private boolean mBluetoothEnabled;
private boolean mLocationEnabled;
private boolean mStartScanning;
private boolean mStopScanning;

ScannerLiveData(final boolean bluetoothEnabled, final boolean locationEnabled) {
mScanningStarted = false;
mBluetoothEnabled = bluetoothEnabled;
mLocationEnabled = locationEnabled;
postValue(this);
}

public void refresh() {
postValue(this);
}

/**
* Updates the flag to notify scanner live data that a stop scan was requested.
*/
public void startScanning() {
mDevices.clear(); //Clear the devices on resuming the scan
mStopScanning = false;
mStartScanning = true;
setValue(this);
}

public boolean isScanRequested(){
return mStartScanning;
}

/**
* Updates the flag to notify scanner live data that a stop scan was requested.
*/
public void stopScanning() {
mStopScanning = true;
mStartScanning = false;
setValue(this);
}

boolean isStopScanRequested(){
return mStopScanning;
}

void scanningStarted() {
mScanningStarted = true;
setValue(this);
}

void scanningStopped() {
mScanningStarted = false;
setValue(this);
}

void bluetoothEnabled() {
mBluetoothEnabled = true;
postValue(this);
}

void bluetoothDisabled() {
mBluetoothEnabled = false;
mUpdatedDeviceIndex = null;
mDevices.clear();
postValue(this);
ScannerLiveData() {
}

void deviceDiscovered(final ScanResult result) {
Expand Down Expand Up @@ -174,36 +112,6 @@ public boolean isEmpty() {
return mDevices.isEmpty();
}

/**
* Returns whether scanning is in progress.
*/
public boolean isScanning() {
return mScanningStarted;
}

public boolean isScanStopped() {
return mScanningStarted;
}

/**
* Returns whether Bluetooth adapter is enabled.
*/
public boolean isBluetoothEnabled() {
return mBluetoothEnabled;
}

/**
* Returns whether Location is enabled.
*/
public boolean isLocationEnabled() {
return mLocationEnabled;
}

void setLocationEnabled(final boolean enabled) {
mLocationEnabled = enabled;
postValue(this);
}

/**
* Finds the index of existing devices on the scan results list.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class ScannerRepository {
* MutableLiveData containing the scanner state to notify MainActivity.
*/
private final ScannerLiveData mScannerLiveData;
private final ScannerStateLiveData mScannerStateLiveData;

private UUID mFilterUuid;

Expand All @@ -77,9 +78,7 @@ public void onScanResult(final int callbackType, @NonNull final ScanResult resul
if (Utils.isLocationRequired(mContext) && !Utils.isLocationEnabled(mContext))
Utils.markLocationNotRequired(mContext);

if (!mScannerLiveData.isStopScanRequested()) {
updateScannerLiveData(result);
}
updateScannerLiveData(result);
} else if (mFilterUuid.equals(BleMeshManager.MESH_PROXY_UUID)) {
final byte[] serviceData = Utils.getServiceData(result, BleMeshManager.MESH_PROXY_UUID);
if (mMeshManagerApi != null) {
Expand All @@ -95,7 +94,7 @@ public void onScanResult(final int callbackType, @NonNull final ScanResult resul
}
}
} catch (Exception ex) {
Log.v(TAG, ex.getMessage());
Log.e(TAG, "Error: " + ex.getMessage());
}
}

Expand All @@ -106,12 +105,7 @@ public void onBatchScanResults(@NonNull final List<ScanResult> results) {

@Override
public void onScanFailed(final int errorCode) {
try {
// TODO This should be handled
mScannerLiveData.scanningStopped();
} catch (Exception ex) {
Log.v(TAG, ex.getMessage() + " : Error code: " + errorCode);
}
mScannerStateLiveData.scanningStopped();
}
};

Expand All @@ -122,7 +116,7 @@ public void onScanFailed(final int errorCode) {
@Override
public void onReceive(final Context context, final Intent intent) {
final boolean enabled = Utils.isLocationEnabled(context);
mScannerLiveData.setLocationEnabled(enabled);
mScannerStateLiveData.setLocationEnabled(enabled);
}
};
/**
Expand All @@ -136,14 +130,13 @@ public void onReceive(final Context context, final Intent intent) {

switch (state) {
case BluetoothAdapter.STATE_ON:
mScannerLiveData.bluetoothEnabled();
mScannerLiveData.startScanning();
mScannerStateLiveData.bluetoothEnabled();
break;
case BluetoothAdapter.STATE_TURNING_OFF:
case BluetoothAdapter.STATE_OFF:
if (previousState != BluetoothAdapter.STATE_TURNING_OFF && previousState != BluetoothAdapter.STATE_OFF) {
stopScan();
mScannerLiveData.bluetoothDisabled();
mScannerStateLiveData.bluetoothDisabled();
}
break;
}
Expand All @@ -154,10 +147,15 @@ public void onReceive(final Context context, final Intent intent) {
public ScannerRepository(final Context context, final MeshManagerApi meshManagerApi) {
this.mContext = context;
this.mMeshManagerApi = meshManagerApi;
mScannerLiveData = new ScannerLiveData(Utils.isBleEnabled(), Utils.isLocationEnabled(context));
mScannerStateLiveData = new ScannerStateLiveData(Utils.isBleEnabled(), Utils.isLocationEnabled(context));
mScannerLiveData = new ScannerLiveData();
}

public ScannerStateLiveData getScannerState() {
return mScannerStateLiveData;
}

public ScannerLiveData getScannerState() {
public ScannerLiveData getScannerResults() {
return mScannerLiveData;
}

Expand All @@ -171,6 +169,7 @@ private void updateScannerLiveData(final ScanResult result) {
} else {
mScannerLiveData.deviceDiscovered(result);
}
mScannerStateLiveData.deviceFound();
}
}
}
Expand Down Expand Up @@ -203,13 +202,7 @@ void unregisterBroadcastReceivers() {
public void startScan(final UUID filterUuid) {
mFilterUuid = filterUuid;

if (mScannerLiveData.isScanRequested()) {
if (mScannerLiveData.isScanning()) {
return;
}
}

if (mScannerLiveData.isStopScanRequested()) {
if (mScannerStateLiveData.isScanning()) {
return;
}

Expand All @@ -222,7 +215,7 @@ public void startScan(final UUID filterUuid) {
}
}

mScannerLiveData.scanningStarted();
mScannerStateLiveData.scanningStarted();
//Scanning settings
final ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
Expand All @@ -246,10 +239,9 @@ public void startScan(final UUID filterUuid) {
* stop scanning for bluetooth devices.
*/
public void stopScan() {
mScannerLiveData.stopScanning();
final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
scanner.stopScan(mScanCallbacks);
mScannerLiveData.scanningStopped();
mScannerStateLiveData.scanningStopped();
}

/**
Expand All @@ -268,6 +260,5 @@ private boolean checkIfNodeIdentityMatches(final byte[] serviceData) {
}
}
return false;

}
}
Loading

0 comments on commit b512524

Please sign in to comment.