From 45898bee862699e06e76cbb3017a1bddfd722a13 Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Wed, 5 Oct 2016 14:56:23 +0200 Subject: [PATCH] onDeviceConnecting event added to BleManagerCallbacks --- .../nrftoolbox/profile/BleManager.java | 3 +- .../profile/BleManagerCallbacks.java | 32 ++++++++++++++----- .../profile/BleProfileActivity.java | 5 +++ .../BleProfileExpandableListActivity.java | 5 +++ .../nrftoolbox/profile/BleProfileService.java | 16 ++++++---- .../BleProfileServiceReadyActivity.java | 22 ++++++++++--- 6 files changed, 63 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleManager.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleManager.java index 91467fd1..be4d7b80 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleManager.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleManager.java @@ -207,9 +207,10 @@ public abstract class BleManager { final boolean autoConnect = shouldAutoConnect(); mUserDisconnected = !autoConnect; // We will receive Linkloss events only when the device is connected with autoConnect=true + mBluetoothDevice = device; + mCallbacks.onDeviceConnecting(device); Logger.v(mLogSession, "Connecting..."); Logger.d(mLogSession, "gatt = device.connectGatt(autoConnect = " + autoConnect + ")"); - mBluetoothDevice = device; mBluetoothGatt = device.connectGatt(mContext, autoConnect, getGattCallback()); } } diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleManagerCallbacks.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleManagerCallbacks.java index bd0a082e..072a6a70 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleManagerCallbacks.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleManagerCallbacks.java @@ -28,8 +28,18 @@ import android.bluetooth.BluetoothGattCallback; public interface BleManagerCallbacks { /** - * Called when the device has been connected. This does not mean that the application may start communication. A service discovery will be handled automatically after this call. Service discovery - * may ends up with calling {@link #onServicesDiscovered(BluetoothDevice, boolean)} or {@link #onDeviceNotSupported(BluetoothDevice)} if required services have not been found. + * Called when the Android device started connecting to given device. + * The {@link #onDeviceConnected(BluetoothDevice)} will be called when the device is connected, + * or {@link #onError(BluetoothDevice, String, int)} in case of error. + * @param device the device that got connected + */ + void onDeviceConnecting(final BluetoothDevice device); + + /** + * Called when the device has been connected. This does not mean that the application may start communication. + * A service discovery will be handled automatically after this call. Service discovery + * may ends up with calling {@link #onServicesDiscovered(BluetoothDevice, boolean)} or + * {@link #onDeviceNotSupported(BluetoothDevice)} if required services have not been found. * @param device the device that got connected */ void onDeviceConnected(final BluetoothDevice device); @@ -41,26 +51,32 @@ public interface BleManagerCallbacks { void onDeviceDisconnecting(final BluetoothDevice device); /** - * Called when the device has disconnected (when the callback returned {@link BluetoothGattCallback#onConnectionStateChange(BluetoothGatt, int, int)} with state DISCONNECTED. + * Called when the device has disconnected (when the callback returned + * {@link BluetoothGattCallback#onConnectionStateChange(BluetoothGatt, int, int)} with state DISCONNECTED. * @param device the device that got disconnected */ void onDeviceDisconnected(final BluetoothDevice device); /** - * This callback is invoked when the Ble Manager lost connection to a device that has been connected with autoConnect option. Otherwise a {@link #onDeviceDisconnected(BluetoothDevice)} - * method will be called on such event. + * This callback is invoked when the Ble Manager lost connection to a device that has been connected + * with autoConnect option (see {@link BleManager#shouldAutoConnect()}. + * Otherwise a {@link #onDeviceDisconnected(BluetoothDevice)} method will be called on such event. * @param device the device that got disconnected due to a link loss */ void onLinklossOccur(final BluetoothDevice device); /** - * Called when service discovery has finished and primary services has been found. The device is ready to operate. This method is not called if the primary, mandatory services were not found - * during service discovery. For example in the Blood Pressure Monitor, a Blood Pressure service is a primary service and Intermediate Cuff Pressure service is a optional secondary service. + * Called when service discovery has finished and primary services has been found. + * This method is not called if the primary, mandatory services were not found during service discovery. + * For example in the Blood Pressure Monitor, a Blood Pressure service is a primary service and + * Intermediate Cuff Pressure service is a optional secondary service. * Existence of battery service is not notified by this call. + *

After successful service discovery the service will initialize all services. + * The {@link #onDeviceReady(BluetoothDevice)} method will be called when the initialization is complete.

* + * @param device the device which services got disconnected * @param optionalServicesFound * if true the secondary services were also found on the device. - * @param device the device which services got disconnected */ void onServicesDiscovered(final BluetoothDevice device, final boolean optionalServicesFound); diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileActivity.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileActivity.java index 408ff3b1..efe97838 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileActivity.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileActivity.java @@ -238,6 +238,11 @@ public abstract class BleProfileActivity extends AppCompatActivity implements Bl // do nothing } + @Override + public void onDeviceConnecting(final BluetoothDevice device) { + // do nothing + } + @Override public void onDeviceConnected(final BluetoothDevice device) { mDeviceConnected = true; diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileExpandableListActivity.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileExpandableListActivity.java index e8a2d898..64aef826 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileExpandableListActivity.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileExpandableListActivity.java @@ -235,6 +235,11 @@ public abstract class BleProfileExpandableListActivity extends ExpandableListAct // do nothing } + @Override + public void onDeviceConnecting(final BluetoothDevice device) { + // do nothing + } + @Override public void onDeviceConnected(final BluetoothDevice device) { mDeviceConnected = true; diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileService.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileService.java index 621f6034..f887fd78 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileService.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileService.java @@ -200,7 +200,8 @@ public abstract class BleProfileService extends Service implements BleManagerCal } /** - * Called when the activity has unbinded from the service before being finished. This method is not called when the activity is killed to be recreated just to change the phone orientation. + * Called when the activity has unbound from the service before being finished. + * This method is not called when the activity is killed to be recreated just to change the phone orientation. */ protected void onUnbind() { // empty @@ -232,11 +233,6 @@ public abstract class BleProfileService extends Service implements BleManagerCal Logger.i(mLogSession, "Service started"); - // notify user about changing the state to CONNECTING - final Intent broadcast = new Intent(BROADCAST_CONNECTION_STATE); - broadcast.putExtra(EXTRA_CONNECTION_STATE, STATE_CONNECTING); - LocalBroadcastManager.getInstance(BleProfileService.this).sendBroadcast(broadcast); - final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE); final BluetoothAdapter adapter = bluetoothManager.getAdapter(); final String deviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS); @@ -268,6 +264,14 @@ public abstract class BleProfileService extends Service implements BleManagerCal mLogSession = null; } + @Override + public void onDeviceConnecting(final BluetoothDevice device) { + final Intent broadcast = new Intent(BROADCAST_CONNECTION_STATE); + broadcast.putExtra(EXTRA_DEVICE, mBluetoothDevice); + broadcast.putExtra(EXTRA_CONNECTION_STATE, STATE_CONNECTING); + LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast); + } + @Override public void onDeviceConnected(final BluetoothDevice device) { mConnected = true; diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileServiceReadyActivity.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileServiceReadyActivity.java index 7ca72b44..df45386a 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileServiceReadyActivity.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileServiceReadyActivity.java @@ -115,9 +115,14 @@ public abstract class BleProfileServiceReadyActivity