Skip to content

Commit 3ec8a98

Browse files
committed
fix: make format of internal hex strings consistent
1 parent f9775a9 commit 3ec8a98

File tree

7 files changed

+39
-29
lines changed

7 files changed

+39
-29
lines changed

android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/Conversion.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ fun stringToBytes(value: String): ByteArray {
2929
if (value == "") {
3030
return ByteArray(0)
3131
}
32-
val hexValues = value.split(" ")
33-
val bytes = ByteArray(hexValues.size)
34-
for (i in hexValues.indices) {
35-
bytes[i] = hexToByte(hexValues[i])
32+
require(value.length % 2 == 0) { "Input string must have an even length, not ${value.length}" }
33+
val bytes = ByteArray(value.length / 2)
34+
for (i in bytes.indices) {
35+
val hexPair = value.substring(i * 2, i * 2 + 2)
36+
bytes[i] = hexToByte(hexPair)
3637
}
3738
return bytes
3839
}

android/src/test/java/com/capacitorjs/community/plugins/bluetoothle/ConversionKtTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ConversionKtTest : TestCase() {
1919
}
2020

2121
fun testStringToBytes() {
22-
val input = "A1 2E 38 D4 89 C3"
22+
val input = "a12e38d489c3"
2323
val output = stringToBytes(input)
2424
val expected = byteArrayOfInts(0xA1, 0x2E, 0x38, 0xD4, 0x89, 0xC3)
2525
expected.forEachIndexed { index, byte ->

ios/Plugin/Conversion.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,19 @@ func dataToString(_ data: Data) -> String {
4848
}
4949

5050
func stringToData(_ dataString: String) -> Data {
51-
let hexValues = dataString.split(separator: " ")
52-
var data = Data(capacity: hexValues.count)
53-
for hex in hexValues {
54-
data.append(UInt8(hex, radix: 16)!)
51+
guard dataString.count % 2 == 0 else {
52+
fatalError("Input string must have an even length, not \(dataString.count)")
53+
}
54+
var data = Data(capacity: dataString.count / 2)
55+
for i in stride(from: 0, to: dataString.count, by: 2) {
56+
let start = dataString.index(dataString.startIndex, offsetBy: i)
57+
let end = dataString.index(start, offsetBy: 2)
58+
let hexPair = dataString[start..<end]
59+
if let byte = UInt8(hexPair, radix: 16) {
60+
data.append(byte)
61+
} else {
62+
fatalError("Invalid hexadecimal value: \(hexPair)")
63+
}
5564
}
5665
return data
5766
}

ios/PluginTests/ConversionTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ConversionTests: XCTestCase {
1212
}
1313

1414
func testStringToData() throws {
15-
let input = "a1 2e 38 d4 89 c3"
15+
let input = "a12e38d489c3"
1616
let output = stringToData(input)
1717
let expected = Data([0xA1, 0x2E, 0x38, 0xD4, 0x89, 0xC3])
1818
for (index, byte) in output.enumerated() {

src/bleClient.spec.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe('BleClient', () => {
9595
expect(BluetoothLe.addListener).toHaveBeenCalledWith('onEnabledChanged', expect.any(Function));
9696
expect(BluetoothLe.startEnabledNotifications).toHaveBeenCalledTimes(1);
9797
expect((BleClient as unknown as BleClientWithPrivate).eventListeners.get('onEnabledChanged')).toBe(
98-
mockPluginListenerHandle,
98+
mockPluginListenerHandle
9999
);
100100
});
101101

@@ -120,7 +120,7 @@ describe('BleClient', () => {
120120

121121
await BleClient.startEnabledNotifications(mockCallback);
122122
expect((BleClient as unknown as BleClientWithPrivate).eventListeners.get('onEnabledChanged')).toBe(
123-
mockPluginListenerHandle,
123+
mockPluginListenerHandle
124124
);
125125
await BleClient.stopEnabledNotifications();
126126
expect(mockPluginListenerHandle.remove).toHaveBeenCalledTimes(1);
@@ -210,7 +210,7 @@ describe('BleClient', () => {
210210
(BluetoothLe.addListener as jest.Mock).mockReturnValue(mockPluginListenerHandle);
211211
await BleClient.connect(mockDevice.deviceId, mockDisconnectCallback);
212212
expect((BleClient as unknown as BleClientWithPrivate).eventListeners.get('disconnected|123')).toBe(
213-
mockPluginListenerHandle,
213+
mockPluginListenerHandle
214214
);
215215
expect(BluetoothLe.connect).toHaveBeenCalledTimes(1);
216216
});
@@ -238,14 +238,14 @@ describe('BleClient', () => {
238238
});
239239

240240
it('should run read', async () => {
241-
(BluetoothLe.read as jest.Mock).mockReturnValue({ value: '00 05 c8 ' });
241+
(BluetoothLe.read as jest.Mock).mockReturnValue({ value: '0005c8' });
242242
const result = await BleClient.read(mockDevice.deviceId, service, characteristic);
243-
expect(result).toEqual(hexStringToDataView('00 05 c8'));
243+
expect(result).toEqual(hexStringToDataView('0005c8'));
244244
expect(BluetoothLe.read).toHaveBeenCalledWith({ deviceId: mockDevice.deviceId, service, characteristic });
245245
});
246246

247247
it('should run read with timeout', async () => {
248-
(BluetoothLe.read as jest.Mock).mockReturnValue({ value: '00 05 c8 ' });
248+
(BluetoothLe.read as jest.Mock).mockReturnValue({ value: '0005c8' });
249249
await BleClient.read(mockDevice.deviceId, service, characteristic, { timeout: 6000 });
250250
expect(BluetoothLe.read).toHaveBeenCalledWith({
251251
deviceId: mockDevice.deviceId,
@@ -276,7 +276,7 @@ describe('BleClient', () => {
276276
deviceId: mockDevice.deviceId,
277277
service,
278278
characteristic,
279-
value: '00 01',
279+
value: '0001',
280280
});
281281
});
282282

@@ -289,7 +289,7 @@ describe('BleClient', () => {
289289
deviceId: mockDevice.deviceId,
290290
service,
291291
characteristic,
292-
value: '03 04 05 06 07',
292+
value: '0304050607',
293293
});
294294
});
295295

@@ -300,7 +300,7 @@ describe('BleClient', () => {
300300
deviceId: mockDevice.deviceId,
301301
service,
302302
characteristic,
303-
value: '00 01',
303+
value: '0001',
304304
timeout: 6000,
305305
});
306306
});
@@ -325,7 +325,7 @@ describe('BleClient', () => {
325325
deviceId: mockDevice.deviceId,
326326
service,
327327
characteristic,
328-
value: '00 01',
328+
value: '0001',
329329
timeout: 6000,
330330
});
331331
});
@@ -369,7 +369,7 @@ describe('BleClient', () => {
369369
});
370370

371371
it('should run readDescriptor with timeout', async () => {
372-
(BluetoothLe.readDescriptor as jest.Mock).mockReturnValue({ value: '00 05 c8 ' });
372+
(BluetoothLe.readDescriptor as jest.Mock).mockReturnValue({ value: '0005c8' });
373373
const result = await BleClient.readDescriptor(mockDevice.deviceId, service, characteristic, descriptor, {
374374
timeout: 6000,
375375
});
@@ -380,7 +380,7 @@ describe('BleClient', () => {
380380
descriptor,
381381
timeout: 6000,
382382
});
383-
expect(result).toEqual(hexStringToDataView('00 05 c8'));
383+
expect(result).toEqual(hexStringToDataView('0005c8'));
384384
});
385385

386386
it('should run writeDescriptor with timeout', async () => {
@@ -393,14 +393,14 @@ describe('BleClient', () => {
393393
numbersToDataView([0, 1]),
394394
{
395395
timeout: 6000,
396-
},
396+
}
397397
);
398398
expect(BluetoothLe.writeDescriptor).toHaveBeenCalledWith({
399399
deviceId: mockDevice.deviceId,
400400
service,
401401
characteristic,
402402
descriptor,
403-
value: '00 01',
403+
value: '0001',
404404
timeout: 6000,
405405
});
406406
});

src/conversion.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('textToDataView', () => {
5656
const result = textToDataView('Hello world');
5757
expect(result.byteLength).toEqual(11);
5858
expect(result.byteOffset).toEqual(0);
59-
expect(dataViewToHexString(result)).toEqual('48 65 6c 6c 6f 20 77 6f 72 6c 64');
59+
expect(dataViewToHexString(result)).toEqual('48656c6c6f20776f726c64');
6060
});
6161

6262
it('should convert an empty text to a DataView', () => {
@@ -69,7 +69,7 @@ describe('textToDataView', () => {
6969

7070
describe('dataViewToText', () => {
7171
it('should convert a DataView to text', () => {
72-
const value = hexStringToDataView('48 65 6c 6c 6f 20 77 6f 72 6c 64');
72+
const value = hexStringToDataView('48656c6c6f20776f726c64');
7373
const result = dataViewToText(value);
7474
expect(result).toEqual('Hello world');
7575
});
@@ -96,7 +96,7 @@ describe('numberToUUID', () => {
9696

9797
describe('hexStringToDataView', () => {
9898
it('should convert a hex string to a DataView', () => {
99-
const value = '00 05 c8';
99+
const value = '0005c8';
100100
const result = hexStringToDataView(value);
101101
expect(result.byteLength).toEqual(3);
102102
expect(result.byteOffset).toEqual(0);
@@ -135,7 +135,7 @@ describe('dataViewToHexString', () => {
135135
it('should convert a DataView to a hex string', () => {
136136
const value = new DataView(Uint8Array.from([0, 5, 200]).buffer);
137137
const result = dataViewToHexString(value);
138-
expect(result).toEqual('00 05 c8');
138+
expect(result).toEqual('0005c8');
139139
});
140140

141141
it('should convert an empty DataView to a hex string', () => {

src/conversion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export function dataViewToHexString(value: DataView): string {
6868
}
6969
return s;
7070
})
71-
.join(' ');
71+
.join('');
7272
}
7373

7474
export function webUUIDToString(uuid: string | number): string {

0 commit comments

Comments
 (0)