'Android Studio Bluetooth InputStream - receiving float data from arduino

I am sending float data from AnalogInput - SerialBT.print(vArduino); via Bluetooth to my Android Studio app. I am getting data in bytes, struggling how to get them in float or at least convert them to float to make calculations. Can I use ObjectInputStream, how?

Java Code:

InputStream inputStream = null;
                try {
                    inputStream = btSocket.getInputStream();
                    inputStream.skip(inputStream.available());

                    
                    byte b = (byte) inputStream.read();
                    System.out.println((char) b);
                }

                } catch (IOException e) {
                    e.printStackTrace();
                }

                    

Solution - reading float value

DataInputStream inputStream = null;
                try {

                    DataInputStream input = new DataInputStream(btSocket.getInputStream());
                    float d = input.readFloat();
                  
                     System.out.println(d);
                   

                } catch (IOException e) {
                    e.printStackTrace();
                }

Problem: im expecting V ~0.00 - 1.50, Later up to 55.00 While ~0.00, it prints 6.336931E-10, ~1.05 - 2.53483E-9

When I changed values to double in Arduino and Android Studio, I cant read the value from

double d = inputStream.readDouble();

Error:

W/System.err: at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:606) W/System.err: at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:88) W/System.err: at java.io.DataInputStream.readFully(DataInputStream.java:198) W/System.err: at java.io.DataInputStream.readLong(DataInputStream.java:413) W/System.err: at java.io.DataInputStream.readDouble(DataInputStream.java:465) at com.example.myapplication.MainActivity$ExampleRunnable.run(MainActivity.java:153) W/System.err: at java.lang.Thread.run(Thread.java:929)

I tried to doing this "manually" - successfully, getting values from array and stickly setting them in sequence to String, and then to double value, but i don't know, is it a good way to do like this.

InputStream inputStream = null;
                try {
                    inputStream = btSocket.getInputStream();
                    inputStream.skip(inputStream.available());
                    char[] byteArray = new char[4];
                    String voltageString = "";

                    for (int i = 0; i < 4; i++) {

                        char b = (char) inputStream.read();
                        byteArray[i]=b;
                        voltageString += b;


                    }
                    System.out.println(Arrays.toString(byteArray));
                   
                    double voltage = Double.parseDouble(voltageString);
                    System.out.println(voltage);

                    } catch (IOException e) {
                    e.printStackTrace();
                    }

In this case, the output values are the same as i wanted, i need only 0.00-60.0, one decimal point is missing, if the value is between 10-100 because of for loop, but that's even better in my calculations.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source