Skip to content

Commit

Permalink
#154 Improve BluetoothAddress Embedded Swift support
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 6, 2024
1 parent 91caf4c commit 825e336
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions Sources/Bluetooth/Address.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,29 @@ extension BluetoothAddress: ByteSwap {

// MARK: - RawRepresentable

#if !hasFeature(Embedded)
extension BluetoothAddress: RawRepresentable {

/// Initialize a Bluetooth Address from its big endian string representation (e.g. `00:1A:7D:DA:71:13`).
public init?(rawValue: String) {

// verify string length
guard rawValue.utf8.count == 17
let characters = rawValue.utf8
guard characters.count == 17,
let separator = ":".utf8.first
else { return nil }

var bytes: ByteValue = (0, 0, 0, 0, 0, 0)

let components = rawValue.split(whereSeparator: { $0 == ":" })
let components = characters.split(whereSeparator: { $0 == separator })

guard components.count == 6
else { return nil }

for (index, string) in components.enumerated() {
for (index, subsequence) in components.enumerated() {

guard string.utf8.count == 2,
let byte = UInt8(string, radix: 16)
guard subsequence.count == 2,
let string = String(subsequence),
let byte = UInt8(hexadecimal: string)
else { return nil }

withUnsafeMutablePointer(to: &bytes) {
Expand All @@ -121,19 +123,6 @@ extension BluetoothAddress: RawRepresentable {

/// Convert a Bluetooth Address to its big endian string representation (e.g. `00:1A:7D:DA:71:13`).
public var rawValue: String {
_description
}
}
#endif

// MARK: - CustomStringConvertible

extension BluetoothAddress: CustomStringConvertible {

public var description: String { _description }

/// Convert a Bluetooth Address to its big endian string representation (e.g. `00:1A:7D:DA:71:13`).
internal var _description: String {
let bytes = self.bigEndian.bytes
return bytes.0.toHexadecimal()
+ ":" + bytes.1.toHexadecimal()
Expand All @@ -144,6 +133,13 @@ extension BluetoothAddress: CustomStringConvertible {
}
}

// MARK: - CustomStringConvertible

extension BluetoothAddress: CustomStringConvertible {

public var description: String { rawValue }
}

// MARK: - Data

public extension BluetoothAddress {
Expand Down

0 comments on commit 825e336

Please sign in to comment.