Skip to content

Commit 0752004

Browse files
committed
Simplified byte juggling
1 parent 203cdc4 commit 0752004

File tree

5 files changed

+199
-23
lines changed

5 files changed

+199
-23
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package com.movisens.smartgattlib;
2+
3+
import java.nio.ByteBuffer;
4+
import java.nio.ByteOrder;
5+
6+
public class GattByteBuffer {
7+
8+
public static GattByteBuffer allocate(int i) {
9+
GattByteBuffer result = new GattByteBuffer();
10+
result.buffer = ByteBuffer.allocate(i);
11+
result.buffer.order(ByteOrder.LITTLE_ENDIAN);
12+
return result;
13+
}
14+
15+
public static GattByteBuffer wrap(byte[] byteArray) {
16+
GattByteBuffer result = new GattByteBuffer();
17+
result.buffer = ByteBuffer.wrap(byteArray);
18+
result.buffer.order(ByteOrder.LITTLE_ENDIAN);
19+
return result;
20+
}
21+
22+
private ByteBuffer buffer;
23+
24+
public byte[] array() {
25+
return buffer.array();
26+
}
27+
28+
public final int capacity() {
29+
return buffer.capacity();
30+
}
31+
32+
public void getInt8(byte[] value, int i, int j) {
33+
buffer.get(value, i, j);
34+
}
35+
36+
public void getUint8(short[] value, int offset, int length) {
37+
for (int i = 0; i < length; i++) {
38+
value[i + offset] = getUint8();
39+
}
40+
}
41+
42+
public Boolean getBoolean() {
43+
if (buffer.get() == (byte) 0) {
44+
return false;
45+
} else {
46+
return true;
47+
}
48+
}
49+
50+
public Float getFloat32() {
51+
return buffer.getFloat();
52+
}
53+
54+
public Short getInt16() {
55+
return buffer.getShort();
56+
}
57+
58+
public Integer getInt32() {
59+
return buffer.getInt();
60+
}
61+
62+
public Long getInt64() {
63+
return buffer.getLong();
64+
}
65+
66+
public Byte getInt8() {
67+
return buffer.get();
68+
}
69+
70+
public String getString() {
71+
String result = "";
72+
byte c;
73+
74+
while ((c = buffer.get()) != 0) {
75+
result += (char) c;
76+
}
77+
78+
return result;
79+
}
80+
81+
public Integer getUint16() {
82+
int result = buffer.getShort();
83+
if (result < 0) {
84+
result += ((int) 1) << 16;
85+
}
86+
return result;
87+
}
88+
89+
public Long getUint32() {
90+
long result = buffer.getInt();
91+
if (result < 0) {
92+
result += ((long) 1) << 32;
93+
}
94+
return result;
95+
}
96+
97+
public Short getUint8() {
98+
short result = buffer.get();
99+
if (result < 0) {
100+
result += ((short) 1) << 8;
101+
}
102+
return result;
103+
}
104+
105+
public GattByteBuffer position(int i) {
106+
buffer.position(i);
107+
return this;
108+
}
109+
110+
public GattByteBuffer putUint8(short[] value, int offset, int length) {
111+
for (int i = 0; i < length; i++) {
112+
putUint8(value[i + offset]);
113+
}
114+
return this;
115+
}
116+
117+
public GattByteBuffer putInt8(byte[] value, int i, int j) {
118+
buffer.put(value, i, j);
119+
return this;
120+
}
121+
122+
public GattByteBuffer putBoolean(boolean doEnable) {
123+
if (doEnable) {
124+
buffer.put((byte) 1);
125+
} else {
126+
buffer.put((byte) 0);
127+
}
128+
return this;
129+
}
130+
131+
public GattByteBuffer putFloat32(Float value) {
132+
buffer.putFloat(value);
133+
return this;
134+
}
135+
136+
public GattByteBuffer putInt16(short value) {
137+
buffer.putShort(value);
138+
return this;
139+
}
140+
141+
public GattByteBuffer putInt32(int value) {
142+
buffer.putInt(value);
143+
return this;
144+
}
145+
146+
public GattByteBuffer putInt64(long value) {
147+
buffer.putLong(value);
148+
return this;
149+
}
150+
151+
public GattByteBuffer putInt8(byte value) {
152+
buffer.put(value);
153+
return this;
154+
}
155+
156+
public GattByteBuffer putString(String value) {
157+
buffer.put(value.getBytes());
158+
buffer.put((byte) 0);
159+
return this;
160+
}
161+
162+
public GattByteBuffer putUint16(int value) {
163+
buffer.putShort((short) value);
164+
return this;
165+
}
166+
167+
public GattByteBuffer putUint32(long value) {
168+
buffer.putInt((int) value);
169+
return this;
170+
}
171+
172+
public GattByteBuffer putUint8(short value) {
173+
buffer.put((byte) value);
174+
return this;
175+
}
176+
177+
public GattByteBuffer rewind() {
178+
buffer.rewind();
179+
return this;
180+
}
181+
182+
public boolean hasRemaining() {
183+
return buffer.hasRemaining();
184+
}
185+
}

