mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-28 12:56:49 +01:00
Huami: Use music control code from Bip S for all devices
needs testing and cleanup for all devices
This commit is contained in:
parent
55f9c8f519
commit
6b20f62e0f
@ -812,9 +812,10 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
|
|||||||
|
|
||||||
if (stateSpec != null && !stateSpec.equals(bufferMusicStateSpec)) {
|
if (stateSpec != null && !stateSpec.equals(bufferMusicStateSpec)) {
|
||||||
bufferMusicStateSpec = stateSpec;
|
bufferMusicStateSpec = stateSpec;
|
||||||
sendMusicStateToDevice();
|
if (isMusicAppStarted) {
|
||||||
|
sendMusicStateToDevice(null, bufferMusicStateSpec);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -826,53 +827,58 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
|
|||||||
|
|
||||||
if (musicSpec != null && !musicSpec.equals(bufferMusicSpec)) {
|
if (musicSpec != null && !musicSpec.equals(bufferMusicSpec)) {
|
||||||
bufferMusicSpec = musicSpec;
|
bufferMusicSpec = musicSpec;
|
||||||
|
if (bufferMusicStateSpec != null) {
|
||||||
|
bufferMusicStateSpec.state = 0;
|
||||||
|
bufferMusicStateSpec.position = 0;
|
||||||
|
}
|
||||||
if (isMusicAppStarted) {
|
if (isMusicAppStarted) {
|
||||||
sendMusicStateToDevice();
|
sendMusicStateToDevice(bufferMusicSpec, bufferMusicStateSpec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected void sendMusicStateToDevice() {
|
private void sendMusicStateToDevice() {
|
||||||
|
sendMusicStateToDevice(bufferMusicSpec, bufferMusicStateSpec);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendMusicStateToDevice(MusicSpec musicSpec, MusicStateSpec musicStateSpec) {
|
||||||
if (characteristicChunked == null) {
|
if (characteristicChunked == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (bufferMusicSpec == null || bufferMusicStateSpec == null) {
|
|
||||||
try {
|
if (musicStateSpec == null) {
|
||||||
TransactionBuilder builder = performInitialized("send dummy playback info to enable music controls");
|
|
||||||
writeToChunked(builder, 3, new byte[]{1, 0, 1, 0, 0, 0, 1, 0});
|
|
||||||
builder.queue(getQueue());
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOG.error("Unable to send dummy music controls");
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte flags = 0x00;
|
byte flags = 0x00;
|
||||||
flags |= 0x01;
|
flags |= 0x01;
|
||||||
int length = 8;
|
int length = 5;
|
||||||
if (bufferMusicSpec.track != null && bufferMusicSpec.track.getBytes().length > 0) {
|
if (musicSpec != null) {
|
||||||
length += bufferMusicSpec.track.getBytes().length + 1;
|
if (musicSpec.artist != null && musicSpec.artist.getBytes().length > 0) {
|
||||||
flags |= 0x02;
|
length += musicSpec.artist.getBytes().length + 1;
|
||||||
}
|
flags |= 0x02;
|
||||||
if (bufferMusicSpec.album != null && bufferMusicSpec.album.getBytes().length > 0) {
|
}
|
||||||
length += bufferMusicSpec.album.getBytes().length + 1;
|
if (musicSpec.album != null && musicSpec.album.getBytes().length > 0) {
|
||||||
flags |= 0x04;
|
length += musicSpec.album.getBytes().length + 1;
|
||||||
}
|
flags |= 0x04;
|
||||||
if (bufferMusicSpec.artist != null && bufferMusicSpec.artist.getBytes().length > 0) {
|
}
|
||||||
length += bufferMusicSpec.artist.getBytes().length + 1;
|
if (musicSpec.track != null && musicSpec.track.getBytes().length > 0) {
|
||||||
flags |= 0x08;
|
length += musicSpec.track.getBytes().length + 1;
|
||||||
|
flags |= 0x08;
|
||||||
|
}
|
||||||
|
if (musicSpec.duration != 0) {
|
||||||
|
length += 2;
|
||||||
|
flags |= 0x10;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// LOG.info("Music flags are: " + (flags & 0xff));
|
|
||||||
try {
|
try {
|
||||||
ByteBuffer buf = ByteBuffer.allocate(length);
|
ByteBuffer buf = ByteBuffer.allocate(length);
|
||||||
buf.order(ByteOrder.LITTLE_ENDIAN);
|
buf.order(ByteOrder.LITTLE_ENDIAN);
|
||||||
buf.put(flags);
|
buf.put(flags);
|
||||||
byte state;
|
byte state;
|
||||||
switch (bufferMusicStateSpec.state) {
|
switch (musicStateSpec.state) {
|
||||||
case MusicStateSpec.STATE_PLAYING:
|
case MusicStateSpec.STATE_PLAYING:
|
||||||
state = 1;
|
state = 1;
|
||||||
break;
|
break;
|
||||||
@ -881,24 +887,26 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
buf.put(state);
|
buf.put(state);
|
||||||
buf.put(new byte[]{0x1, 0x0, 0x0, 0x0}); //unknown
|
buf.put((byte) 0);
|
||||||
buf.put(new byte[]{0x1,0x0}); //show track
|
buf.putShort((short) musicStateSpec.position);
|
||||||
// buf.put(new byte[]{0x1,0x1}); //show album
|
|
||||||
|
|
||||||
|
if (musicSpec != null) {
|
||||||
if (bufferMusicSpec.track != null && bufferMusicSpec.track.getBytes().length > 0) {
|
if (musicSpec.artist != null && musicSpec.artist.getBytes().length > 0) {
|
||||||
buf.put(bufferMusicSpec.track.getBytes());
|
buf.put(musicSpec.artist.getBytes());
|
||||||
buf.put((byte) 0);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (bufferMusicSpec.album != null && bufferMusicSpec.album.getBytes().length > 0) {
|
|
||||||
buf.put(bufferMusicSpec.album.getBytes());
|
|
||||||
buf.put((byte) 0);
|
|
||||||
}
|
|
||||||
if (bufferMusicSpec.artist != null && bufferMusicSpec.artist.getBytes().length > 0) {
|
|
||||||
buf.put(bufferMusicSpec.artist.getBytes());
|
|
||||||
buf.put((byte) 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
TransactionBuilder builder = performInitialized("send playback info");
|
TransactionBuilder builder = performInitialized("send playback info");
|
||||||
writeToChunked(builder, 3, buf.array());
|
writeToChunked(builder, 3, buf.array());
|
||||||
@ -907,9 +915,7 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOG.error("Unable to send playback state");
|
LOG.error("Unable to send playback state");
|
||||||
}
|
}
|
||||||
|
LOG.info("sendMusicStateToDevice: " + musicSpec + " " + musicStateSpec);
|
||||||
// LOG.info("Sent music: " + bufferMusicSpec.toString() + " " + bufferMusicStateSpec.toString());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -211,104 +211,4 @@ public class AmazfitBipSSupport extends AmazfitBipSupport {
|
|||||||
return this;
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user