如何使用BroadcastReceiver检测蓝牙状态

时间:2015-02-07 19:15:17

标签: android android-intent bluetooth

我遇到了问题。我读过这个话题: How to detect Bluetooth state change using a broadcast receiver? 我以相同的方式编写了我的应用程序但是当我运行它并关闭应用程序的蓝牙将崩溃。系统版本:4.0.3。这是代码:

MainActivity:

package com.example.lokalizing;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                                                 BluetoothAdapter.ERROR);
            switch (state) {
            case BluetoothAdapter.STATE_OFF:
                Toast.makeText(context, "Bluetooth off", Toast.LENGTH_LONG);
                break;
            case BluetoothAdapter.STATE_TURNING_OFF:
                Toast.makeText(context, "Turning Bluetooth off", Toast.LENGTH_LONG);
                break;
            case BluetoothAdapter.STATE_ON:
                Toast.makeText(context, "Bluetooth on", Toast.LENGTH_LONG);
                break;
            case BluetoothAdapter.STATE_TURNING_ON:
                Toast.makeText(context, "Turning Bluetooth on", Toast.LENGTH_LONG);
                break;
            }
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toast.makeText(getApplicationContext(), "Włączono program", Toast.LENGTH_LONG).show();
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(mReceiver, filter);
}

@Override
public void onDestroy() {
    super.onDestroy();

    /* ... */

    // Unregister broadcast listeners
    unregisterReceiver(mReceiver);
}

public void showToast(String msg) {
    Toast.makeText(getApplicationContext(),msg, Toast.LENGTH_SHORT).show();
}
}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lokalizing"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="15" />

<uses-permission android:name="android.permission.BLUETOOTH" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <receiver
        android:name="com.example.lokalizing.MainActivity"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
        </intent-filter>
    </receiver>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

</manifest>

2 个答案:

答案 0 :(得分:2)

在Toast实现中,您错过了调用方法show();

答案 1 :(得分:1)

从清单中删除<receiver>声明。

您已将MainActivity声明为<activity><receiver>。这是一个明确的禁忌。

当您在代码中动态创建BroadcastReceiver并在onCreate()中注册时。你不需要在清单中有任何东西。