The
AudioStreamer() liberary offers what you are looking for. it takes the Microphone recording and buffers the samples in a
Byte()-Array. Then you will need the
ByteConverter()-Library to convert them into
Short()-Arrays. The
ByteConverter.LittleEndian must set to true.
In combination with a
Timer you can fetch every 20msec what the AudioStreamer() buffered for you. The length (in samples) will differ, because the timer is not exact enough. So you have to calculate the average from the number of samples that the AudioStreamer delivered.
The AudioStreamer Object is placed in the STARTER-module. The ByteConverter and your code belongs to the MAIN module.
This is completely async. So you do not wait in a DoWhile-Loop as on arduino, but fetch the sample chunks every 20msec from the AudioStreamer. The converting and your calculation of each chunk afterwards will last roundabout <5msec only.
Perhaps you will have a look on my ringbuffer tutorial, where I also use the AudioStreamer (but for Playback). But the procedures how to access the AudioStreamer are nearly the same you will need.
https://www.b4x.com/android/forum/threads/audio-ringbuffer.122761/