Add Miracast function to newer Android versions According to Wikipedia:

Miracast support was built into Android with version 4.2 ...
... Support was
dropped again with Android 6.0 Marshmallow in 2015, in favor of
Google's own Google Cast protocol.

Any answers associated with these older versions of Android do not apply.
Modern Android devices include a &quot;cast&quot; function, but it's Google proprietary and so cheap HDMI TV dongles might not support it, especially since it's probably a moving target, and Google wants you to buy their device.
There are apps that simply help you turn the casting function on and off, but all those do is save you from digging into the settings yourself (they don't add any casting functionality).
Is there a way to add Miracast function to a modern Android device?
If Zillinium's suggestion to make changes into the "system / build.prop" does not make it fully function, you might need to also change the "/system/etc/wfdconfig.xml" file also. The following steps require both require root access and custom recovery TWRP.

Pre N (Nougat) the long way. You could use the nano command to add and/or change values, but for simplicity and to stay in-bounds, just use a file explorer and text editor. 


Go into the file /system/etc/wfdconfig.xml
Change the code:

...
&lt;ContentProtection&gt;
    &lt;Valid&gt;1&lt;/Valid&gt;
 ...


Change 1 to 0 to make it look like this code:

...
&lt;ContentProtection&gt;
    &lt;Valid&gt;0&lt;/Valid&gt;
...

Go into the file /system/build.prop. Go down to this line of code and AFTER it:

persist.debug.wfd.enable=1


Add a new line of code by creating this line:

persist.debug.wfd.appmonitoring=1

Save and exit.
Restart phone.
connect phone to computer.
Open an elevated command prompt and throw these commands:


adb shell
su
setprop persist.debug.wfd.appmonitoring 1

Verify the setprop getprop persist.debug.wfd.appmonitoring

If 1 is returned, then you should be good to go. 

Side note: if this should put you into a bootloop, go into custom recovery TWRP. 
Open a command prompt and throw these commands:


adb shell
chmod 644 /system/build.prop


That should fix bootloop.


For Android N (Nougat) the short way:

From the custom recovery TWRP:


Open a elevated command prompt:

adb reboot bootloader

Next, mount the /vendor partition from within TWRP`
Then throw the command:

adb shell 'echo persist.debug.wfd.enable=1 &gt;&gt;/vendor/build.prop'

Reboot system from TWRP menu


You should be good to go.