mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-28 21:06:50 +01:00
Support for finding a lost device (closes #42)
This commit is contained in:
parent
c7b4f295a1
commit
23d91ac79e
@ -161,4 +161,10 @@ public abstract class AbstractBTDeviceSupport extends AbstractDeviceSupport {
|
|||||||
byte[] bytes = gbDeviceProtocol.encodeReboot();
|
byte[] bytes = gbDeviceProtocol.encodeReboot();
|
||||||
sendToDevice(bytes);
|
sendToDevice(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFindDevice(boolean start) {
|
||||||
|
byte[] bytes = gbDeviceProtocol.encodeLocateDevice(start);
|
||||||
|
sendToDevice(bytes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,6 +59,7 @@ public class BluetoothCommunicationService extends Service {
|
|||||||
public static final String ACTION_REBOOT = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.reboot";
|
public static final String ACTION_REBOOT = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.reboot";
|
||||||
public static final String ACTION_FETCH_ACTIVITY_DATA = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.fetch_activity_data";
|
public static final String ACTION_FETCH_ACTIVITY_DATA = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.fetch_activity_data";
|
||||||
public static final String ACTION_DISCONNECT = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.disconnect";
|
public static final String ACTION_DISCONNECT = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.disconnect";
|
||||||
|
public static final String ACTION_FIND_DEVICE = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.find_device";
|
||||||
|
|
||||||
public static final String EXTRA_PERFORM_PAIR = "perform_pair";
|
public static final String EXTRA_PERFORM_PAIR = "perform_pair";
|
||||||
|
|
||||||
@ -208,6 +209,11 @@ public class BluetoothCommunicationService extends Service {
|
|||||||
mDeviceSupport = null;
|
mDeviceSupport = null;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case ACTION_FIND_DEVICE: {
|
||||||
|
boolean start = intent.getBooleanExtra("find_start", false);
|
||||||
|
mDeviceSupport.onFindDevice(start);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case ACTION_CALLSTATE:
|
case ACTION_CALLSTATE:
|
||||||
GBCommand command = GBCommand.values()[intent.getIntExtra("call_command", 0)]; // UGLY
|
GBCommand command = GBCommand.values()[intent.getIntExtra("call_command", 0)]; // UGLY
|
||||||
|
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
package nodomain.freeyourgadget.gadgetbridge;
|
package nodomain.freeyourgadget.gadgetbridge;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.app.ProgressDialog;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
@ -198,11 +201,32 @@ public class ControlCenter extends Activity {
|
|||||||
startService(startIntent);
|
startService(startIntent);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
case R.id.controlcenter_find_device:
|
||||||
|
if (selectedDevice != null) {
|
||||||
|
findDevice(true);
|
||||||
|
ProgressDialog dialog = ProgressDialog.show(
|
||||||
|
this,
|
||||||
|
getString(R.string.control_center_find_lost_device),
|
||||||
|
getString(R.string.control_center_cancel_to_stop_vibration),
|
||||||
|
true, true,
|
||||||
|
new DialogInterface.OnCancelListener() {
|
||||||
|
@Override
|
||||||
|
public void onCancel(DialogInterface dialog) {
|
||||||
|
findDevice(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return super.onContextItemSelected(item);
|
return super.onContextItemSelected(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void findDevice(boolean start) {
|
||||||
|
Intent startIntent = new Intent(this, BluetoothCommunicationService.class);
|
||||||
|
startIntent.putExtra("find_start", start);
|
||||||
|
startIntent.setAction(BluetoothCommunicationService.ACTION_FIND_DEVICE);
|
||||||
|
startService(startIntent);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
|
@ -34,4 +34,6 @@ public interface EventHandler {
|
|||||||
void onFetchActivityData();
|
void onFetchActivityData();
|
||||||
|
|
||||||
void onReboot();
|
void onReboot();
|
||||||
|
|
||||||
|
void onFindDevice(boolean start);
|
||||||
}
|
}
|
||||||
|
@ -210,4 +210,12 @@ public class ServiceDeviceSupport implements DeviceSupport {
|
|||||||
}
|
}
|
||||||
delegate.onReboot();
|
delegate.onReboot();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFindDevice(boolean start) {
|
||||||
|
if (checkBusy("find device")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
delegate.onFindDevice(start);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,6 +69,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
|||||||
//same as above, but remains untouched for the ack message
|
//same as above, but remains untouched for the ack message
|
||||||
private GregorianCalendar activityDataTimestampToAck = null;
|
private GregorianCalendar activityDataTimestampToAck = null;
|
||||||
private volatile boolean telephoneRinging;
|
private volatile boolean telephoneRinging;
|
||||||
|
private volatile boolean isLocatingDevice;
|
||||||
|
|
||||||
|
|
||||||
public MiBandSupport() {
|
public MiBandSupport() {
|
||||||
@ -135,10 +136,15 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
|||||||
return getNotification(vibrateDuration, vibrateTimes, flashTimes, flashColour, originalColour, flashDuration);
|
return getNotification(vibrateDuration, vibrateTimes, flashTimes, flashColour, originalColour, flashDuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendDefaultNotification(TransactionBuilder builder) {
|
private void sendDefaultNotification(TransactionBuilder builder, short repeat, BtLEAction extraAction) {
|
||||||
BluetoothGattCharacteristic characteristic = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT);
|
BluetoothGattCharacteristic characteristic = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT);
|
||||||
LOG.info("Sending notification to MiBand: " + characteristic);
|
LOG.info("Sending notification to MiBand: " + characteristic + " (" + repeat + " times)");
|
||||||
builder.write(characteristic, getDefaultNotification()).queue(getQueue());
|
byte[] defaultNotification = getDefaultNotification();
|
||||||
|
for (short i = 0; i < repeat; i++) {
|
||||||
|
builder.write(characteristic, defaultNotification);
|
||||||
|
builder.add(extraAction);
|
||||||
|
}
|
||||||
|
builder.queue(getQueue());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -153,7 +159,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
|||||||
*/
|
*/
|
||||||
private void sendCustomNotification(VibrationProfile vibrationProfile, int flashTimes, int flashColour, int originalColour, long flashDuration, BtLEAction extraAction, TransactionBuilder builder) {
|
private void sendCustomNotification(VibrationProfile vibrationProfile, int flashTimes, int flashColour, int originalColour, long flashDuration, BtLEAction extraAction, TransactionBuilder builder) {
|
||||||
BluetoothGattCharacteristic controlPoint = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT);
|
BluetoothGattCharacteristic controlPoint = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT);
|
||||||
for (byte i = 0; i < vibrationProfile.getRepeat(); i++) {
|
for (short i = 0; i < vibrationProfile.getRepeat(); i++) {
|
||||||
int[] onOffSequence = vibrationProfile.getOnOffSequence();
|
int[] onOffSequence = vibrationProfile.getOnOffSequence();
|
||||||
for (int j = 0; j < onOffSequence.length; j++) {
|
for (int j = 0; j < onOffSequence.length; j++) {
|
||||||
int on = onOffSequence[j];
|
int on = onOffSequence[j];
|
||||||
@ -245,10 +251,10 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void performDefaultNotification(String task) {
|
private void performDefaultNotification(String task, short repeat, BtLEAction extraAction) {
|
||||||
try {
|
try {
|
||||||
TransactionBuilder builder = performInitialized(task);
|
TransactionBuilder builder = performInitialized(task);
|
||||||
sendDefaultNotification(builder);
|
sendDefaultNotification(builder, repeat, extraAction);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
LOG.error("Unable to send notification to MI device", ex);
|
LOG.error("Unable to send notification to MI device", ex);
|
||||||
}
|
}
|
||||||
@ -314,7 +320,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
|||||||
|
|
||||||
private VibrationProfile getPreferredVibrateProfile(String notificationOrigin, SharedPreferences prefs, int repeat) {
|
private VibrationProfile getPreferredVibrateProfile(String notificationOrigin, SharedPreferences prefs, int repeat) {
|
||||||
String profileId = getNotificationPrefStringValue(VIBRATION_PROFILE, notificationOrigin, prefs, DEFAULT_VALUE_VIBRATION_PROFILE);
|
String profileId = getNotificationPrefStringValue(VIBRATION_PROFILE, notificationOrigin, prefs, DEFAULT_VALUE_VIBRATION_PROFILE);
|
||||||
return VibrationProfile.getProfile(profileId, (byte) repeat);
|
return VibrationProfile.getProfile(profileId, (byte) (repeat & 0xfff));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -432,6 +438,21 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFindDevice(boolean start) {
|
||||||
|
isLocatingDevice = start;
|
||||||
|
|
||||||
|
if (start) {
|
||||||
|
AbortTransactionAction abortAction = new AbortTransactionAction() {
|
||||||
|
@Override
|
||||||
|
protected boolean shouldAbort() {
|
||||||
|
return !isLocatingDevice;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
performDefaultNotification("locating device", (short) 255, abortAction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFetchActivityData() {
|
public void onFetchActivityData() {
|
||||||
try {
|
try {
|
||||||
|
@ -41,7 +41,7 @@ public class VibrationProfile {
|
|||||||
private final String id;
|
private final String id;
|
||||||
|
|
||||||
private int[] onOffSequence;
|
private int[] onOffSequence;
|
||||||
private byte repeat; // 1-10
|
private short repeat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new profile instance.
|
* Creates a new profile instance.
|
||||||
@ -49,7 +49,7 @@ public class VibrationProfile {
|
|||||||
* @param onOffSequence a sequence of alternating on and off durations, in milliseconds
|
* @param onOffSequence a sequence of alternating on and off durations, in milliseconds
|
||||||
* @param repeat how ofoften the sequence shall be repeated
|
* @param repeat how ofoften the sequence shall be repeated
|
||||||
*/
|
*/
|
||||||
public VibrationProfile(String id, int[] onOffSequence, byte repeat) {
|
public VibrationProfile(String id, int[] onOffSequence, short repeat) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.repeat = repeat;
|
this.repeat = repeat;
|
||||||
this.onOffSequence = onOffSequence;
|
this.onOffSequence = onOffSequence;
|
||||||
@ -63,7 +63,7 @@ public class VibrationProfile {
|
|||||||
return onOffSequence;
|
return onOffSequence;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte getRepeat() {
|
public short getRepeat() {
|
||||||
return repeat;
|
return repeat;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,6 +62,10 @@ public abstract class GBDeviceProtocol {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] encodeLocateDevice(boolean start) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public GBDeviceCommand decodeResponse(byte[] responseData) {
|
public GBDeviceCommand decodeResponse(byte[] responseData) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,9 @@
|
|||||||
<item
|
<item
|
||||||
android:id="@+id/controlcenter_start_sleepmonitor"
|
android:id="@+id/controlcenter_start_sleepmonitor"
|
||||||
android:title="@string/controlcenter_start_sleepmonitor"/>
|
android:title="@string/controlcenter_start_sleepmonitor"/>
|
||||||
|
<item
|
||||||
|
android:id="@+id/controlcenter_find_device"
|
||||||
|
android:title="@string/control_center_find_device"/>
|
||||||
<item
|
<item
|
||||||
android:id="@+id/controlcenter_disconnect"
|
android:id="@+id/controlcenter_disconnect"
|
||||||
android:title="@string/controlcenter_disconnect"/>
|
android:title="@string/controlcenter_disconnect"/>
|
||||||
|
@ -133,5 +133,8 @@
|
|||||||
<string name="pref_screen_notification_profile_pebblemsg">Pebble Notification</string>
|
<string name="pref_screen_notification_profile_pebblemsg">Pebble Notification</string>
|
||||||
<string name="pref_screen_notification_profile_k9mail">K9 Mail Notification</string>
|
<string name="pref_screen_notification_profile_k9mail">K9 Mail Notification</string>
|
||||||
<string name="pref_screen_notification_profile_incoming_call">Incoming Call Notification</string>
|
<string name="pref_screen_notification_profile_incoming_call">Incoming Call Notification</string>
|
||||||
|
<string name="control_center_find_device">Find Device…</string>
|
||||||
|
<string name="control_center_find_lost_device">Find lost Device</string>
|
||||||
|
<string name="control_center_cancel_to_stop_vibration">Cancel to stop vibration.</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
Loading…
Reference in New Issue
Block a user