From 90232536c0da35b926f67c18165179283877bed5 Mon Sep 17 00:00:00 2001 From: pwespi Date: Sat, 25 Jan 2025 11:05:12 +0100 Subject: [PATCH 1/3] fix(ios): replace deprecated getConfigValue --- ios/Plugin/Plugin.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/Plugin/Plugin.swift b/ios/Plugin/Plugin.swift index bea5527..0913fbf 100644 --- a/ios/Plugin/Plugin.swift +++ b/ios/Plugin/Plugin.swift @@ -496,7 +496,7 @@ public class BluetoothLe: CAPPlugin { } private func getDisplayStrings() -> [String: String] { - let configDisplayStrings = getConfigValue("displayStrings") as? [String: String] ?? [String: String]() + let configDisplayStrings = getConfig().getObject("displayStrings") as? [String: String] ?? [String: String]() var displayStrings = [String: String]() displayStrings["noDeviceFound"] = configDisplayStrings["noDeviceFound"] ?? "No device found" displayStrings["availableDevices"] = configDisplayStrings["availableDevices"] ?? "Available devices" From 59e4cc820b58e6c6051672bcddf599824fcbdf52 Mon Sep 17 00:00:00 2001 From: pwespi Date: Sat, 25 Jan 2025 10:56:41 +0100 Subject: [PATCH 2/3] fix: make format of internal hex strings consistent --- .../plugins/bluetoothle/Conversion.kt | 9 +++--- .../plugins/bluetoothle/ConversionKtTest.kt | 2 +- ios/Plugin/Conversion.swift | 17 ++++++++--- ios/PluginTests/ConversionTests.swift | 2 +- src/bleClient.spec.ts | 28 +++++++++---------- src/conversion.spec.ts | 8 +++--- src/conversion.ts | 2 +- 7 files changed, 39 insertions(+), 29 deletions(-) diff --git a/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/Conversion.kt b/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/Conversion.kt index 8b2d218..daf3b53 100644 --- a/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/Conversion.kt +++ b/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/Conversion.kt @@ -29,10 +29,11 @@ fun stringToBytes(value: String): ByteArray { if (value == "") { return ByteArray(0) } - val hexValues = value.split(" ") - val bytes = ByteArray(hexValues.size) - for (i in hexValues.indices) { - bytes[i] = hexToByte(hexValues[i]) + require(value.length % 2 == 0) { "Input string must have an even length, not ${value.length}" } + val bytes = ByteArray(value.length / 2) + for (i in bytes.indices) { + val hexPair = value.substring(i * 2, i * 2 + 2) + bytes[i] = hexToByte(hexPair) } return bytes } diff --git a/android/src/test/java/com/capacitorjs/community/plugins/bluetoothle/ConversionKtTest.kt b/android/src/test/java/com/capacitorjs/community/plugins/bluetoothle/ConversionKtTest.kt index 8496dab..daf8ca4 100644 --- a/android/src/test/java/com/capacitorjs/community/plugins/bluetoothle/ConversionKtTest.kt +++ b/android/src/test/java/com/capacitorjs/community/plugins/bluetoothle/ConversionKtTest.kt @@ -19,7 +19,7 @@ class ConversionKtTest : TestCase() { } fun testStringToBytes() { - val input = "A1 2E 38 D4 89 C3" + val input = "a12e38d489c3" val output = stringToBytes(input) val expected = byteArrayOfInts(0xA1, 0x2E, 0x38, 0xD4, 0x89, 0xC3) expected.forEachIndexed { index, byte -> diff --git a/ios/Plugin/Conversion.swift b/ios/Plugin/Conversion.swift index 9ebbc13..a2ae6a3 100644 --- a/ios/Plugin/Conversion.swift +++ b/ios/Plugin/Conversion.swift @@ -48,10 +48,19 @@ func dataToString(_ data: Data) -> String { } func stringToData(_ dataString: String) -> Data { - let hexValues = dataString.split(separator: " ") - var data = Data(capacity: hexValues.count) - for hex in hexValues { - data.append(UInt8(hex, radix: 16)!) + guard dataString.count % 2 == 0 else { + fatalError("Input string must have an even length, not \(dataString.count)") + } + var data = Data(capacity: dataString.count / 2) + for i in stride(from: 0, to: dataString.count, by: 2) { + let start = dataString.index(dataString.startIndex, offsetBy: i) + let end = dataString.index(start, offsetBy: 2) + let hexPair = dataString[start.. { expect(BluetoothLe.addListener).toHaveBeenCalledWith('onEnabledChanged', expect.any(Function)); expect(BluetoothLe.startEnabledNotifications).toHaveBeenCalledTimes(1); expect((BleClient as unknown as BleClientWithPrivate).eventListeners.get('onEnabledChanged')).toBe( - mockPluginListenerHandle, + mockPluginListenerHandle ); }); @@ -120,7 +120,7 @@ describe('BleClient', () => { await BleClient.startEnabledNotifications(mockCallback); expect((BleClient as unknown as BleClientWithPrivate).eventListeners.get('onEnabledChanged')).toBe( - mockPluginListenerHandle, + mockPluginListenerHandle ); await BleClient.stopEnabledNotifications(); expect(mockPluginListenerHandle.remove).toHaveBeenCalledTimes(1); @@ -210,7 +210,7 @@ describe('BleClient', () => { (BluetoothLe.addListener as jest.Mock).mockReturnValue(mockPluginListenerHandle); await BleClient.connect(mockDevice.deviceId, mockDisconnectCallback); expect((BleClient as unknown as BleClientWithPrivate).eventListeners.get('disconnected|123')).toBe( - mockPluginListenerHandle, + mockPluginListenerHandle ); expect(BluetoothLe.connect).toHaveBeenCalledTimes(1); }); @@ -238,14 +238,14 @@ describe('BleClient', () => { }); it('should run read', async () => { - (BluetoothLe.read as jest.Mock).mockReturnValue({ value: '00 05 c8 ' }); + (BluetoothLe.read as jest.Mock).mockReturnValue({ value: '0005c8' }); const result = await BleClient.read(mockDevice.deviceId, service, characteristic); - expect(result).toEqual(hexStringToDataView('00 05 c8')); + expect(result).toEqual(hexStringToDataView('0005c8')); expect(BluetoothLe.read).toHaveBeenCalledWith({ deviceId: mockDevice.deviceId, service, characteristic }); }); it('should run read with timeout', async () => { - (BluetoothLe.read as jest.Mock).mockReturnValue({ value: '00 05 c8 ' }); + (BluetoothLe.read as jest.Mock).mockReturnValue({ value: '0005c8' }); await BleClient.read(mockDevice.deviceId, service, characteristic, { timeout: 6000 }); expect(BluetoothLe.read).toHaveBeenCalledWith({ deviceId: mockDevice.deviceId, @@ -276,7 +276,7 @@ describe('BleClient', () => { deviceId: mockDevice.deviceId, service, characteristic, - value: '00 01', + value: '0001', }); }); @@ -289,7 +289,7 @@ describe('BleClient', () => { deviceId: mockDevice.deviceId, service, characteristic, - value: '03 04 05 06 07', + value: '0304050607', }); }); @@ -300,7 +300,7 @@ describe('BleClient', () => { deviceId: mockDevice.deviceId, service, characteristic, - value: '00 01', + value: '0001', timeout: 6000, }); }); @@ -325,7 +325,7 @@ describe('BleClient', () => { deviceId: mockDevice.deviceId, service, characteristic, - value: '00 01', + value: '0001', timeout: 6000, }); }); @@ -369,7 +369,7 @@ describe('BleClient', () => { }); it('should run readDescriptor with timeout', async () => { - (BluetoothLe.readDescriptor as jest.Mock).mockReturnValue({ value: '00 05 c8 ' }); + (BluetoothLe.readDescriptor as jest.Mock).mockReturnValue({ value: '0005c8' }); const result = await BleClient.readDescriptor(mockDevice.deviceId, service, characteristic, descriptor, { timeout: 6000, }); @@ -380,7 +380,7 @@ describe('BleClient', () => { descriptor, timeout: 6000, }); - expect(result).toEqual(hexStringToDataView('00 05 c8')); + expect(result).toEqual(hexStringToDataView('0005c8')); }); it('should run writeDescriptor with timeout', async () => { @@ -393,14 +393,14 @@ describe('BleClient', () => { numbersToDataView([0, 1]), { timeout: 6000, - }, + } ); expect(BluetoothLe.writeDescriptor).toHaveBeenCalledWith({ deviceId: mockDevice.deviceId, service, characteristic, descriptor, - value: '00 01', + value: '0001', timeout: 6000, }); }); diff --git a/src/conversion.spec.ts b/src/conversion.spec.ts index 1a38ab0..c913d6d 100644 --- a/src/conversion.spec.ts +++ b/src/conversion.spec.ts @@ -56,7 +56,7 @@ describe('textToDataView', () => { const result = textToDataView('Hello world'); expect(result.byteLength).toEqual(11); expect(result.byteOffset).toEqual(0); - expect(dataViewToHexString(result)).toEqual('48 65 6c 6c 6f 20 77 6f 72 6c 64'); + expect(dataViewToHexString(result)).toEqual('48656c6c6f20776f726c64'); }); it('should convert an empty text to a DataView', () => { @@ -69,7 +69,7 @@ describe('textToDataView', () => { describe('dataViewToText', () => { it('should convert a DataView to text', () => { - const value = hexStringToDataView('48 65 6c 6c 6f 20 77 6f 72 6c 64'); + const value = hexStringToDataView('48656c6c6f20776f726c64'); const result = dataViewToText(value); expect(result).toEqual('Hello world'); }); @@ -96,7 +96,7 @@ describe('numberToUUID', () => { describe('hexStringToDataView', () => { it('should convert a hex string to a DataView', () => { - const value = '00 05 c8'; + const value = '0005c8'; const result = hexStringToDataView(value); expect(result.byteLength).toEqual(3); expect(result.byteOffset).toEqual(0); @@ -135,7 +135,7 @@ describe('dataViewToHexString', () => { it('should convert a DataView to a hex string', () => { const value = new DataView(Uint8Array.from([0, 5, 200]).buffer); const result = dataViewToHexString(value); - expect(result).toEqual('00 05 c8'); + expect(result).toEqual('0005c8'); }); it('should convert an empty DataView to a hex string', () => { diff --git a/src/conversion.ts b/src/conversion.ts index 8698e27..93593ef 100644 --- a/src/conversion.ts +++ b/src/conversion.ts @@ -68,7 +68,7 @@ export function dataViewToHexString(value: DataView): string { } return s; }) - .join(' '); + .join(''); } export function webUUIDToString(uuid: string | number): string { From 9f983c0b8551f9dce1b47e3fc7db6e8b985f19d9 Mon Sep 17 00:00:00 2001 From: pwespi Date: Sat, 25 Jan 2025 11:31:04 +0100 Subject: [PATCH 3/3] chore: run fmt --- src/bleClient.spec.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/bleClient.spec.ts b/src/bleClient.spec.ts index f0b5da2..20d27d5 100644 --- a/src/bleClient.spec.ts +++ b/src/bleClient.spec.ts @@ -95,7 +95,7 @@ describe('BleClient', () => { expect(BluetoothLe.addListener).toHaveBeenCalledWith('onEnabledChanged', expect.any(Function)); expect(BluetoothLe.startEnabledNotifications).toHaveBeenCalledTimes(1); expect((BleClient as unknown as BleClientWithPrivate).eventListeners.get('onEnabledChanged')).toBe( - mockPluginListenerHandle + mockPluginListenerHandle, ); }); @@ -120,7 +120,7 @@ describe('BleClient', () => { await BleClient.startEnabledNotifications(mockCallback); expect((BleClient as unknown as BleClientWithPrivate).eventListeners.get('onEnabledChanged')).toBe( - mockPluginListenerHandle + mockPluginListenerHandle, ); await BleClient.stopEnabledNotifications(); expect(mockPluginListenerHandle.remove).toHaveBeenCalledTimes(1); @@ -210,7 +210,7 @@ describe('BleClient', () => { (BluetoothLe.addListener as jest.Mock).mockReturnValue(mockPluginListenerHandle); await BleClient.connect(mockDevice.deviceId, mockDisconnectCallback); expect((BleClient as unknown as BleClientWithPrivate).eventListeners.get('disconnected|123')).toBe( - mockPluginListenerHandle + mockPluginListenerHandle, ); expect(BluetoothLe.connect).toHaveBeenCalledTimes(1); }); @@ -393,7 +393,7 @@ describe('BleClient', () => { numbersToDataView([0, 1]), { timeout: 6000, - } + }, ); expect(BluetoothLe.writeDescriptor).toHaveBeenCalledWith({ deviceId: mockDevice.deviceId,