In some cases you may want to use the Network class instead of Cellular or WiFi. The Network class is the base class of specific types of networking, and some common methods like connect() and ready() exist in the base Network class. This is handy if you have a library that works for both Cellular and Wi-Fi, and you want the caller of the library to specify which to use. It also can greatly reduce the number of #ifdef you have in your code.
Hey Rick,
I would like to learn how to do that.
Are there any examples somewhere that I can look at or can you point me in the right direction?
Thank you!
The methods available in the Network class are here:
You’ll find most of the common ones like on, off, connect, disconnect, ready, etc. are all implemented in the Network class which will do the right thing for Cellular and WiFi devices.
Though you do need to be a little careful if you have Ethernet because it’s ambiguous to which you really want.
The other thing that’s good for libraries is passing in a NetworkClass & as a parameter during library setup. The caller can pass Cellular or WiFi or Ethernet as the parameter and you can call methods like on, off, connect, etc. for the interface type that they’ve selected. Cellular is a singleton instance of CellularClass and that is a subclass of NetworkClass so it works really well.
Documented the Network class, which can be used to write code that is able to do things like connect() and ready() independent of the type of networking so you can write code that works on both cellular and Wi-Fi without using #ifdef.