0

How can I get final string 'data' into this function beginListenForData() for use to another function upload()? BeginListen get data from bluetooth...

  void beginListenForData()
  {
  final Handler handler = new Handler(); 

  workerThread = new Thread(new Runnable()
  {
      public void run()
      {              
              try {        
                        final String data = new String(encodedBytes, "US-ASCII");
                        readBufferPosition = 0;
                        //data2=data;
                              handler.post(new Runnable()
                              {
                                  public void run()
                                  {
                                      myLabel.setText(data);    
Anon
  • 2,608
  • 6
  • 26
  • 38
user1909897
  • 61
  • 4
  • 10

2 Answers2

0

Make data as field instead of local variable.

 String data;

 void beginListenForData()
  {
  final Handler handler = new Handler(); 

  workerThread = new Thread(new Runnable()
  {
      public void run()
      {              
              try {        
                        data = new String(encodedBytes, "US-ASCII");
                        readBufferPosition = 0;
                        //data2=data;
                              handler.post(new Runnable()
                              {
                                  public void run()
                                  {
                                      myLabel.setText(data);    
Uriel Frankel
  • 14,304
  • 8
  • 47
  • 69
  • All the code is here...[link](https://groups.google.com/forum/?fromgroups=#!topic/android-developers/3OXczPCSick) – user1909897 Dec 18 '12 at 14:44
0

I took a look at your code on google and I had this to ask:

Why are you re-declaring "data" again inside ListenBt() instead of manipulating the global final data variable?

EDIT: And you cannot manipulate a final variable by reassigning aliases, you'll get an exception.
...Well maybe with reflection, but that's cheating!

You could achieve what you are looking for by calling "this.data" while data is a public global variable and setting it equal to what's desired.

If you are currently getting "0" when calling the upload function, it means it's referencing the global final variable data instead of what was done inside ListenBt().

Community
  • 1
  • 1