2015-04-12 71 views
0

以下是我的應用程序: 它執行藍牙掃描,掃描功能在找到設備時會回調。 我想將特定按鈕的文本從「搜索」更改爲「連接」,一旦它找到一個特定的設備(它用設備名稱識別)。如何在回調中更改按鈕的文本? (Android)

但是,該按鈕在回調的範圍內無法訪問。

有沒有辦法做到這一點?我認爲這純粹是一個範圍問題,而我對這類事情幾乎沒有經驗。

代碼:

Context context; 
    context = this; 

    update_str = ""; 

    Button ConnectButton = (Button) findViewById(R.id.Connect); 
    ConnectButton.setText("Waiting for device to be found"); 

    Button ScanButton = (Button) findViewById(R.id.Scan); 

    ScanButton.setOnClickListener(new View.OnClickListener() { 
             public void onClick(View v) { 
              btAdapter.startLeScan(leScanCallback); 
             } 
            }); 

    BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() { 
     public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) { 

      update_str = update_str.concat(" " + device.getName()); 
      ((TextView)findViewById (R.id.text)).setText (update_str); 
      nom_device = device.getName(); 
      if (nom_device=="bill_gates"){ 

       context.ConnectButton.setText(nom_device); 

       context.ConnectButton.setOnClickListener(new View.OnClickListener() { 

        public void onClick(View v) { 
         BluetoothGatt bluetoothGatt = device.connectGatt(getApplicationContext(), false, context.btleGattCallback); 
        } 

      }); 
     } 

     } 
    }; 

編譯器錯誤日誌:

C:\Code\Grizz\app\src\main\java\com\grizz\grizzmvp\MainActivity.java 
 
Error:(84, 28) error: cannot find symbol variable ConnectButton 
 
Error:(89, 117) error: cannot find symbol variable btleGattCallback 
 
Error:(86, 28) error: cannot find symbol variable ConnectButton 
 
Note: C:\Code\Grizz\app\src\main\java\com\grizz\grizzmvp\MainActivity.java uses or overrides a deprecated API. 
 
Note: Recompile with -Xlint:deprecation for details. 
 
Error:Execution failed for task ':app:compileDebugJava'. 
 
> Compilation failed; see the compiler error output for details. 
 
Information:BUILD FAILED 
 
Information:Total time: 1.381 secs 
 
Information:4 errors 
 
Information:0 warnings 
 
Information:See complete output in console

+0

您不必在每次想要更改某物時都按Id查找按鈕。在第一行中聲明它後,第二行可能只是:'ConnectButton.setText(Searching ...「);'。哦,你有多酷的名字! – Barthy

+0

爲什麼你有一個Button叫做」ConnectButton 「,其中ID爲」連接「和另一個ID爲」ConnectButton「的按鈕?它也將有助於看到您的logcat /錯誤日誌 – Barthy

+0

這是意外的,並已按照您的建議更正 – Barth

回答

0

當在方法體內定義一個匿名內部類時,所有在該方法範圍內聲明爲final的變量都是 ,它們可以從內部類中訪問。對於標量值,一旦分配了 ,則最終變量的值不能更改。對於 對象值,引用無法更改。這允許Java 編譯器在運行時「捕獲」變量的值,並將 副本作爲內部類中的字段存儲。一旦外部方法終止並且其堆棧幀已被移除,則原始變量 消失,但內部類的私有副本仍然存在於該類自己的 內存中。

final Button ConnectButton = (Button) findViewById(R.id.Connect); 
ConnectButton.setText("Waiting for device to be found"); 

final Button ScanButton = (Button) findViewById(R.id.Scan); 
ScanButton.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     btAdapter.startLeScan(leScanCallback); 
    } 
}); 

BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() { 
    public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) { 
     update_str = update_str.concat(" " + device.getName()); 
     ((TextView)findViewById (R.id.text)).setText (update_str); 
     nom_device = device.getName(); 
     if (nom_device.equals("bill_gates")){ 

      ConnectButton.setText(nom_device); 
      ConnectButton.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        BluetoothGatt bluetoothGatt = device.connectGatt(getApplicationContext(), false, context.btleGattCallback); 
       } 
      }); 
     } 
    } 
}; 
0

嘗試:

Context context ; //Global 
    context=this; //In oncreate of activity, 

// construction of button 
Button ConnectButton = (Button) findViewById(R.id.Connect); 
((Button)findViewById(R.id.ConnectButton)).setText("Searching.."); 

public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) { 

      nom_device = device.getName(); 
      if (nom_device=="bill_gates"){ 
       ((Button)findViewById(R.id.ConnectButton)).setText("Connect"); 

       // This part fails because the callback doesn't recognize ConnectButton. It's out of his scope. 
       context.ConnectButton.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View v) { 
         BluetoothGatt bluetoothGatt = device.connectGatt(getApplicationContext(), false, btleGattCallback); 
        } 

      }); 
+0

感謝您的回答,但問題仍然存在,即使var上下文似乎被識別,與ConnectButton相反...? – Barth

+0

我不確定這個問題,你能否像你看到的那樣更新代碼,可能是一些AS或編譯器給你的確切錯誤的截圖 – Kay