Converting string to integer

edited April 2016 in Android Mode

Hi,

recently i stumbled into an odd problem. The fact is, that i'm not able to convert a number (string) (sent from an arduino like device) to android using Bluetooth. However the same procedure works great when send the same string to a PC using serial port.

So the code on my mbed side is just:

bt.printf("%i\r\n",Tobj1);

where Tobj1 is an integer number varying from 20000 to 100000 (it's a thermopile temp. sensor)

and on the android side I use:

String info2 = trim(info); Tobj = int(info2);

where info is the result from onBluetoothDataEvent

If i use text and try to put the value on screen I can see the results, so this is working fine, but if I want to put on the screen the Tobj variable, it's always 0. So the conversion Tobj = int(info2) does not work. I tried with several methods, using splitTokens, but as far I saw, the lenghth is verying so the app crashes immediatly.

Is there any other way to convert a string into an integer ?

Answers

  • Hi,

    none of above works. As I stated, when using serial port on my PC it works like it should. I'm able to draw the graph. If I try to send data to my Android phone using BT connection I can see the string (if i put the string on screen, I see the numbers), if i try to convert the same string to an INT using variable = int(string), I get a 0.

    I don't know, what I'm doing wrong.

  • I'm confused: Are you trying to receive or send values?
    Are the values being sent as String or as int?

  • On the Android side, I'm trying to convert an received string and convert it to an integer for further data manipulation. On the Arduino side I send data using printf("%i\r\n",myInteger). I have a BT transciever hooked on Arduino. So basicly, I'm sending a string made of numbers rapresenting myInteger variable. I'm able to convert this string on a PC, but fail on the Android device.

  • Sorry, no Android here. Regardless for printf("%i\r\n", myInteger);, I believe the 1st link should work:

    void serialEvent(final Serial s) {
      myString = s.readString().trim();
      redraw = true;
    }
    

    It only lacks int() in order to convert myString to int:

    int myInt;
    
    void serialEvent(final Serial s) {
      myInt = int( s.readString().trim() );
      redraw = true;
    }
    
  • Thank you anyway. As stated before, using serial port wotks Ok, something is going wrong using BlueTooth communication. I have to figure it out. I'll post the solution (if I find it) to this topic in any case some one else stubmble to this problem

  • Ok, I figured it out (kidna). So, the Ketay bluetooth library checks the OnBluetoothDataEvent method every 100ms. I was sending data to the BT module every 10ms, hence the data was accumulating in random way, so the integer extraction was impossible. After lowering the transmission rate to 200ms, it works fine. For this application is enough, but this is just a practice for another application where I would need more than 10 measurements per second, so is there any way to modify the Ketay library or is this a BT spcecific?

Sign In or Register to comment.