src/com/movisens/smartgattlib/characteristics/BatteryLevel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.movisens.smartgattlib.characteristics;
22

3-
import com.movisens.smartgattlib.GattUtils;
3+
import com.movisens.smartgattlib.GattByteBuffer;
44

55
public class BatteryLevel {
66
int level = -1;
77

88
public BatteryLevel(byte[] value) {
9-
level = GattUtils.getIntValue(value, GattUtils.FORMAT_UINT8, 0);
9+
level = GattByteBuffer.wrap(value).getUint8();
1010
}
1111

1212
/**

src/com/movisens/smartgattlib/characteristics/BodySensorLocation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.movisens.smartgattlib.characteristics;
22

3-
import com.movisens.smartgattlib.GattUtils;
3+
import com.movisens.smartgattlib.GattByteBuffer;
44

55
public class BodySensorLocation {
66
Location location = Location.Other;
@@ -10,7 +10,7 @@ public enum Location {
1010
}
1111

1212
public BodySensorLocation(byte[] value) {
13-
int loc = GattUtils.getIntValue(value, GattUtils.FORMAT_UINT8, 0);
13+
int loc = GattByteBuffer.wrap(value).getUint8();
1414

1515
switch (loc) {
1616
case 0:

src/com/movisens/smartgattlib/characteristics/HeartRateMeasurement.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import java.util.ArrayList;
44

5+
import com.movisens.smartgattlib.GattByteBuffer;
56
import com.movisens.smartgattlib.GattUtils;
67

7-
8-
98
public class HeartRateMeasurement {
109

1110
ArrayList<Float> rrIntervals = new ArrayList<Float>();
@@ -18,17 +17,12 @@ public enum SensorWorn {
1817
}
1918

2019
public HeartRateMeasurement(byte[] value) {
21-
int length = value.length;
22-
int offset = 1;
23-
byte flags = value[0];
20+
GattByteBuffer bb = GattByteBuffer.wrap(value);
21+
byte flags = bb.getInt8();
2422
if (isHeartRateInUINT16(flags)) {
25-
hrmval = GattUtils.getIntValue(value, GattUtils.FORMAT_UINT16,
26-
offset);
27-
offset += 2;
23+
hrmval = bb.getUint16();
2824
} else {
29-
hrmval = GattUtils.getIntValue(value, GattUtils.FORMAT_UINT8,
30-
offset);
31-
offset += 1;
25+
hrmval = bb.getUint8();
3226
}
3327
if (isWornStatusPresent(flags)) {
3428
if (isSensorWorn(flags)) {
@@ -38,14 +32,11 @@ public HeartRateMeasurement(byte[] value) {
3832
}
3933
}
4034
if (isEePresent(flags)) {
41-
eeval = GattUtils.getIntValue(value, GattUtils.FORMAT_UINT16,
42-
offset);
43-
offset += 2;
35+
eeval = bb.getUint16();
4436
}
4537
if (isRrIntPresent(flags)) {
46-
for (int i = offset; i < length; i += 2) {
47-
rrIntervals.add(GattUtils.getIntValue(value,
48-
GattUtils.FORMAT_UINT16, i) / 1024F);
38+
while (bb.hasRemaining()) {
39+
rrIntervals.add(bb.getUint16() / 1024F);
4940
}
5041
}
5142
}

src/com/movisens/smartgattlib/characteristics/ManufacturerNameString.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.movisens.smartgattlib.characteristics;
22

3-
import com.movisens.smartgattlib.GattUtils;
3+
import com.movisens.smartgattlib.GattByteBuffer;
44

55
public class ManufacturerNameString {
66
String content = "";
77

88
public ManufacturerNameString(byte[] value) {
9-
content = GattUtils.getStringValue(value, 0);
9+
content = GattByteBuffer.wrap(value).getString();
1010
}
1111

1212
public String getManufacturerNameString() {

0 commit comments

Comments
 (0)