How i getting a value out of the function?

Showing 1-4 of 4 messages
How i getting a value out of the function? A Power 12/18/12 6:41 AM

 Hello.I make an apk for listen bluetooth and i want to get value data from function ListenBt() to pass to another function upload().I make public final string data but no work!Any help?

   public class Activity2 extends Activity {

    public String strValue;
    public String strValue2;
   
    public int t;//Temperature
    public String urlt;
    public final String data="0";
   
    TextView txt;
      
    Handler handler = new Handler();
  
    TextView myLabel;
   
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);
 
        //txt=(TextView)findViewById(R.id.txt);
        myLabel = (TextView)findViewById(R.id.label);         
    
    }
   
    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
          String response = "";
          for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
              HttpResponse execute = client.execute(httpGet);
              InputStream content = execute.getEntity().getContent();

              BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
              String s = "";
              while ((s = buffer.readLine()) != null) {
                response += s;
              }

            } catch (Exception e) {
              e.printStackTrace();
            }
          }
          return response;
        }

        @Override
        protected void onPostExecute(String result) {
            TextView text3 = (TextView) findViewById(R.id.txt3);
            text3.setText(result);
            try {
                sendData(result);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           
        }
      }

    public void readWebpage(View view) {
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] { strValue });

      }
   
    protected void onStart() {
        super.onStart();
        try
        {
                     
            findBT();
            openBT();
                                       
        }
        catch (IOException ex) { }
        runnable.run();
 
  }

  private Runnable runnable = new Runnable()
  {

      public void run()
      {
            
              DownloadWebPageTask task = new DownloadWebPageTask();
              task.execute(new String[] { strValue });
              beginListenForData();
              upload();
       
        if(work){  handler.postDelayed(this, 30000);}//Refresh page time
      }
  };
 
  void ListenBt()
  {
      final Handler handler = new Handler();
      final byte delimiter = 10; //This is the ASCII code for a newline character
     
      stopWorker = false;
      readBufferPosition = 0;
      readBuffer = new byte[1024];
      workerThread = new Thread(new Runnable()
      {
          public void run()
          {               
             while(!Thread.currentThread().isInterrupted() && !stopWorker)
             {
                  try
                  {
                      int bytesAvailable = mmInputStream.available();                       
                      if(bytesAvailable > 0)
                      {
                          byte[] packetBytes = new byte[bytesAvailable];
                          mmInputStream.read(packetBytes);
                          for(int i=0;i<bytesAvailable;i++)
                          {
                              byte b = packetBytes[i];
                              if(b == delimiter)
                              {
                                  byte[] encodedBytes = new byte[readBufferPosition];
                                  System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                                  final String data = new String(encodedBytes, "US-ASCII");
                                 
                                  readBufferPosition = 0;
                                 
                                  handler.post(new Runnable()
                                  {
                                      public void run()
                                      {
                                          myLabel.setText(data);
                                         
                                      }
                                 });
                              }
                              else
                              {
                                  readBuffer[readBufferPosition++] = b;
                              }
                          }
                      }
                  }
                  catch (IOException ex)
                  {
                      stopWorker = true;
                  }
             }
         }
      });

      workerThread.start();
  }
 
 
   
    public void upload(){
   
        t = Integer.parseInt(data);
        SimpleHttpPut.main(urlt,t);//Upload data class   
      
    }   
   
}

Re: How i getting a value out of the function? bob 12/18/12 7:09 AM

Change this:


final String data = new String(encodedBytes, "US-ASCII");


to this:


data = new String(encodedBytes, "US-ASCII");





Re: How i getting a value out of the function? A Power 12/18/12 7:26 AM

I try it but no work...

Re: How i getting a value out of the function? bob 12/18/12 7:55 AM
How are you initializing mmInputStream?