1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-27 20:36:51 +01:00

Amazfit Bip S: Music support

This commit is contained in:
Zhong Jianxin 2020-05-18 14:21:31 +08:00 committed by Gitea
parent c0558c570c
commit 44a9ce0267
3 changed files with 112 additions and 4 deletions

View File

@ -76,6 +76,11 @@ public class AmazfitBipSCoordinator extends HuamiCoordinator {
return true;
}
@Override
public boolean supportsMusicInfo() {
return true;
}
@Override
public int[] getSupportedDeviceSpecificSettings(GBDevice device) {
return new int[]{

View File

@ -188,9 +188,9 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
private RealtimeSamplesSupport realtimeSamplesSupport;
private boolean alarmClockRinging;
private boolean isMusicAppStarted = false;
private MusicSpec bufferMusicSpec = null;
private MusicStateSpec bufferMusicStateSpec = null;
protected boolean isMusicAppStarted = false;
protected MusicSpec bufferMusicSpec = null;
protected MusicStateSpec bufferMusicStateSpec = null;
private boolean heartRateNotifyEnabled;
private int mMTU = 23;
@ -829,7 +829,7 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
}
private void sendMusicStateToDevice() {
protected void sendMusicStateToDevice() {
if (characteristicChunked == null) {
return;
}

View File

@ -35,6 +35,8 @@ import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiFWHelper;
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec;
import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.util.NotificationUtils;
@ -200,4 +202,105 @@ public class AmazfitBipSSupport extends AmazfitBipSupport {
return this;
}
@Override
public void onSetMusicState(MusicStateSpec stateSpec) {
if (stateSpec != null && !stateSpec.equals(bufferMusicStateSpec)) {
bufferMusicStateSpec = stateSpec;
if (isMusicAppStarted) {
sendMusicStateToBipS(null, bufferMusicStateSpec);
}
}
}
@Override
public void onSetMusicInfo(MusicSpec musicSpec) {
if (musicSpec != null && !musicSpec.equals(bufferMusicSpec)) {
bufferMusicSpec = musicSpec;
if (bufferMusicStateSpec != null) {
bufferMusicStateSpec.state = 0;
bufferMusicStateSpec.position = 0;
}
if (isMusicAppStarted) {
sendMusicStateToBipS(bufferMusicSpec, bufferMusicStateSpec);
}
}
}
@Override
protected void sendMusicStateToDevice() {
sendMusicStateToBipS(bufferMusicSpec, bufferMusicStateSpec);
}
private void sendMusicStateToBipS(MusicSpec musicSpec, MusicStateSpec musicStateSpec) {
if (musicStateSpec == null) {
return;
}
byte flags = 0x00;
flags |= 0x01;
int length = 5;
if (musicSpec != null) {
if (musicSpec.artist != null && musicSpec.artist.getBytes().length > 0) {
length += musicSpec.artist.getBytes().length + 1;
flags |= 0x02;
}
if (musicSpec.album != null && musicSpec.album.getBytes().length > 0) {
length += musicSpec.album.getBytes().length + 1;
flags |= 0x04;
}
if (musicSpec.track != null && musicSpec.track.getBytes().length > 0) {
length += musicSpec.track.getBytes().length + 1;
flags |= 0x08;
}
if (musicSpec.duration != 0) {
length += 2;
flags |= 0x10;
}
}
try {
ByteBuffer buf = ByteBuffer.allocate(length);
buf.order(ByteOrder.LITTLE_ENDIAN);
buf.put(flags);
byte state;
switch (musicStateSpec.state) {
case MusicStateSpec.STATE_PLAYING:
state = 1;
break;
default:
state = 0;
}
buf.put(state);
buf.put((byte) 0);
buf.putShort((short)musicStateSpec.position);
if (musicSpec != null) {
if (musicSpec.artist != null && musicSpec.artist.getBytes().length > 0) {
buf.put(musicSpec.artist.getBytes());
buf.put((byte) 0);
}
if (musicSpec.album != null && musicSpec.album.getBytes().length > 0) {
buf.put(musicSpec.album.getBytes());
buf.put((byte) 0);
}
if (musicSpec.track != null && musicSpec.track.getBytes().length > 0) {
buf.put(musicSpec.track.getBytes());
buf.put((byte) 0);
}
if (musicSpec.duration != 0) {
buf.putShort((short) musicSpec.duration);
}
}
TransactionBuilder builder = performInitialized("send playback info");
writeToChunked(builder, 3, buf.array());
builder.queue(getQueue());
} catch (IOException e) {
LOG.error("Unable to send playback state");
}
//LOG.info("sendMusicStateToDevice: " + musicSpec + " " + musicStateSpec);
}
}