对象或服务内部的广播接收器?

时间:2019-02-09 18:30:57

标签: android bluetooth android-service android-broadcast

我有一个使用蓝牙的Android应用。为了始终具有蓝牙功能,我已经实现了一项服务,即使该应用程序在后台运行也始终处于打开状态。由于将有很多专用于蓝牙的代码,我决定为与蓝牙相关的所有方法创建一个ListActivity。每次启动服务时,都会创建此蓝牙类的新对象。

现在,考虑到这一点,我的问题是:如果我做适配器。在此对象中进行发现,如何处理广播接收器?对于接收者,我有:

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':bootRun'.

    Caused by: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_201\bin\java.exe'' finished with non-zero exit value 1
    at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:369)
Error Failed to start server (NOTE: Stack trace has been filtered. Use --verbose to see entire trace.)
java.util.concurrent.ExecutionException: org.gradle.tooling.BuildException: Could not execute build using Gradle distribution 'https://services.gradle.org/distributions/gradle-3.5-bin.zip'.
 Caused by: org.gradle.tooling.BuildException: Could not execute build using Gradle distribution 'https://services.gradle.org/distributions/gradle-3.5-bin.zip'.
    at org.gradle.tooling.internal.consumer.ExceptionTransformer.transform(ExceptionTransformer.java:51)
    Caused by: org.gradle.internal.exceptions.LocationAwareException: Execution failed for task ':bootRun'.
    at org.gradle.initialization.DefaultExceptionAnalyser.transform(DefaultExceptionAnalyser.java:74)
    at org.gradle.initialization.MultipleBuildFailuresExceptionAnalyser.transform(MultipleBuildFailuresExceptionAnalyser.java:47)

Caused by: org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':bootRun'.
           at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:112)
    ... 44 more
Caused by: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_201\bin\java.exe'' finished with non-zero exit value 1
    at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:369)
          at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:90)
    ... 78 more
| Error Failed to start server

我将其放在服务和对象中以进行不同的测试。要添加接收器,我有:

   private final BroadcastReceiver BEReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        int a = 1;
        a++;
};

我在服务的开头添加了这些行以在本地调用它,但是它不起作用。然后,我在用于蓝牙的对象内部创建了BEFilter,然后在这里放置了蓝牙对象的BEFilter。都不起作用。我还在蓝牙对象的构造函数中添加了这些行,以在本地执行此操作,但它们均无效。我该怎么办?发现适配器后,我需要添加设备。

清单上我有:

            IntentFilter BEFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
            mycontext.registerReceiver(BEReceiver, BEFilter);

对于蓝牙对象,服务以及创建服务的主要活动。

我的问题是,从不调用BEReceiver,并且调试中没有错误。

清单上我也有:

   <intent-filter>
                <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
            <action android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
            <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
            <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
     </intent-filter>

1 个答案:

答案 0 :(得分:0)

Android Broadcast Receiver bluetooth events catching

看来我要找的是:

IntentFilter filter2 = new IntentFilter();
filter2.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter2.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter2.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
registerReceiver(mBroadcastReceiver2, filter2);

与此有关,当适配器正在扫描时被触发。