2018-02-26 14:27:32 +01:00
|
|
|
/* Copyright (C) 2017-2018 Andreas Shimokawa, Carsten Pfeiffer
|
2017-08-13 16:31:11 +02:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2018-06-14 18:16:49 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service.devices.huami.amazfitbip;
|
2017-08-13 16:31:11 +02:00
|
|
|
|
2017-08-29 22:16:59 +02:00
|
|
|
import android.bluetooth.BluetoothGatt;
|
|
|
|
import android.bluetooth.BluetoothGattCharacteristic;
|
2017-10-24 22:01:25 +02:00
|
|
|
import android.content.Context;
|
2017-08-25 00:21:47 +02:00
|
|
|
import android.net.Uri;
|
|
|
|
|
2017-08-21 23:47:47 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2017-08-19 20:58:13 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.ByteOrder;
|
2017-09-28 15:44:07 +02:00
|
|
|
import java.util.Locale;
|
2018-04-27 09:28:33 +02:00
|
|
|
import java.util.Set;
|
2017-08-26 00:42:04 +02:00
|
|
|
import java.util.SimpleTimeZone;
|
2017-08-29 22:16:59 +02:00
|
|
|
import java.util.UUID;
|
2017-08-19 20:58:13 +02:00
|
|
|
|
2017-11-01 23:32:54 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2017-10-24 22:01:25 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper;
|
2017-10-23 10:28:54 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiWeatherConditions;
|
2017-10-24 22:01:25 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.huami.amazfitbip.AmazfitBipFWHelper;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.huami.amazfitbip.AmazfitBipService;
|
2017-08-29 22:16:59 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBand2Service;
|
2017-08-16 20:55:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
|
2018-02-08 22:21:15 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
2017-08-18 16:21:54 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
|
2018-03-31 16:21:25 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.RecordedDataTypes;
|
2017-11-30 18:26:10 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.Weather;
|
2017-08-19 20:58:13 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
|
2018-02-08 22:21:15 +01:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.ConditionalWriteAction;
|
2017-08-21 23:47:47 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertCategory;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.NewAlert;
|
2017-10-24 22:01:25 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiIcon;
|
2018-08-01 22:56:01 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiSupport;
|
2018-07-16 14:36:52 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.amazfitbip.operations.AmazfitBipFetchLogsOperation;
|
2018-08-01 22:56:01 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.operations.FetchActivityOperation;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.operations.FetchSportsSummaryOperation;
|
2018-07-16 14:36:52 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.NotificationStrategy;
|
2018-04-27 09:28:33 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
2017-08-18 16:21:54 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
|
2017-08-25 23:35:16 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Version;
|
2017-08-13 16:31:11 +02:00
|
|
|
|
2018-08-01 22:56:01 +02:00
|
|
|
public class AmazfitBipSupport extends HuamiSupport {
|
2017-08-21 23:47:47 +02:00
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(AmazfitBipSupport.class);
|
|
|
|
|
2017-09-27 21:46:23 +02:00
|
|
|
public AmazfitBipSupport() {
|
|
|
|
super(LOG);
|
|
|
|
}
|
|
|
|
|
2017-08-13 16:31:11 +02:00
|
|
|
@Override
|
|
|
|
public NotificationStrategy getNotificationStrategy() {
|
|
|
|
return new AmazfitBipTextNotificationStrategy(this);
|
|
|
|
}
|
|
|
|
|
2017-08-18 16:21:54 +02:00
|
|
|
@Override
|
|
|
|
public void onNotification(NotificationSpec notificationSpec) {
|
|
|
|
if (notificationSpec.type == NotificationType.GENERIC_ALARM_CLOCK) {
|
|
|
|
onAlarmClock(notificationSpec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-20 23:58:41 +02:00
|
|
|
String senderOrTiltle = StringUtils.getFirstOf(notificationSpec.sender, notificationSpec.title);
|
2017-08-18 16:21:54 +02:00
|
|
|
|
2017-08-21 23:47:47 +02:00
|
|
|
String message = StringUtils.truncate(senderOrTiltle, 32) + "\0";
|
2017-08-20 23:58:41 +02:00
|
|
|
if (notificationSpec.subject != null) {
|
|
|
|
message += StringUtils.truncate(notificationSpec.subject, 128) + "\n\n";
|
2017-08-18 16:21:54 +02:00
|
|
|
}
|
2017-08-20 23:58:41 +02:00
|
|
|
if (notificationSpec.body != null) {
|
|
|
|
message += StringUtils.truncate(notificationSpec.body, 128);
|
|
|
|
}
|
|
|
|
|
2017-08-21 23:47:47 +02:00
|
|
|
try {
|
|
|
|
TransactionBuilder builder = performInitialized("new notification");
|
|
|
|
AlertNotificationProfile<?> profile = new AlertNotificationProfile(this);
|
2017-09-11 23:18:07 +02:00
|
|
|
profile.setMaxLength(230);
|
2017-08-21 23:47:47 +02:00
|
|
|
|
2017-10-21 22:50:28 +02:00
|
|
|
byte customIconId = HuamiIcon.mapToIconId(notificationSpec.type);
|
2017-08-21 23:47:47 +02:00
|
|
|
|
2017-10-22 00:02:36 +02:00
|
|
|
AlertCategory alertCategory = AlertCategory.CustomHuami;
|
2017-08-21 23:47:47 +02:00
|
|
|
|
|
|
|
// The SMS icon for AlertCategory.SMS is unique and not available as iconId
|
|
|
|
if (notificationSpec.type == NotificationType.GENERIC_SMS) {
|
|
|
|
alertCategory = AlertCategory.SMS;
|
|
|
|
}
|
2017-09-02 23:24:52 +02:00
|
|
|
// EMAIL icon does not work in FW 0.0.8.74, it did in 0.0.7.90
|
2017-10-21 22:50:28 +02:00
|
|
|
else if (customIconId == HuamiIcon.EMAIL) {
|
2017-09-02 23:24:52 +02:00
|
|
|
alertCategory = AlertCategory.Email;
|
|
|
|
}
|
2017-08-21 23:47:47 +02:00
|
|
|
|
|
|
|
NewAlert alert = new NewAlert(alertCategory, 1, message, customIconId);
|
|
|
|
profile.newAlert(builder, alert);
|
|
|
|
builder.queue(getQueue());
|
|
|
|
} catch (IOException ex) {
|
|
|
|
LOG.error("Unable to send notification to Amazfit Bip", ex);
|
|
|
|
}
|
2017-08-18 16:21:54 +02:00
|
|
|
}
|
|
|
|
|
2017-08-13 16:31:11 +02:00
|
|
|
@Override
|
|
|
|
public void onFindDevice(boolean start) {
|
2017-08-16 20:55:20 +02:00
|
|
|
CallSpec callSpec = new CallSpec();
|
|
|
|
callSpec.command = start ? CallSpec.CALL_INCOMING : CallSpec.CALL_END;
|
|
|
|
callSpec.name = "Gadgetbridge";
|
|
|
|
onSetCallState(callSpec);
|
2017-08-13 16:31:11 +02:00
|
|
|
}
|
2017-08-15 17:23:12 +02:00
|
|
|
|
|
|
|
@Override
|
2017-10-21 22:50:28 +02:00
|
|
|
public void handleButtonEvent() {
|
|
|
|
// ignore
|
2017-08-15 17:23:12 +02:00
|
|
|
}
|
2017-08-19 20:58:13 +02:00
|
|
|
|
2018-01-20 01:34:40 +01:00
|
|
|
@Override
|
|
|
|
protected AmazfitBipSupport setDisplayItems(TransactionBuilder builder) {
|
2018-04-27 09:28:33 +02:00
|
|
|
if (gbDevice.getType() != DeviceType.AMAZFITBIP) {
|
|
|
|
return this; // Disable for Cor for now
|
|
|
|
}
|
|
|
|
if (gbDevice.getFirmwareVersion() == null) {
|
|
|
|
LOG.warn("Device not initialized yet, won't set menu items");
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Version version = new Version(gbDevice.getFirmwareVersion());
|
2018-04-27 21:05:12 +02:00
|
|
|
if (version.compareTo(new Version("0.1.1.14")) < 0) {
|
2018-04-27 09:28:33 +02:00
|
|
|
LOG.warn("Won't set menu items since firmware is too low to be safe");
|
|
|
|
return this;
|
|
|
|
}
|
2018-01-20 01:34:40 +01:00
|
|
|
|
2018-04-27 09:28:33 +02:00
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
Set<String> pages = prefs.getStringSet("bip_display_items", null);
|
|
|
|
LOG.info("Setting display items to " + (pages == null ? "none" : pages));
|
|
|
|
byte[] command = AmazfitBipService.COMMAND_CHANGE_SCREENS.clone();
|
2018-01-20 01:34:40 +01:00
|
|
|
|
2018-05-15 23:12:18 +02:00
|
|
|
boolean shortcut_weather = false;
|
|
|
|
boolean shortcut_alipay = false;
|
|
|
|
|
2018-04-27 09:28:33 +02:00
|
|
|
if (pages != null) {
|
|
|
|
if (pages.contains("status")) {
|
|
|
|
command[1] |= 0x02;
|
|
|
|
}
|
|
|
|
if (pages.contains("activity")) {
|
|
|
|
command[1] |= 0x04;
|
|
|
|
}
|
|
|
|
if (pages.contains("weather")) {
|
|
|
|
command[1] |= 0x08;
|
|
|
|
}
|
|
|
|
if (pages.contains("alarm")) {
|
|
|
|
command[1] |= 0x10;
|
|
|
|
}
|
|
|
|
if (pages.contains("timer")) {
|
|
|
|
command[1] |= 0x20;
|
|
|
|
}
|
|
|
|
if (pages.contains("compass")) {
|
|
|
|
command[1] |= 0x40;
|
|
|
|
}
|
|
|
|
if (pages.contains("settings")) {
|
|
|
|
command[1] |= 0x80;
|
|
|
|
}
|
|
|
|
if (pages.contains("alipay")) {
|
|
|
|
command[2] |= 0x01;
|
|
|
|
}
|
2018-05-15 23:12:18 +02:00
|
|
|
if (pages.contains("shortcut_weather")) {
|
|
|
|
shortcut_weather = true;
|
|
|
|
}
|
|
|
|
if (pages.contains("shortcut_alipay")) {
|
|
|
|
shortcut_alipay = true;
|
|
|
|
}
|
2018-04-27 09:28:33 +02:00
|
|
|
}
|
|
|
|
builder.write(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_3_CONFIGURATION), command);
|
2018-05-15 23:12:18 +02:00
|
|
|
setShortcuts(builder, shortcut_weather, shortcut_alipay);
|
|
|
|
|
2018-01-20 01:34:40 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-05-15 23:12:18 +02:00
|
|
|
private void setShortcuts(TransactionBuilder builder, boolean weather, boolean alipay) {
|
|
|
|
LOG.info("Setting shortcuts: weather=" + weather + " alipay=" + alipay);
|
|
|
|
|
|
|
|
// Basically a hack to put weather first always, if alipay is the only enabled one
|
|
|
|
// there are actually two alipays set but the second one disabled.... :P
|
|
|
|
byte[] command = new byte[]{0x10,
|
|
|
|
(byte) ((alipay || weather) ? 0x80 : 0x00), (byte) (weather ? 0x02 : 0x01),
|
|
|
|
(byte) ((alipay && weather) ? 0x81 : 0x01), 0x01,
|
|
|
|
};
|
|
|
|
|
|
|
|
builder.write(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_3_CONFIGURATION), command);
|
|
|
|
}
|
|
|
|
|
2017-08-26 00:42:04 +02:00
|
|
|
@Override
|
2017-08-19 20:58:13 +02:00
|
|
|
public void onSendWeather(WeatherSpec weatherSpec) {
|
2017-09-04 23:23:51 +02:00
|
|
|
if (gbDevice.getFirmwareVersion() == null) {
|
|
|
|
LOG.warn("Device not initialized yet, so not sending weather info");
|
|
|
|
return;
|
|
|
|
}
|
2017-12-04 22:31:36 +01:00
|
|
|
boolean supportsConditionString = false;
|
2017-09-04 23:23:51 +02:00
|
|
|
|
2017-12-04 22:31:36 +01:00
|
|
|
Version version = new Version(gbDevice.getFirmwareVersion());
|
|
|
|
if (version.compareTo(new Version("0.0.8.74")) >= 0) {
|
|
|
|
supportsConditionString = true;
|
|
|
|
}
|
|
|
|
int tz_offset_hours = SimpleTimeZone.getDefault().getOffset(weatherSpec.timestamp * 1000L) / (1000 * 60 * 60);
|
2017-08-19 20:58:13 +02:00
|
|
|
try {
|
2017-12-04 22:31:36 +01:00
|
|
|
TransactionBuilder builder;
|
|
|
|
builder = performInitialized("Sending current temp");
|
2017-09-03 23:56:30 +02:00
|
|
|
|
2017-12-04 22:31:36 +01:00
|
|
|
byte condition = HuamiWeatherConditions.mapToAmazfitBipWeatherCode(weatherSpec.currentConditionCode);
|
|
|
|
|
|
|
|
int length = 8;
|
|
|
|
if (supportsConditionString) {
|
|
|
|
length += weatherSpec.currentCondition.getBytes().length + 1;
|
2017-08-25 23:35:16 +02:00
|
|
|
}
|
2017-12-04 22:31:36 +01:00
|
|
|
ByteBuffer buf = ByteBuffer.allocate(length);
|
|
|
|
buf.order(ByteOrder.LITTLE_ENDIAN);
|
|
|
|
|
|
|
|
buf.put((byte) 2);
|
|
|
|
buf.putInt(weatherSpec.timestamp);
|
|
|
|
buf.put((byte) (tz_offset_hours * 4));
|
|
|
|
buf.put(condition);
|
|
|
|
buf.put((byte) (weatherSpec.currentTemp - 273));
|
2017-09-03 23:56:30 +02:00
|
|
|
|
2017-12-04 22:31:36 +01:00
|
|
|
if (supportsConditionString) {
|
|
|
|
buf.put(weatherSpec.currentCondition.getBytes());
|
|
|
|
buf.put((byte) 0);
|
2017-08-25 23:35:16 +02:00
|
|
|
}
|
|
|
|
|
2017-12-04 22:31:36 +01:00
|
|
|
builder.write(getCharacteristic(AmazfitBipService.UUID_CHARACTERISTIC_WEATHER), buf.array());
|
|
|
|
builder.queue(getQueue());
|
2017-12-08 08:26:10 +01:00
|
|
|
} catch (Exception ex) {
|
|
|
|
LOG.error("Error sending current weather", ex);
|
|
|
|
}
|
|
|
|
|
2018-07-16 14:19:02 +02:00
|
|
|
if (gbDevice.getType() != DeviceType.AMAZFITCOR) {
|
2018-03-24 19:28:06 +01:00
|
|
|
try {
|
|
|
|
TransactionBuilder builder;
|
|
|
|
builder = performInitialized("Sending air quality index");
|
|
|
|
int length = 8;
|
|
|
|
String aqiString = "(n/a)";
|
|
|
|
if (supportsConditionString) {
|
|
|
|
length += aqiString.getBytes().length + 1;
|
|
|
|
}
|
|
|
|
ByteBuffer buf = ByteBuffer.allocate(length);
|
|
|
|
buf.order(ByteOrder.LITTLE_ENDIAN);
|
|
|
|
buf.put((byte) 4);
|
|
|
|
buf.putInt(weatherSpec.timestamp);
|
|
|
|
buf.put((byte) (tz_offset_hours * 4));
|
|
|
|
buf.putShort((short) 0);
|
|
|
|
if (supportsConditionString) {
|
|
|
|
buf.put(aqiString.getBytes());
|
|
|
|
buf.put((byte) 0);
|
|
|
|
}
|
|
|
|
builder.write(getCharacteristic(AmazfitBipService.UUID_CHARACTERISTIC_WEATHER), buf.array());
|
|
|
|
builder.queue(getQueue());
|
|
|
|
} catch (IOException ex) {
|
|
|
|
LOG.error("Error sending air quality");
|
2017-12-08 08:26:10 +01:00
|
|
|
}
|
2017-12-04 22:31:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
TransactionBuilder builder = performInitialized("Sending weather forecast");
|
2017-08-25 23:35:16 +02:00
|
|
|
|
2017-11-29 23:57:36 +01:00
|
|
|
final byte NR_DAYS = (byte) (1 + weatherSpec.forecasts.size());
|
2017-08-25 23:35:16 +02:00
|
|
|
int bytesPerDay = 4;
|
2017-11-30 18:26:10 +01:00
|
|
|
|
2017-08-25 23:35:16 +02:00
|
|
|
int conditionsLength = 0;
|
|
|
|
if (supportsConditionString) {
|
|
|
|
bytesPerDay = 5;
|
|
|
|
conditionsLength = weatherSpec.currentCondition.getBytes().length;
|
2017-11-30 18:26:10 +01:00
|
|
|
for (WeatherSpec.Forecast forecast : weatherSpec.forecasts) {
|
|
|
|
conditionsLength += Weather.getConditionString(forecast.conditionCode).getBytes().length;
|
|
|
|
}
|
2017-08-25 23:35:16 +02:00
|
|
|
}
|
2017-11-30 18:26:10 +01:00
|
|
|
|
2017-08-25 23:35:16 +02:00
|
|
|
int length = 7 + bytesPerDay * NR_DAYS + conditionsLength;
|
|
|
|
ByteBuffer buf = ByteBuffer.allocate(length);
|
|
|
|
|
2017-08-19 20:58:13 +02:00
|
|
|
buf.order(ByteOrder.LITTLE_ENDIAN);
|
|
|
|
buf.put((byte) 1);
|
|
|
|
buf.putInt(weatherSpec.timestamp);
|
2017-08-26 00:42:04 +02:00
|
|
|
buf.put((byte) (tz_offset_hours * 4));
|
2017-08-19 20:58:13 +02:00
|
|
|
|
|
|
|
buf.put(NR_DAYS);
|
|
|
|
|
2017-10-23 10:28:54 +02:00
|
|
|
byte condition = HuamiWeatherConditions.mapToAmazfitBipWeatherCode(weatherSpec.currentConditionCode);
|
2017-08-19 23:49:19 +02:00
|
|
|
buf.put(condition);
|
|
|
|
buf.put(condition);
|
2017-08-19 20:58:13 +02:00
|
|
|
buf.put((byte) (weatherSpec.todayMaxTemp - 273));
|
|
|
|
buf.put((byte) (weatherSpec.todayMinTemp - 273));
|
2017-08-25 23:35:16 +02:00
|
|
|
if (supportsConditionString) {
|
|
|
|
buf.put(weatherSpec.currentCondition.getBytes());
|
2017-11-30 18:26:10 +01:00
|
|
|
buf.put((byte) 0);
|
2017-08-25 23:35:16 +02:00
|
|
|
}
|
2017-08-19 23:49:19 +02:00
|
|
|
|
2017-11-29 23:57:36 +01:00
|
|
|
for (WeatherSpec.Forecast forecast : weatherSpec.forecasts) {
|
|
|
|
condition = HuamiWeatherConditions.mapToAmazfitBipWeatherCode(forecast.conditionCode);
|
|
|
|
buf.put(condition);
|
|
|
|
buf.put(condition);
|
|
|
|
buf.put((byte) (forecast.maxTemp - 273));
|
|
|
|
buf.put((byte) (forecast.minTemp - 273));
|
|
|
|
if (supportsConditionString) {
|
2017-11-30 18:26:10 +01:00
|
|
|
buf.put(Weather.getConditionString(forecast.conditionCode).getBytes());
|
|
|
|
buf.put((byte) 0);
|
2017-11-29 23:57:36 +01:00
|
|
|
}
|
2017-08-25 23:35:16 +02:00
|
|
|
}
|
2017-08-19 20:58:13 +02:00
|
|
|
|
2018-07-21 17:18:08 +02:00
|
|
|
if (characteristicChunked != null) {
|
|
|
|
writeToChunked(builder, 1, buf.array());
|
|
|
|
} else {
|
|
|
|
builder.write(getCharacteristic(AmazfitBipService.UUID_CHARACTERISTIC_WEATHER), buf.array());
|
|
|
|
}
|
|
|
|
|
2017-08-19 20:58:13 +02:00
|
|
|
builder.queue(getQueue());
|
2017-09-03 23:50:35 +02:00
|
|
|
} catch (Exception ex) {
|
2017-12-08 08:26:10 +01:00
|
|
|
LOG.error("Error sending weather forecast", ex);
|
2017-08-19 20:58:13 +02:00
|
|
|
}
|
2018-03-24 19:22:49 +01:00
|
|
|
|
|
|
|
if (gbDevice.getType() == DeviceType.AMAZFITCOR) {
|
|
|
|
try {
|
|
|
|
TransactionBuilder builder;
|
|
|
|
builder = performInitialized("Sending forecast location");
|
|
|
|
|
|
|
|
int length = 2 + weatherSpec.location.getBytes().length;
|
|
|
|
ByteBuffer buf = ByteBuffer.allocate(length);
|
|
|
|
buf.order(ByteOrder.LITTLE_ENDIAN);
|
|
|
|
buf.put((byte) 8);
|
|
|
|
buf.put(weatherSpec.location.getBytes());
|
|
|
|
buf.put((byte) 0);
|
|
|
|
|
|
|
|
builder.write(getCharacteristic(AmazfitBipService.UUID_CHARACTERISTIC_WEATHER), buf.array());
|
|
|
|
builder.queue(getQueue());
|
|
|
|
} catch (Exception ex) {
|
|
|
|
LOG.error("Error sending current forecast location", ex);
|
|
|
|
}
|
|
|
|
}
|
2017-08-19 20:58:13 +02:00
|
|
|
}
|
2017-08-29 22:16:59 +02:00
|
|
|
|
2017-09-18 23:24:11 +02:00
|
|
|
@Override
|
2018-03-31 16:21:25 +02:00
|
|
|
public void onFetchRecordedData(int dataTypes) {
|
2017-09-18 23:24:11 +02:00
|
|
|
try {
|
2018-03-31 16:21:25 +02:00
|
|
|
// FIXME: currently only one data type supported, these are meant to be flags
|
|
|
|
if (dataTypes == RecordedDataTypes.TYPE_ACTIVITY) {
|
|
|
|
new FetchActivityOperation(this).perform();
|
|
|
|
} else if (dataTypes == RecordedDataTypes.TYPE_GPS_TRACKS) {
|
|
|
|
new FetchSportsSummaryOperation(this).perform();
|
|
|
|
} else if (dataTypes == RecordedDataTypes.TYPE_DEBUGLOGS) {
|
|
|
|
new AmazfitBipFetchLogsOperation(this).perform();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG.warn("fetching multiple data types at once is not supported yet");
|
|
|
|
}
|
2017-09-18 23:24:11 +02:00
|
|
|
} catch (IOException ex) {
|
2018-03-31 16:21:25 +02:00
|
|
|
LOG.error("Unable to fetch recorded data types" + dataTypes, ex);
|
2017-09-18 23:24:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 22:16:59 +02:00
|
|
|
@Override
|
|
|
|
public boolean onCharacteristicChanged(BluetoothGatt gatt,
|
|
|
|
BluetoothGattCharacteristic characteristic) {
|
|
|
|
boolean handled = super.onCharacteristicChanged(gatt, characteristic);
|
|
|
|
if (!handled) {
|
|
|
|
UUID characteristicUUID = characteristic.getUuid();
|
|
|
|
if (MiBand2Service.UUID_CHARACTERISTIC_3_CONFIGURATION.equals(characteristicUUID)) {
|
|
|
|
return handleConfigurationInfo(characteristic.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean handleConfigurationInfo(byte[] value) {
|
|
|
|
if (value == null || value.length < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (value[0] == 0x10 && value[1] == 0x0e && value[2] == 0x01) {
|
|
|
|
String gpsVersion = new String(value, 3, value.length - 3);
|
|
|
|
LOG.info("got gps version = " + gpsVersion);
|
|
|
|
gbDevice.setFirmwareVersion2(gpsVersion);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this probably does more than only getting the GPS version...
|
|
|
|
private AmazfitBipSupport requestGPSVersion(TransactionBuilder builder) {
|
|
|
|
LOG.info("Requesting GPS version");
|
|
|
|
builder.write(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_3_CONFIGURATION), AmazfitBipService.COMMAND_REQUEST_GPS_VERSION);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-09-28 15:44:07 +02:00
|
|
|
private AmazfitBipSupport setLanguage(TransactionBuilder builder) {
|
2018-02-08 22:21:15 +01:00
|
|
|
|
2017-09-28 15:44:07 +02:00
|
|
|
String language = Locale.getDefault().getLanguage();
|
|
|
|
String country = Locale.getDefault().getCountry();
|
|
|
|
|
|
|
|
LOG.info("Setting watch language, phone language = " + language + " country = " + country);
|
|
|
|
|
2018-02-08 22:21:15 +01:00
|
|
|
final byte[] command_new;
|
|
|
|
final byte[] command_old;
|
|
|
|
String localeString;
|
|
|
|
|
2017-11-01 23:32:54 +01:00
|
|
|
switch (GBApplication.getPrefs().getInt("amazfitbip_language", -1)) {
|
|
|
|
case 0:
|
2018-02-08 22:21:15 +01:00
|
|
|
command_old = AmazfitBipService.COMMAND_SET_LANGUAGE_SIMPLIFIED_CHINESE;
|
|
|
|
localeString = "zh_CN";
|
2017-11-01 23:32:54 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
2018-02-08 22:21:15 +01:00
|
|
|
command_old = AmazfitBipService.COMMAND_SET_LANGUAGE_TRADITIONAL_CHINESE;
|
|
|
|
localeString = "zh_TW";
|
2017-11-01 23:32:54 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
2018-02-08 22:21:15 +01:00
|
|
|
command_old = AmazfitBipService.COMMAND_SET_LANGUAGE_ENGLISH;
|
|
|
|
localeString = "en_US";
|
2017-11-01 23:32:54 +01:00
|
|
|
break;
|
2018-01-30 23:06:08 +01:00
|
|
|
case 3:
|
2018-02-08 22:21:15 +01:00
|
|
|
command_old = AmazfitBipService.COMMAND_SET_LANGUAGE_SPANISH;
|
|
|
|
localeString = "es_ES";
|
2017-11-01 23:32:54 +01:00
|
|
|
break;
|
2018-07-16 14:36:52 +02:00
|
|
|
case 4:
|
|
|
|
command_old = AmazfitBipService.COMMAND_SET_LANGUAGE_ENGLISH;
|
|
|
|
localeString = "ru_RU";
|
|
|
|
break;
|
2017-11-01 23:32:54 +01:00
|
|
|
default:
|
2018-02-08 22:21:15 +01:00
|
|
|
switch (language) {
|
|
|
|
case "zh":
|
|
|
|
if (country.equals("TW") || country.equals("HK") || country.equals("MO")) { // Taiwan, Hong Kong, Macao
|
|
|
|
command_old = AmazfitBipService.COMMAND_SET_LANGUAGE_TRADITIONAL_CHINESE;
|
|
|
|
localeString = "zh_TW";
|
|
|
|
} else {
|
|
|
|
command_old = AmazfitBipService.COMMAND_SET_LANGUAGE_SIMPLIFIED_CHINESE;
|
|
|
|
localeString = "zh_CN";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "es":
|
|
|
|
command_old = AmazfitBipService.COMMAND_SET_LANGUAGE_SPANISH;
|
|
|
|
localeString = "es_ES";
|
|
|
|
break;
|
2018-07-16 14:36:52 +02:00
|
|
|
case "ru":
|
|
|
|
command_old = AmazfitBipService.COMMAND_SET_LANGUAGE_ENGLISH;
|
|
|
|
localeString = "ru_RU";
|
|
|
|
break;
|
2018-02-08 22:21:15 +01:00
|
|
|
default:
|
|
|
|
command_old = AmazfitBipService.COMMAND_SET_LANGUAGE_ENGLISH;
|
|
|
|
localeString = "en_US";
|
|
|
|
break;
|
2017-11-01 23:32:54 +01:00
|
|
|
}
|
2017-09-28 15:44:07 +02:00
|
|
|
}
|
2018-02-08 22:21:15 +01:00
|
|
|
command_new = AmazfitBipService.COMMAND_SET_LANGUAGE_NEW_TEMPLATE;
|
|
|
|
System.arraycopy(localeString.getBytes(), 0, command_new, 3, localeString.getBytes().length);
|
|
|
|
|
|
|
|
builder.add(new ConditionalWriteAction(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_3_CONFIGURATION)) {
|
|
|
|
@Override
|
|
|
|
protected byte[] checkCondition() {
|
2018-07-16 15:16:23 +02:00
|
|
|
if (gbDevice.getType() == DeviceType.MIBAND3 ||
|
|
|
|
(gbDevice.getType() == DeviceType.AMAZFITBIP && new Version(gbDevice.getFirmwareVersion()).compareTo(new Version("0.1.0.77")) >= 0) ||
|
|
|
|
(gbDevice.getType() == DeviceType.AMAZFITCOR && new Version(gbDevice.getFirmwareVersion()).compareTo(new Version("1.0.7.23")) >= 0)) {
|
2018-02-08 22:21:15 +01:00
|
|
|
return command_new;
|
|
|
|
} else {
|
|
|
|
return command_old;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-11-01 23:32:54 +01:00
|
|
|
|
2017-09-28 15:44:07 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-01-20 01:34:40 +01:00
|
|
|
|
2017-08-29 22:16:59 +02:00
|
|
|
@Override
|
|
|
|
public void phase2Initialize(TransactionBuilder builder) {
|
|
|
|
super.phase2Initialize(builder);
|
|
|
|
LOG.info("phase2Initialize...");
|
2017-09-28 15:44:07 +02:00
|
|
|
setLanguage(builder);
|
2017-08-29 22:16:59 +02:00
|
|
|
requestGPSVersion(builder);
|
|
|
|
}
|
2017-10-24 22:01:25 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public HuamiFWHelper createFWHelper(Uri uri, Context context) throws IOException {
|
|
|
|
return new AmazfitBipFWHelper(uri, context);
|
|
|
|
}
|
2017-08-29 22:16:59 +02:00
|
|
|
}
|