MacUSBNotificationsMBS events don't fire with code instantiation

The Add-Ons forum category seems gone?

Anyway, Xojo 2021r2.1, MacOS 13, MBS 241

I have a generic Object class with a property of type MacUSBNotificationsMBS. In its constructor I add handlers for its events:

but the handlers never execute.

Under Windows using WinUSBNotificationMBS it works fine.

The MBS example program, which subclasses MacUSBNotificationMBS to expose its events, also works fine, however I prefer not to use this method because it would degrade the encapsulation of my SerialConnector class.

Any idea why the events wouldn’t fire when AddHandler is used?

It’s called “Extras” now

1 Like

Did macOS ask for your app to be allowed access to USB drives? Just spitballing a permissions guess.

No. The MBS notifications classes just provide events when a device is connected or disconnected, they don’t attempt any access, and besides, the devices I’m working with are USB-serial converters, not drives. And the MBS example works fine.

Example project @Christian_Schmitz

MacUSBNotification.xojo_binary_project.zip (4.4 KB)

I’m gonna make a suggestion. Instead of using AddHandler with WeakAddressOf, try subclassing the MBS classes, implementing the events and then set up delegates. It gives you greater control, allows you to change the target on the fly, and even detect if you’ve forgotten to hook one of them up. The way I do this looks like this( I’ll use timer)

Class MyTimer as Timer
  Delegate ActionDelegate()
  Property ActionCallback as ActionDelegate
  EventHandler Action()
    If actionCallback <> Nil then
      ActionCallback.invoke
    End if
  End EventHandler
End Class

Then instead of AddHandler, you just assign the address to ActionCallback.

1 Like

That’s basically my workaround, and yes it works, but it’s ugly to have two extra classes is my project which wouldn’t be necessary if the plug-in worked; I’d prefer to have it all encapsulated in one class.

Sorry, but this class calls events in the constructor.

So it checks if you have implemented the events there and you addHandler comes too late.