mirror of
https://github.com/aljazceru/Android-nRF-Toolbox.git
synced 2026-01-09 09:44:22 +01:00
onDeviceConnecting event added to BleManagerCallbacks
This commit is contained in:
@@ -207,9 +207,10 @@ public abstract class BleManager<E extends BleManagerCallbacks> {
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
* <p>After successful service discovery the service will initialize all services.
|
||||
* The {@link #onDeviceReady(BluetoothDevice)} method will be called when the initialization is complete.</p>
|
||||
*
|
||||
* @param device the device which services got disconnected
|
||||
* @param optionalServicesFound
|
||||
* if <code>true</code> 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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -115,9 +115,14 @@ public abstract class BleProfileServiceReadyActivity<E extends BleProfileService
|
||||
onLinklossOccur(bluetoothDevice);
|
||||
break;
|
||||
}
|
||||
case BleProfileService.STATE_CONNECTING:
|
||||
case BleProfileService.STATE_DISCONNECTING:
|
||||
// current implementation does nothing in this states
|
||||
case BleProfileService.STATE_CONNECTING: {
|
||||
onDeviceConnecting(bluetoothDevice);
|
||||
break;
|
||||
}
|
||||
case BleProfileService.STATE_DISCONNECTING: {
|
||||
onDeviceDisconnecting(bluetoothDevice);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
// there should be no other actions
|
||||
break;
|
||||
@@ -474,6 +479,11 @@ public abstract class BleProfileServiceReadyActivity<E extends BleProfileService
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeviceConnecting(final BluetoothDevice device) {
|
||||
// empty default implementation
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeviceConnected(final BluetoothDevice device) {
|
||||
mDeviceNameView.setText(mDeviceName);
|
||||
@@ -483,7 +493,7 @@ public abstract class BleProfileServiceReadyActivity<E extends BleProfileService
|
||||
|
||||
@Override
|
||||
public void onDeviceDisconnecting(final BluetoothDevice device) {
|
||||
// do nothing
|
||||
// empty default implementation
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -515,7 +525,9 @@ public abstract class BleProfileServiceReadyActivity<E extends BleProfileService
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract void onServicesDiscovered(final BluetoothDevice device, final boolean optionalServicesFound);
|
||||
public void onServicesDiscovered(final BluetoothDevice device, final boolean optionalServicesFound) {
|
||||
// empty default implementation
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeviceReady(final BluetoothDevice device) {
|
||||
|
||||
Reference in New Issue
Block a user