mirror of
https://github.com/aljazceru/Android-nRF-Toolbox.git
synced 2025-12-24 01:44:23 +01:00
Fixes in Proximity profile
This commit is contained in:
@@ -23,7 +23,6 @@
|
||||
package no.nordicsemi.android.nrftoolbox.proximity;
|
||||
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothGatt;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.text.TextUtils;
|
||||
@@ -31,6 +30,7 @@ import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.List;
|
||||
@@ -95,6 +95,7 @@ public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.ViewHolder
|
||||
private TextView addressView;
|
||||
private TextView batteryView;
|
||||
private ImageButton actionButton;
|
||||
private ProgressBar progress;
|
||||
|
||||
ViewHolder(final View itemView) {
|
||||
super(itemView);
|
||||
@@ -103,14 +104,13 @@ public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.ViewHolder
|
||||
addressView = itemView.findViewById(R.id.address);
|
||||
batteryView = itemView.findViewById(R.id.battery);
|
||||
actionButton = itemView.findViewById(R.id.action_find_silent);
|
||||
progress = itemView.findViewById(R.id.progress);
|
||||
|
||||
// Configure FIND / SILENT button
|
||||
actionButton.setOnClickListener(v -> {
|
||||
final int position = getAdapterPosition();
|
||||
final BluetoothDevice device = mDevices.get(position);
|
||||
final boolean on = mService.toggleImmediateAlert(device);
|
||||
|
||||
actionButton.setImageResource(on ? R.drawable.ic_stat_notify_proximity_silent : R.drawable.ic_stat_notify_proximity_find);
|
||||
mService.toggleImmediateAlert(device);
|
||||
});
|
||||
|
||||
// Configure Disconnect button
|
||||
@@ -124,7 +124,7 @@ public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.ViewHolder
|
||||
}
|
||||
|
||||
private void bind(final BluetoothDevice device) {
|
||||
final int state = mService.getConnectionState(device);
|
||||
final boolean ready = mService.isReady(device);
|
||||
|
||||
String name = device.getName();
|
||||
if (TextUtils.isEmpty(name))
|
||||
@@ -134,14 +134,15 @@ public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.ViewHolder
|
||||
|
||||
final boolean on = mService.isImmediateAlertOn(device);
|
||||
actionButton.setImageResource(on ? R.drawable.ic_stat_notify_proximity_silent : R.drawable.ic_stat_notify_proximity_find);
|
||||
actionButton.setVisibility(state == BluetoothGatt.STATE_CONNECTED ? View.VISIBLE : View.GONE);
|
||||
actionButton.setVisibility(ready ? View.VISIBLE : View.GONE);
|
||||
progress.setVisibility(ready ? View.GONE : View.VISIBLE);
|
||||
|
||||
final Integer batteryValue = mService.getBatteryLevel(device);
|
||||
if (batteryValue != null) {
|
||||
batteryView.getCompoundDrawables()[0 /*left*/].setLevel(batteryValue);
|
||||
batteryView.setVisibility(View.VISIBLE);
|
||||
batteryView.setText(batteryView.getResources().getString(R.string.battery, batteryValue));
|
||||
batteryView.setAlpha(state == BluetoothGatt.STATE_CONNECTED ? 1.0f : 0.5f);
|
||||
batteryView.setAlpha(ready ? 1.0f : 0.5f);
|
||||
} else {
|
||||
batteryView.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@@ -55,7 +55,9 @@ public class LinkLossFragment extends DialogFragment {
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
return new AlertDialog.Builder(requireContext())
|
||||
.setTitle(getString(R.string.app_name)).setMessage(getString(R.string.proximity_notification_linkloss_alert, mName))
|
||||
.setPositiveButton(R.string.ok, null).create();
|
||||
.setTitle(getString(R.string.app_name))
|
||||
.setMessage(getString(R.string.proximity_notification_link_loss_alert, mName))
|
||||
.setPositiveButton(R.string.ok, null)
|
||||
.create();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,6 +149,11 @@ public class ProximityActivity extends BleMulticonnectProfileServiceReadyActivit
|
||||
mAdapter.onBatteryValueReceived(device); // Value will be obtained from the service
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private void onRemoteAlarmSwitched(final BluetoothDevice device, final boolean on) {
|
||||
if (mAdapter != null)
|
||||
mAdapter.onDeviceStateChanged(device); // Value will be obtained from the service
|
||||
}
|
||||
|
||||
private void showLinkLossDialog(final String name) {
|
||||
try {
|
||||
@@ -158,6 +163,7 @@ public class ProximityActivity extends BleMulticonnectProfileServiceReadyActivit
|
||||
// the activity must have been destroyed
|
||||
}
|
||||
}
|
||||
|
||||
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(final Context context, final Intent intent) {
|
||||
@@ -168,6 +174,10 @@ public class ProximityActivity extends BleMulticonnectProfileServiceReadyActivit
|
||||
final int batteryLevel = intent.getIntExtra(ProximityService.EXTRA_BATTERY_LEVEL, 0);
|
||||
// Update GUI
|
||||
onBatteryLevelChanged(device, batteryLevel);
|
||||
} else if (ProximityService.BROADCAST_ALARM_SWITCHED.equals(action)) {
|
||||
final boolean on = intent.getBooleanExtra(ProximityService.EXTRA_ALARM_STATE, false);
|
||||
// Update GUI
|
||||
onRemoteAlarmSwitched(device, on);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -175,6 +185,7 @@ public class ProximityActivity extends BleMulticonnectProfileServiceReadyActivit
|
||||
private static IntentFilter makeIntentFilter() {
|
||||
final IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(ProximityService.BROADCAST_BATTERY_LEVEL);
|
||||
intentFilter.addAction(ProximityService.BROADCAST_ALARM_SWITCHED);
|
||||
return intentFilter;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,9 @@ import android.support.annotation.NonNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import no.nordicsemi.android.ble.callback.FailCallback;
|
||||
import no.nordicsemi.android.ble.common.data.alert.AlertLevelData;
|
||||
import no.nordicsemi.android.ble.error.GattError;
|
||||
import no.nordicsemi.android.log.LogContract;
|
||||
import no.nordicsemi.android.nrftoolbox.battery.BatteryManager;
|
||||
import no.nordicsemi.android.nrftoolbox.parser.AlertLevelParser;
|
||||
@@ -71,6 +73,8 @@ class ProximityManager extends BatteryManager<ProximityManagerCallbacks> {
|
||||
protected void initialize() {
|
||||
super.initialize();
|
||||
writeCharacteristic(mLinkLossCharacteristic, AlertLevelData.highAlert())
|
||||
.done(device -> log(LogContract.Log.Level.INFO, "Link loss alert level set"))
|
||||
.fail((device, status) -> log(LogContract.Log.Level.WARNING, "Failed to set link loss level: " + status))
|
||||
.enqueue();
|
||||
}
|
||||
|
||||
@@ -105,12 +109,9 @@ class ProximityManager extends BatteryManager<ProximityManagerCallbacks> {
|
||||
|
||||
/**
|
||||
* Toggles the immediate alert on the target device.
|
||||
*
|
||||
* @return True if alarm has been enabled, false if disabled.
|
||||
*/
|
||||
public boolean toggleImmediateAlert() {
|
||||
public void toggleImmediateAlert() {
|
||||
writeImmediateAlert(!mAlertOn);
|
||||
return mAlertOn;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,11 +123,19 @@ class ProximityManager extends BatteryManager<ProximityManagerCallbacks> {
|
||||
if (!isConnected())
|
||||
return;
|
||||
|
||||
log(LogContract.Log.Level.VERBOSE, on ? "Setting alarm to HIGH..." : "Disabling alarm...");
|
||||
writeCharacteristic(mAlertLevelCharacteristic, on ? AlertLevelData.highAlert() : AlertLevelData.noAlert())
|
||||
.with((device, data) -> log(LogContract.Log.Level.APPLICATION, "\"" + AlertLevelParser.parse(data) + "\" sent"))
|
||||
.done(device -> mAlertOn = on)
|
||||
.fail((device, status) -> log(LogContract.Log.Level.APPLICATION, "Alert Level characteristic not found"))
|
||||
.before(device -> log(LogContract.Log.Level.VERBOSE,
|
||||
on ? "Setting alarm to HIGH..." : "Disabling alarm..."))
|
||||
.with((device, data) -> log(LogContract.Log.Level.APPLICATION,
|
||||
"\"" + AlertLevelParser.parse(data) + "\" sent"))
|
||||
.done(device -> {
|
||||
mAlertOn = on;
|
||||
mCallbacks.onRemoteAlarmSwitched(device, on);
|
||||
})
|
||||
.fail((device, status) -> log(LogContract.Log.Level.WARNING,
|
||||
status == FailCallback.REASON_NULL_ATTRIBUTE ?
|
||||
"Alert Level characteristic not found" :
|
||||
GattError.parse(status)))
|
||||
.enqueue();
|
||||
}
|
||||
|
||||
|
||||
@@ -21,8 +21,12 @@
|
||||
*/
|
||||
package no.nordicsemi.android.nrftoolbox.proximity;
|
||||
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import no.nordicsemi.android.nrftoolbox.battery.BatteryManagerCallbacks;
|
||||
|
||||
interface ProximityManagerCallbacks extends BatteryManagerCallbacks {
|
||||
// No additional methods
|
||||
void onRemoteAlarmSwitched(@NonNull final BluetoothDevice device, final boolean on);
|
||||
}
|
||||
|
||||
@@ -245,7 +245,7 @@ class ProximityServerManager {
|
||||
public void onConnectionStateChange(final BluetoothDevice device, final int status, final int newState) {
|
||||
mLogger.log(device, LogContract.Log.Level.DEBUG,
|
||||
"[Server callback] Connection state changed with status: " + status
|
||||
+ " and new state: " + stateToString(newState) + " (" + newState + ")");
|
||||
+ " and new state: " + newState + " (" + stateToString(newState) + ")");
|
||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||
if (newState == BluetoothGatt.STATE_CONNECTED) {
|
||||
mLogger.log(device, LogContract.Log.Level.INFO,
|
||||
|
||||
@@ -22,9 +22,10 @@
|
||||
package no.nordicsemi.android.nrftoolbox.proximity;
|
||||
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
public interface ProximityServerManagerCallbacks {
|
||||
void onAlarmTriggered(final BluetoothDevice device);
|
||||
void onAlarmTriggered(@NonNull final BluetoothDevice device);
|
||||
|
||||
void onAlarmStopped(final BluetoothDevice device);
|
||||
void onAlarmStopped(@NonNull final BluetoothDevice device);
|
||||
}
|
||||
|
||||
@@ -60,6 +60,9 @@ public class ProximityService extends BleMulticonnectProfileService implements P
|
||||
public static final String BROADCAST_BATTERY_LEVEL = "no.nordicsemi.android.nrftoolbox.BROADCAST_BATTERY_LEVEL";
|
||||
public static final String EXTRA_BATTERY_LEVEL = "no.nordicsemi.android.nrftoolbox.EXTRA_BATTERY_LEVEL";
|
||||
|
||||
public static final String BROADCAST_ALARM_SWITCHED = "no.nordicsemi.android.nrftoolbox.BROADCAST_ALARM_SWITCHED";
|
||||
public static final String EXTRA_ALARM_STATE = "no.nordicsemi.android.nrftoolbox.EXTRA_ALARM_STATE";
|
||||
|
||||
private final static String ACTION_DISCONNECT = "no.nordicsemi.android.nrftoolbox.proximity.ACTION_DISCONNECT";
|
||||
private final static String ACTION_FIND = "no.nordicsemi.android.nrftoolbox.proximity.ACTION_FIND";
|
||||
private final static String ACTION_SILENT = "no.nordicsemi.android.nrftoolbox.proximity.ACTION_SILENT";
|
||||
@@ -93,11 +96,10 @@ public class ProximityService extends BleMulticonnectProfileService implements P
|
||||
* Toggles the Immediate Alert on given remote device.
|
||||
*
|
||||
* @param device the connected device.
|
||||
* @return True if alarm has been enabled, false if disabled.
|
||||
*/
|
||||
public boolean toggleImmediateAlert(final BluetoothDevice device) {
|
||||
public void toggleImmediateAlert(final BluetoothDevice device) {
|
||||
final ProximityManager manager = (ProximityManager) getBleManager(device);
|
||||
return manager.toggleImmediateAlert();
|
||||
manager.toggleImmediateAlert();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -165,7 +167,6 @@ public class ProximityService extends BleMulticonnectProfileService implements P
|
||||
break;
|
||||
}
|
||||
mBinder.toggleImmediateAlert(device);
|
||||
createNotificationForConnectedDevice(device);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -310,15 +311,27 @@ public class ProximityService extends BleMulticonnectProfileService implements P
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAlarmTriggered(final BluetoothDevice device) {
|
||||
public void onAlarmTriggered(@NonNull final BluetoothDevice device) {
|
||||
playAlarm(device);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAlarmStopped(final BluetoothDevice device) {
|
||||
public void onAlarmStopped(@NonNull final BluetoothDevice device) {
|
||||
stopAlarm(device);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRemoteAlarmSwitched(@NonNull final BluetoothDevice device, final boolean on) {
|
||||
final Intent broadcast = new Intent(BROADCAST_ALARM_SWITCHED);
|
||||
broadcast.putExtra(EXTRA_DEVICE, device);
|
||||
broadcast.putExtra(EXTRA_ALARM_STATE, on);
|
||||
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
|
||||
|
||||
if (!mBound) {
|
||||
createBackgroundNotification();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBatteryLevelChanged(@NonNull final BluetoothDevice device, final int batteryLevel) {
|
||||
final Intent broadcast = new Intent(BROADCAST_BATTERY_LEVEL);
|
||||
@@ -386,6 +399,7 @@ public class ProximityService extends BleMulticonnectProfileService implements P
|
||||
// If there are more, just write number of them
|
||||
text.append(getString(R.string.proximity_notification_text_nothing_connected_number_disconnected, numberOfDisconnectedDevices));
|
||||
}
|
||||
text.append(".");
|
||||
builder.setContentText(text);
|
||||
}
|
||||
|
||||
@@ -457,8 +471,8 @@ public class ProximityService extends BleMulticonnectProfileService implements P
|
||||
// This notification is to be shown not in a group
|
||||
|
||||
final String name = getDeviceName(device);
|
||||
builder.setContentTitle(getString(R.string.proximity_notification_linkloss_alert, name));
|
||||
builder.setTicker(getString(R.string.proximity_notification_linkloss_alert, name));
|
||||
builder.setContentTitle(getString(R.string.proximity_notification_link_loss_alert, name));
|
||||
builder.setTicker(getString(R.string.proximity_notification_link_loss_alert, name));
|
||||
|
||||
final Notification notification = builder.build();
|
||||
final NotificationManagerCompat nm = NotificationManagerCompat.from(this);
|
||||
|
||||
@@ -71,7 +71,8 @@
|
||||
<android.support.v7.widget.RecyclerView
|
||||
android:id="@android:id/list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
android:layout_height="match_parent"
|
||||
tools:listitem="@layout/activity_feature_proximity_item"/>
|
||||
</LinearLayout>
|
||||
|
||||
<Button
|
||||
@@ -83,7 +84,7 @@
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginBottom="@dimen/activity_vertical_margin_bottom"
|
||||
android:onClick="onAddDeviceClicked"
|
||||
android:text="@string/action_connect"/>
|
||||
android:text="@string/action_add_device"/>
|
||||
|
||||
<ImageView
|
||||
android:layout_width="wrap_content"
|
||||
|
||||
@@ -71,7 +71,8 @@
|
||||
<android.support.v7.widget.RecyclerView
|
||||
android:id="@android:id/list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
android:layout_height="match_parent"
|
||||
tools:listitem="@layout/activity_feature_proximity_item"/>
|
||||
</LinearLayout>
|
||||
|
||||
<Button
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="?android:attr/listPreferredItemHeightSmall"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:paddingBottom="2dp"
|
||||
android:paddingEnd="8dp"
|
||||
android:paddingStart="8dp"
|
||||
@@ -40,9 +41,9 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:ellipsize="marquee"
|
||||
android:layout_toRightOf="@+id/icon"
|
||||
android:gravity="center_vertical"
|
||||
android:textSize="20sp"
|
||||
tools:text="Nordic_Prox"
|
||||
android:textColor="@android:color/black"
|
||||
android:singleLine="true"/>
|
||||
|
||||
@@ -51,6 +52,7 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="12sp"
|
||||
tools:text="AA:BB:CC:DD:EE:FF"
|
||||
android:layout_below="@+id/name"
|
||||
android:lines="1"/>
|
||||
|
||||
@@ -64,24 +66,32 @@
|
||||
android:layout_toRightOf="@+id/address"
|
||||
android:drawableLeft="@drawable/ic_battery"
|
||||
android:layout_marginLeft="4dp"
|
||||
tools:visibility="visible"
|
||||
tools:text="100%"
|
||||
android:visibility="gone"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
<ImageButton
|
||||
<ProgressBar
|
||||
android:id="@+id/progress"
|
||||
style="?android:progressBarStyleSmall"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginRight="16dp"/>
|
||||
|
||||
<android.support.v7.widget.AppCompatImageButton
|
||||
android:id="@+id/action_find_silent"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:backgroundTint="@color/actionBarColorDark"
|
||||
app:backgroundTint="@color/actionBarColorDark"
|
||||
android:src="@drawable/ic_stat_notify_proximity_find"
|
||||
tools:visibility="visible"
|
||||
android:visibility="gone"/>
|
||||
|
||||
<ImageButton
|
||||
<android.support.v7.widget.AppCompatImageButton
|
||||
android:id="@+id/action_disconnect"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:backgroundTint="@color/orange"
|
||||
app:backgroundTint="@color/orange"
|
||||
android:src="@drawable/ic_action_disconnect"/>
|
||||
|
||||
|
||||
@@ -32,12 +32,12 @@
|
||||
<string name="proximity_action_silent">Silent</string>
|
||||
<string name="proximity_default_device_name">Proximity Tag</string>
|
||||
|
||||
<string name="proximity_notification_text_nothing_connected_one_disconnected">%s is disconnected.</string>
|
||||
<string name="proximity_notification_text_nothing_connected_number_disconnected">%d is disconnected.</string>
|
||||
<string name="proximity_notification_text">%s connected</string>
|
||||
<string name="proximity_notification_summary_text_name">%s is connected.</string>
|
||||
<string name="proximity_notification_summary_text_number">%d is connected.</string>
|
||||
<string name="proximity_notification_linkloss_alert">%s is getting away!</string>
|
||||
<string name="proximity_notification_text_nothing_connected_one_disconnected">%s is disconnected</string>
|
||||
<string name="proximity_notification_text_nothing_connected_number_disconnected">%d tags are disconnected</string>
|
||||
<string name="proximity_notification_text">%s is connected.</string>
|
||||
<string name="proximity_notification_summary_text_name">%s is connected</string>
|
||||
<string name="proximity_notification_summary_text_number">%d tags are connected</string>
|
||||
<string name="proximity_notification_link_loss_alert">%s is getting away!</string>
|
||||
|
||||
<string name="proximity_devices_title">YOUR TAGS</string>
|
||||
<string name="proximity_server_error">GATT Server failed to start (error %d).</string>
|
||||
|
||||
Reference in New Issue
Block a user