Skip to content

Commit 084a8c5

Browse files
committed
Advertise ESP23 with Bluetooth address
1 parent 6251d20 commit 084a8c5

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

esp32-led-blink-sdk/main/Main.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,18 @@ func app_main() {
3434
let flags: GAPFlags = [.lowEnergyGeneralDiscoverableMode, .notSupportedBREDR]
3535
let advertisement = LowEnergyAdvertisingData(beacon: beacon, flags: flags)
3636
try bluetooth.gap.setAdvertisement(advertisement)
37-
print("Configured advertisement")
37+
38+
// read address
39+
let address = try bluetooth.hostController.address()
3840

3941
// set scan response
40-
let name = GAPShortLocalName(name: "ESP32-C6")
42+
let name = GAPShortLocalName(name: "ESP32-C6 " + address.description)
4143
let scanResponse: LowEnergyAdvertisingData = GAPDataEncoder.encode(name)
4244
try bluetooth.gap.setScanResponse(scanResponse)
43-
print("Configured scan response")
4445

4546
// start advertisement
4647
try bluetooth.gap.startAdvertising()
48+
print("Advertising as \(name)")
4749
}
4850
catch {
4951
print("Bluetooth error \(error.rawValue)")

esp32-led-blink-sdk/main/NimBLE.swift

+23-14
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,18 @@
1212
struct NimBLE: ~Copyable {
1313

1414
init() throws(ESPError) {
15-
//esp_nimble_hci_init()
16-
try nvs_flash_init().throwsESPError()
17-
print("nvs_flash_init()")
18-
try nimble_port_init().throwsESPError()
19-
print("nimble_port_init()")
20-
//var options = esp_bt_controller_config_t()
21-
//try esp_bt_controller_init(&options).throwsESPError()
22-
//print("esp_bt_controller_init()")
23-
//try esp_bt_controller_enable(ESP_BT_MODE_BLE).throwsESPError()
24-
// print("esp_bt_controller_enable()")
15+
try nvs_flash_init().throwsESPError()
16+
try nimble_port_init().throwsESPError()
2517
nimble_port_freertos_init(nimble_host_task)
2618
}
27-
19+
2820
deinit {
29-
//esp_nimble_hci_deinit()
21+
nimble_port_freertos_deinit()
3022
}
3123

3224
var gap = GAP()
25+
26+
var hostController = HostController()
3327
}
3428

3529
@_silgen_name("nvs_flash_init")
@@ -42,10 +36,10 @@ internal func nimble_port_init() -> Int32
4236
internal func nimble_port_run() -> Int32
4337

4438
@_silgen_name("nimble_port_freertos_init")
45-
internal func nimble_port_freertos_init(_ function: (UnsafeMutableRawPointer) -> ()) -> Int32
39+
internal func nimble_port_freertos_init(_ function: (UnsafeMutableRawPointer) -> ())
4640

4741
@_silgen_name("nimble_port_freertos_deinit")
48-
internal func nimble_port_freertos_deinit() -> Int32
42+
internal func nimble_port_freertos_deinit()
4943

5044
internal func nimble_host_task(_ parameters: UnsafeMutableRawPointer) {
5145
print("BLE Host Task Started")
@@ -103,4 +97,19 @@ public struct GAP: ~Copyable {
10397

10498
internal func _gap_callback(event: UnsafeMutablePointer<ble_gap_event>?, context: UnsafeMutableRawPointer?) -> Int32 {
10599
return 0
100+
}
101+
102+
public struct HostController: ~Copyable {
103+
104+
func address(
105+
type: LowEnergyAddressType = .public
106+
) throws(NimBLEError) -> BluetoothAddress {
107+
var address = BluetoothAddress.zero
108+
try withUnsafeMutablePointer(to: &address) {
109+
$0.withMemoryRebound(to: UInt8.self, capacity: 6) {
110+
ble_hs_id_copy_addr(type.rawValue, $0, nil)
111+
}
112+
}.throwsError()
113+
return address
114+
}
106115
}

0 commit comments

Comments
 (0)