From a042fb4b09e2d6e26fbd8af48bc477ae3f756aef Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Fri, 29 Apr 2016 22:38:25 -0500 Subject: [PATCH] fixed iBeacon functionality --- Sources/BluetoothLinux/iBeacon.swift | 41 ++++++++++--------- Sources/UnitTests/iBeaconTests.swift | 11 +++-- Sources/iBeaconTest/iBeaconTest.swift | 2 +- .../BluetoothLinuxTests/iBeaconTestsHelper.h | 2 +- 4 files changed, 30 insertions(+), 26 deletions(-) diff --git a/Sources/BluetoothLinux/iBeacon.swift b/Sources/BluetoothLinux/iBeacon.swift index a0025c2..2df860f 100644 --- a/Sources/BluetoothLinux/iBeacon.swift +++ b/Sources/BluetoothLinux/iBeacon.swift @@ -67,33 +67,34 @@ internal func SetBeaconData(UUID: SwiftFoundation.UUID, major: UInt16, minor: UI // set UUID bytes - let littleUUIDBytes = isBigEndian ? UUID.toData().byteValue : UUID.toData().byteValue.reversed() + let littleUUIDBytes = isBigEndian ? UUID.toData().byteValue.reversed() : UUID.toData().byteValue parameter.data.9 = littleUUIDBytes[0] parameter.data.10 = littleUUIDBytes[1] - parameter.data.12 = littleUUIDBytes[2] - parameter.data.13 = littleUUIDBytes[3] - parameter.data.14 = littleUUIDBytes[4] - parameter.data.15 = littleUUIDBytes[5] - parameter.data.16 = littleUUIDBytes[6] - parameter.data.17 = littleUUIDBytes[7] - parameter.data.18 = littleUUIDBytes[8] - parameter.data.19 = littleUUIDBytes[9] - parameter.data.20 = littleUUIDBytes[10] - parameter.data.21 = littleUUIDBytes[11] - parameter.data.22 = littleUUIDBytes[12] - parameter.data.23 = littleUUIDBytes[13] - parameter.data.24 = littleUUIDBytes[14] + parameter.data.11 = littleUUIDBytes[2] + parameter.data.12 = littleUUIDBytes[3] + parameter.data.13 = littleUUIDBytes[4] + parameter.data.14 = littleUUIDBytes[5] + parameter.data.15 = littleUUIDBytes[6] + parameter.data.16 = littleUUIDBytes[7] + parameter.data.17 = littleUUIDBytes[8] + parameter.data.18 = littleUUIDBytes[9] + parameter.data.19 = littleUUIDBytes[10] + parameter.data.20 = littleUUIDBytes[11] + parameter.data.21 = littleUUIDBytes[12] + parameter.data.22 = littleUUIDBytes[13] + parameter.data.23 = littleUUIDBytes[14] + parameter.data.24 = littleUUIDBytes[15] - let majorBytes = major.littleEndian.bytes + let majorBytes = major.bytes - parameter.data.25 = majorBytes.0 - parameter.data.26 = majorBytes.1 + parameter.data.25 = majorBytes.1 + parameter.data.26 = majorBytes.0 - let minorBytes = minor.littleEndian.bytes + let minorBytes = minor.bytes - parameter.data.27 = minorBytes.0 - parameter.data.28 = minorBytes.1 + parameter.data.27 = minorBytes.1 + parameter.data.28 = minorBytes.0 parameter.data.29 = RSSI } diff --git a/Sources/UnitTests/iBeaconTests.swift b/Sources/UnitTests/iBeaconTests.swift index 45c4ce8..052950c 100644 --- a/Sources/UnitTests/iBeaconTests.swift +++ b/Sources/UnitTests/iBeaconTests.swift @@ -19,13 +19,13 @@ let identifier = UUID() - let major: UInt16 = 0 + let major: UInt16 = 1 let minor: UInt16 = 1 - let rssi: Int8 = -59 + let rssi: Int8 = -29 - var adverstisementDataParameter = beaconAdvertisementData(identifier.toData().byteValue, CInt(major), CInt(minor), CInt(rssi)) + var adverstisementDataParameter = beaconAdvertisementData(identifier.toData().byteValue, CInt(major).littleEndian, CInt(minor).littleEndian, CInt(rssi).littleEndian) var parameterBytes = [UInt8].init(repeating: 0, count: Int(adverstisementDataParameter.length)) @@ -37,7 +37,10 @@ XCTAssert(adverstisementDataParameter.length == advertisingDataCommand.length, "Invalid Length: \(adverstisementDataParameter.length) == \(advertisingDataCommand.length)") - //XCTAssert(memcmp(<#T##UnsafePointer!#>, <#T##UnsafePointer!#>, <#T##Int#>)) + let dataPointer1 = withUnsafePointer(&adverstisementDataParameter.data) { return UnsafePointer($0) } + let dataPointer2 = withUnsafePointer(&advertisingDataCommand.data) { return UnsafePointer($0) } + + XCTAssert(memcmp(dataPointer1, dataPointer2, Int(advertisingDataCommand.length)) == 0, "Invalid generated data: \n\(adverstisementDataParameter.data)\n == \n\(advertisingDataCommand.data))") } } diff --git a/Sources/iBeaconTest/iBeaconTest.swift b/Sources/iBeaconTest/iBeaconTest.swift index ca8ccff..77ddc14 100644 --- a/Sources/iBeaconTest/iBeaconTest.swift +++ b/Sources/iBeaconTest/iBeaconTest.swift @@ -23,7 +23,7 @@ func iBeaconTest(adapter: Adapter, timeout: Int) { print("Enabling iBeacon \(iBeaconUUID) for \(timeout) seconds") - do { try adapter.enableBeacon(UUID: iBeaconUUID, mayor: 1, minor: 1, RSSI: -59) } + do { try adapter.enableBeacon(UUID: iBeaconUUID, mayor: 1, minor: 1, RSSI: -29) } catch { Error("Error enabling iBeacon: \(error)") } diff --git a/Xcode/BluetoothLinuxTests/iBeaconTestsHelper.h b/Xcode/BluetoothLinuxTests/iBeaconTestsHelper.h index 53213a0..e5a7f59 100644 --- a/Xcode/BluetoothLinuxTests/iBeaconTestsHelper.h +++ b/Xcode/BluetoothLinuxTests/iBeaconTestsHelper.h @@ -81,7 +81,7 @@ static inline le_set_advertising_data_cp beaconAdvertisementData(const unsigned int i; for(i = 0; i < 16; i++) { - adv_data_cp.data[adv_data_cp.length + segment_length] = htobs((unsigned char)uuid[i]); segment_length++; + adv_data_cp.data[adv_data_cp.length + segment_length] = (unsigned char)uuid[i]; segment_length++; } // Major number