Tuesday, December 18, 2012

[android-developers] Re: How i getting a value out of the function?

Change this:


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


to this:


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





On Tuesday, December 18, 2012 8:41:34 AM UTC-6, Antonis Kanaris wrote:

 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   
      
    }   
   
}

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment