1212struct 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
4236internal 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
5044internal func nimble_host_task( _ parameters: UnsafeMutableRawPointer ) {
5145 print ( " BLE Host Task Started " )
@@ -103,4 +97,19 @@ public struct GAP: ~Copyable {
10397
10498internal 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