如何更改回调中按钮的文本? (机器人)

时间:2015-04-12 10:06:07

标签: android bluetooth callback scope

这是我的应用: 它进行蓝牙扫描,扫描功能在找到设备时进行回调。 我想在找到特定设备(它用设备名称识别)后,将特定按钮的文本从“搜索”更改为“连接”。

但是在回调范围内无法访问该按钮。

有没有办法做到这一点?我认为这纯粹是一个范围问题,我对这类事情几乎没有经验。

代码:

 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

2 个答案:

答案 0 :(得分: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);
                    } 

            }); 

答案 1 :(得分: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);
                }
            });
        }
    }
};