安卓:监控蓝牙发现 - 这是什么编译错误是什么意思? [英] Android: Monitoring Bluetooth Discovery - what does this compile error mean?

查看:241
本文介绍了安卓:监控蓝牙发现 - 这是什么编译错误是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用Wrox的的专业的Andr​​oid 2应用开发的书在Android的蓝牙编程。发现监控器的例子(第432)有这个code片断:

I'm learning Bluetooth programming on Android using the Wrox Professional Android 2 Application Development book. The discovery monitor example (pg 432) has this code snippet:

    BroadcastReceiver discoveryMonitor = new BroadcastReceiver() {

          String dStarted = BluetoothAdapter.ACTION_DISCOVERY_STARTED;
          String dFinished = BluetoothAdapter.ACTION_DISCOVERY_FINISHED;

          @Override
          public void onReceive(Context context, Intent intent) {
            if (dStarted.equals(intent.getAction())) { 
              // Discovery has started.
              Toast.makeText(getApplicationContext(),
                             "Discovery Started...", Toast.LENGTH_SHORT).show();
            }
            else if (dFinished.equals(intent.getAction())) {
              // Discovery has completed.
              Toast.makeText(getApplicationContext(),
                             "Discovery Completed...", Toast.LENGTH_SHORT).show();
            }
          }      
        };
        registerReceiver(discoveryMonitor, 
                         new IntentFilter(dStarted));
        registerReceiver(discoveryMonitor, 
                         new IntentFilter(dFinished));

...并在每两个​​在最后我得到registerReceiver电话。 。

... and on each of the two registerReceiver calls at the end I get . . .

上的语法错误标记,AnnotationName预期,而不是​​结果
语法错误,将类型VariableDeclaratorId来完成FormalParameterList

什么是annotationName和发生了什么事情错在这里?

What's an annotationName and what's going wrong here?

在此先感谢!

推荐答案

您的问题是你定义的变量 dStarted dFinished 作为当地人 discoveryMonitor 广播接收器,
这样,你不能在registerReceiver使用它们(...)

Your problem is that you defined variables dStarted and dFinished as locals for discoveryMonitor BroadcastReceiver, That way, you can't use them in registerReceiver(...)

您必须将它们定义为全球性的,或者使用

You have to define them as global, or use

registerReceiver(discoveryMonitor, new 
                 IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED));

代替。

这篇关于安卓:监控蓝牙发现 - 这是什么编译错误是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