@@ -133,7 +133,7 @@ type Address struct {
133133}
134134
135135// Connect starts a connection attempt to the given peripheral device address.
136- func (a * Adapter ) Connect (address Address , params ConnectionParams ) (* Device , error ) {
136+ func (a * Adapter ) Connect (address Address , params ConnectionParams ) (Device , error ) {
137137 if _debug {
138138 println ("Connect" )
139139 }
@@ -145,14 +145,14 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (*Device, er
145145 if err := a .hci .leCreateConn (0x0060 , 0x0030 , 0x00 ,
146146 random , makeNINAAddress (address .MAC ),
147147 0x00 , 0x0006 , 0x000c , 0x0000 , 0x00c8 , 0x0004 , 0x0006 ); err != nil {
148- return nil , err
148+ return Device {} , err
149149 }
150150
151151 // are we connected?
152152 start := time .Now ().UnixNano ()
153153 for {
154154 if err := a .hci .poll (); err != nil {
155- return nil , err
155+ return Device {} , err
156156 }
157157
158158 if a .hci .connectData .connected {
@@ -163,15 +163,18 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (*Device, er
163163 random = true
164164 }
165165
166- d := & Device {adapter : a ,
167- handle : a .hci .connectData .handle ,
166+ d := Device {
168167 Address : Address {
169168 MACAddress {
170169 MAC : makeAddress (a .hci .connectData .peerBdaddr ),
171170 isRandom : random },
172171 },
173- mtu : defaultMTU ,
174- notificationRegistrations : make ([]notificationRegistration , 0 ),
172+ deviceInternal : & deviceInternal {
173+ adapter : a ,
174+ handle : a .hci .connectData .handle ,
175+ mtu : defaultMTU ,
176+ notificationRegistrations : make ([]notificationRegistration , 0 ),
177+ },
175178 }
176179 a .connectedDevices = append (a .connectedDevices , d )
177180
@@ -189,10 +192,10 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (*Device, er
189192
190193 // cancel connection attempt that failed
191194 if err := a .hci .leCancelConn (); err != nil {
192- return nil , err
195+ return Device {} , err
193196 }
194197
195- return nil , ErrConnect
198+ return Device {} , ErrConnect
196199}
197200
198201type notificationRegistration struct {
@@ -202,28 +205,32 @@ type notificationRegistration struct {
202205
203206// Device is a connection to a remote peripheral.
204207type Device struct {
205- adapter * Adapter
206208 Address Address
209+ * deviceInternal
210+ }
211+
212+ type deviceInternal struct {
213+ adapter * Adapter
207214 handle uint16
208215 mtu uint16
209216
210217 notificationRegistrations []notificationRegistration
211218}
212219
213220// Disconnect from the BLE device.
214- func (d * Device ) Disconnect () error {
221+ func (d Device ) Disconnect () error {
215222 if _debug {
216223 println ("Disconnect" )
217224 }
218225 if err := d .adapter .hci .disconnect (d .handle ); err != nil {
219226 return err
220227 }
221228
222- d .adapter .connectedDevices = []* Device {}
229+ d .adapter .connectedDevices = []Device {}
223230 return nil
224231}
225232
226- func (d * Device ) findNotificationRegistration (handle uint16 ) * notificationRegistration {
233+ func (d Device ) findNotificationRegistration (handle uint16 ) * notificationRegistration {
227234 for _ , n := range d .notificationRegistrations {
228235 if n .handle == handle {
229236 return & n
@@ -233,14 +240,14 @@ func (d *Device) findNotificationRegistration(handle uint16) *notificationRegist
233240 return nil
234241}
235242
236- func (d * Device ) addNotificationRegistration (handle uint16 , callback func ([]byte )) {
243+ func (d Device ) addNotificationRegistration (handle uint16 , callback func ([]byte )) {
237244 d .notificationRegistrations = append (d .notificationRegistrations ,
238245 notificationRegistration {
239246 handle : handle ,
240247 callback : callback ,
241248 })
242249}
243250
244- func (d * Device ) startNotifications () {
251+ func (d Device ) startNotifications () {
245252 d .adapter .startNotifications ()
246253}
0 commit comments