mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-07-12 00:14:04 +02:00
374 lines
13 KiB
Java
374 lines
13 KiB
Java
|
package nodomain.freeyourgadget.gadgetbridge.service.devices.asteroidos;
|
||
|
|
||
|
import android.bluetooth.BluetoothGatt;
|
||
|
import android.bluetooth.BluetoothGattCharacteristic;
|
||
|
import android.content.Intent;
|
||
|
import android.net.Uri;
|
||
|
import android.os.Build;
|
||
|
|
||
|
import org.slf4j.Logger;
|
||
|
import org.slf4j.LoggerFactory;
|
||
|
|
||
|
import java.io.ByteArrayOutputStream;
|
||
|
import java.nio.charset.StandardCharsets;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Date;
|
||
|
import java.util.GregorianCalendar;
|
||
|
import java.util.UUID;
|
||
|
|
||
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventBatteryInfo;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.devices.asteroidos.AsteroidOSConstants;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.devices.asteroidos.AsteroidOSMediaCommand;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.devices.asteroidos.AsteroidOSNotification;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.devices.asteroidos.AsteroidOSWeather;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.model.Alarm;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.GattService;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.IntentListener;
|
||
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.battery.BatteryInfoProfile;
|
||
|
|
||
|
public class AsteroidOSDeviceSupport extends AbstractBTLEDeviceSupport {
|
||
|
private static final Logger LOG = LoggerFactory.getLogger(AsteroidOSDeviceSupport.class);
|
||
|
private final BatteryInfoProfile<AsteroidOSDeviceSupport> batteryInfoProfile;
|
||
|
|
||
|
private final GBDeviceEventBatteryInfo batteryCmd = new GBDeviceEventBatteryInfo();
|
||
|
public AsteroidOSDeviceSupport() {
|
||
|
super(LOG);
|
||
|
addSupportedService(AsteroidOSConstants.SERVICE_UUID);
|
||
|
addSupportedService(AsteroidOSConstants.TIME_SERVICE_UUID);
|
||
|
addSupportedService(GattService.UUID_SERVICE_BATTERY_SERVICE);
|
||
|
addSupportedService(AsteroidOSConstants.WEATHER_SERVICE_UUID);
|
||
|
addSupportedService(AsteroidOSConstants.NOTIFICATION_SERVICE_UUID);
|
||
|
addSupportedService(AsteroidOSConstants.MEDIA_SERVICE_UUID);
|
||
|
|
||
|
IntentListener mListener = new IntentListener() {
|
||
|
@Override
|
||
|
public void notify(Intent intent) {
|
||
|
String action = intent.getAction();
|
||
|
if (BatteryInfoProfile.ACTION_BATTERY_INFO.equals(action)) {
|
||
|
handleBatteryInfo((nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.battery.BatteryInfo) intent.getParcelableExtra(BatteryInfoProfile.EXTRA_BATTERY_INFO));
|
||
|
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
batteryInfoProfile = new BatteryInfoProfile<>(this);
|
||
|
batteryInfoProfile.addListener(mListener);
|
||
|
addSupportedProfile(batteryInfoProfile);
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
private void handleBatteryInfo(nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.battery.BatteryInfo info) {
|
||
|
batteryCmd.level = info.getPercentCharged();
|
||
|
handleGBDeviceEvent(batteryCmd);
|
||
|
}
|
||
|
|
||
|
public boolean onCharacteristicChanged(BluetoothGatt gatt,
|
||
|
BluetoothGattCharacteristic characteristic) {
|
||
|
super.onCharacteristicChanged(gatt, characteristic);
|
||
|
|
||
|
UUID characteristicUUID = characteristic.getUuid();
|
||
|
byte[] value = characteristic.getValue();
|
||
|
|
||
|
if (characteristicUUID.equals(AsteroidOSConstants.MEDIA_COMMANDS_CHAR)) {
|
||
|
handleMediaCommand(gatt, characteristic);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
LOG.info("Characteristic changed UUID: " + characteristicUUID);
|
||
|
LOG.info("Characteristic changed value: " + characteristic.getValue());
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
|
||
|
@Override
|
||
|
protected TransactionBuilder initializeDevice(TransactionBuilder builder) {
|
||
|
builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext()));
|
||
|
getDevice().setFirmwareVersion("N/A");
|
||
|
getDevice().setFirmwareVersion2("N/A");
|
||
|
|
||
|
builder.notify(getCharacteristic(AsteroidOSConstants.MEDIA_COMMANDS_CHAR), true);
|
||
|
builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZED, getContext()));
|
||
|
|
||
|
batteryInfoProfile.requestBatteryInfo(builder);
|
||
|
batteryInfoProfile.enableNotify(builder, true);
|
||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||
|
builder.requestMtu(256);
|
||
|
}
|
||
|
return builder;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onNotification(NotificationSpec notificationSpec) {
|
||
|
AsteroidOSNotification notif = new AsteroidOSNotification(notificationSpec);
|
||
|
TransactionBuilder builder = new TransactionBuilder("send notification");
|
||
|
safeWriteToCharacteristic(builder, AsteroidOSConstants.NOTIFICATION_UPDATE_CHAR, notif.toString().getBytes(StandardCharsets.UTF_8));
|
||
|
builder.queue(getQueue());
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onDeleteNotification(int id) {
|
||
|
AsteroidOSNotification notif = new AsteroidOSNotification(id);
|
||
|
TransactionBuilder builder = new TransactionBuilder("delete notification");
|
||
|
safeWriteToCharacteristic(builder, AsteroidOSConstants.NOTIFICATION_UPDATE_CHAR, notif.toString().getBytes(StandardCharsets.UTF_8));
|
||
|
builder.queue(getQueue());
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onSetTime() {
|
||
|
GregorianCalendar now = BLETypeConversions.createCalendar();
|
||
|
Date nowTime = now.getTime();
|
||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||
|
baos.write((byte) nowTime.getYear());
|
||
|
baos.write((byte) nowTime.getMonth());
|
||
|
baos.write((byte) nowTime.getDay());
|
||
|
baos.write((byte) nowTime.getHours());
|
||
|
baos.write((byte) nowTime.getMinutes());
|
||
|
baos.write((byte) nowTime.getSeconds());
|
||
|
TransactionBuilder builder = new TransactionBuilder("set time");
|
||
|
safeWriteToCharacteristic(builder, AsteroidOSConstants.TIME_SET_CHAR, baos.toByteArray());
|
||
|
builder.queue(getQueue());
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
@Override
|
||
|
public void onSetCallState(CallSpec callSpec) {
|
||
|
AsteroidOSNotification call = new AsteroidOSNotification(callSpec);
|
||
|
TransactionBuilder builder = new TransactionBuilder("send call");
|
||
|
safeWriteToCharacteristic(builder, AsteroidOSConstants.NOTIFICATION_UPDATE_CHAR, call.toString().getBytes(StandardCharsets.UTF_8));
|
||
|
builder.queue(getQueue());
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onSetCannedMessages(CannedMessagesSpec cannedMessagesSpec) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onSetMusicState(MusicStateSpec stateSpec) {
|
||
|
TransactionBuilder builder = new TransactionBuilder("set music state");
|
||
|
if (stateSpec.state == MusicStateSpec.STATE_PLAYING) {
|
||
|
safeWriteToCharacteristic(builder, AsteroidOSConstants.MEDIA_PLAYING_CHAR, new byte[]{1});
|
||
|
} else {
|
||
|
safeWriteToCharacteristic(builder, AsteroidOSConstants.MEDIA_PLAYING_CHAR, new byte[]{0});
|
||
|
}
|
||
|
builder.queue(getQueue());
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onSetMusicInfo(MusicSpec musicSpec) {
|
||
|
// Send title
|
||
|
{
|
||
|
TransactionBuilder builder = new TransactionBuilder("set music title");
|
||
|
safeWriteToCharacteristic(builder, AsteroidOSConstants.MEDIA_TITLE_CHAR, musicSpec.track.getBytes(StandardCharsets.UTF_8));
|
||
|
builder.queue(getQueue());
|
||
|
}
|
||
|
// Send album
|
||
|
{
|
||
|
TransactionBuilder builder = new TransactionBuilder("set music album");
|
||
|
safeWriteToCharacteristic(builder, AsteroidOSConstants.MEDIA_ALBUM_CHAR, musicSpec.album.getBytes(StandardCharsets.UTF_8));
|
||
|
builder.queue(getQueue());
|
||
|
}
|
||
|
// Send artist
|
||
|
{
|
||
|
TransactionBuilder builder = new TransactionBuilder("set music artist");
|
||
|
safeWriteToCharacteristic(builder, AsteroidOSConstants.MEDIA_ARTIST_CHAR, musicSpec.artist.getBytes(StandardCharsets.UTF_8));
|
||
|
builder.queue(getQueue());
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onEnableRealtimeSteps(boolean enable) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onInstallApp(Uri uri) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onAppInfoReq() {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onAppStart(UUID uuid, boolean start) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onAppDelete(UUID uuid) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onAppConfiguration(UUID appUuid, String config, Integer id) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onAppReorder(UUID[] uuids) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onFetchRecordedData(int dataTypes) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onReset(int flags) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onHeartRateTest() {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onEnableRealtimeHeartRateMeasurement(boolean enable) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onFindDevice(boolean start) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onSetConstantVibration(int integer) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onScreenshotReq() {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onEnableHeartRateSleepSupport(boolean enable) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onSetHeartRateMeasurementInterval(int seconds) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onAddCalendarEvent(CalendarEventSpec calendarEventSpec) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onDeleteCalendarEvent(byte type, long id) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onSendConfiguration(String config) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onReadConfiguration(String config) {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onTestNewFunction() {
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onSendWeather(WeatherSpec weatherSpec) {
|
||
|
AsteroidOSWeather asteroidOSWeather = new AsteroidOSWeather(weatherSpec);
|
||
|
// Send weather city
|
||
|
{
|
||
|
TransactionBuilder builder = new TransactionBuilder("send weather city");
|
||
|
safeWriteToCharacteristic(builder, AsteroidOSConstants.WEATHER_CITY_CHAR, asteroidOSWeather.getCityName());
|
||
|
builder.queue(getQueue());
|
||
|
}
|
||
|
// Send weather conditions
|
||
|
{
|
||
|
TransactionBuilder builder = new TransactionBuilder("send weather conditions");
|
||
|
safeWriteToCharacteristic(builder, AsteroidOSConstants.WEATHER_IDS_CHAR, asteroidOSWeather.getWeatherConditions());
|
||
|
builder.queue(getQueue());
|
||
|
}
|
||
|
// Send min temps
|
||
|
{
|
||
|
TransactionBuilder builder = new TransactionBuilder("send weather min temps");
|
||
|
safeWriteToCharacteristic(builder, AsteroidOSConstants.WEATHER_MIN_TEMPS_CHAR, asteroidOSWeather.getMinTemps());
|
||
|
builder.queue(getQueue());
|
||
|
}
|
||
|
// Send max temps
|
||
|
{
|
||
|
TransactionBuilder builder = new TransactionBuilder("send weather max temps");
|
||
|
safeWriteToCharacteristic(builder, AsteroidOSConstants.WEATHER_MAX_TEMPS_CHAR, asteroidOSWeather.getMaxTemps());
|
||
|
builder.queue(getQueue());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean useAutoConnect() {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* This will check if the characteristic exists and can be written
|
||
|
* <p>
|
||
|
* Keeps backwards compatibility with firmware that can't take all the information
|
||
|
*/
|
||
|
private void safeWriteToCharacteristic(TransactionBuilder builder, UUID uuid, byte[] data) {
|
||
|
BluetoothGattCharacteristic characteristic = getCharacteristic(uuid);
|
||
|
if (characteristic != null &&
|
||
|
(characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
|
||
|
builder.write(characteristic, data);
|
||
|
} else {
|
||
|
LOG.warn("Tried to write to a characteristic that did not exist or was not writable!");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean onCharacteristicRead(BluetoothGatt gatt,
|
||
|
BluetoothGattCharacteristic characteristic, int status) {
|
||
|
if (super.onCharacteristicRead(gatt, characteristic, status)) {
|
||
|
return true;
|
||
|
}
|
||
|
UUID characteristicUUID = characteristic.getUuid();
|
||
|
|
||
|
LOG.info("Unhandled characteristic read: " + characteristicUUID);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
public void handleMediaCommand (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
|
||
|
LOG.info("handle media command");
|
||
|
AsteroidOSMediaCommand command = new AsteroidOSMediaCommand(characteristic.getValue()[0]);
|
||
|
GBDeviceEventMusicControl event = command.toMusicControlEvent();
|
||
|
evaluateGBDeviceEvent(event);
|
||
|
}
|
||
|
|
||
|
}
|