diff --git a/adapter_nrf51.go b/adapter_nrf51.go index 2b98cc1..9f38dcc 100644 --- a/adapter_nrf51.go +++ b/adapter_nrf51.go @@ -43,7 +43,9 @@ func handleEvent() { switch id { case C.BLE_GAP_EVT_CONNECTED: currentConnection.handle.Reg = uint16(gapEvent.conn_handle) - DefaultAdapter.connectHandler(Address{}, true) + connectEvent := gapEvent.params.unionfield_connected() + address := Address{makeMACAddress(connectEvent.peer_addr)} + DefaultAdapter.connectHandler(address, true) case C.BLE_GAP_EVT_DISCONNECTED: if defaultAdvertisement.isAdvertising.Get() != 0 { // The advertisement was running but was automatically stopped @@ -111,3 +113,11 @@ func (a *Adapter) Address() (MACAddress, error) { } return MACAddress{MAC: makeAddress(addr.addr)}, nil } + +// Convert a C.ble_gap_addr_t to a MACAddress struct. +func makeMACAddress(addr C.ble_gap_addr_t) MACAddress { + return MACAddress{ + MAC: makeAddress(addr.addr), + isRandom: addr.addr_type != 0, + } +} diff --git a/adapter_nrf528xx-full.go b/adapter_nrf528xx-full.go index f772863..bd4f4c1 100644 --- a/adapter_nrf528xx-full.go +++ b/adapter_nrf528xx-full.go @@ -25,20 +25,21 @@ func handleEvent() { switch id { case C.BLE_GAP_EVT_CONNECTED: connectEvent := gapEvent.params.unionfield_connected() + address := Address{makeMACAddress(connectEvent.peer_addr)} switch connectEvent.role { case C.BLE_GAP_ROLE_PERIPH: if debug { println("evt: connected in peripheral role") } currentConnection.handle.Reg = uint16(gapEvent.conn_handle) - DefaultAdapter.connectHandler(Address{}, true) + DefaultAdapter.connectHandler(address, true) case C.BLE_GAP_ROLE_CENTRAL: if debug { println("evt: connected in central role") } connectionAttempt.connectionHandle = gapEvent.conn_handle connectionAttempt.state.Set(2) // connection was successful - DefaultAdapter.connectHandler(Address{}, true) + DefaultAdapter.connectHandler(address, true) } case C.BLE_GAP_EVT_DISCONNECTED: if debug { @@ -81,8 +82,7 @@ func handleEvent() { scanReportBuffer.len = byte(advReport.data.len) globalScanResult.RSSI = int16(advReport.rssi) globalScanResult.Address = Address{ - MACAddress{MAC: makeAddress(advReport.peer_addr.addr), - isRandom: advReport.peer_addr.bitfield_addr_type() != 0}, + makeMACAddress(advReport.peer_addr), } globalScanResult.AdvertisementPayload = &scanReportBuffer // Signal to the main thread that there was a scan report. diff --git a/adapter_nrf528xx-peripheral.go b/adapter_nrf528xx-peripheral.go index 5e9f181..d26d392 100644 --- a/adapter_nrf528xx-peripheral.go +++ b/adapter_nrf528xx-peripheral.go @@ -28,7 +28,8 @@ func handleEvent() { println("evt: connected in peripheral role") } currentConnection.handle.Reg = uint16(gapEvent.conn_handle) - DefaultAdapter.connectHandler(Address{}, true) + connectEvent := gapEvent.params.unionfield_connected() + DefaultAdapter.connectHandler(Address{makeMACAddress(connectEvent.peer_addr)}, true) case C.BLE_GAP_EVT_DISCONNECTED: if debug { println("evt: disconnected") diff --git a/adapter_nrf528xx.go b/adapter_nrf528xx.go index d05ab02..557699d 100644 --- a/adapter_nrf528xx.go +++ b/adapter_nrf528xx.go @@ -59,3 +59,11 @@ func (a *Adapter) Address() (MACAddress, error) { } return MACAddress{MAC: makeAddress(addr.addr)}, nil } + +// Convert a C.ble_gap_addr_t to a MACAddress struct. +func makeMACAddress(addr C.ble_gap_addr_t) MACAddress { + return MACAddress{ + MAC: makeAddress(addr.addr), + isRandom: addr.bitfield_addr_type() != 0, + } +}