Android:蓝牙,wifi,网络信息,广播接收器不在设备/模拟器中工作

时间:2012-05-29 06:53:01

标签: android bluetooth broadcastreceiver wifi

当我使用广播接收器在wifi,蓝牙开/关时使用广播接收器显示我的应用程序时,我感到困惑

当我添加蓝牙代码时,应用程序崩溃,在此之前,无线和网络连接在EMULATOR中显示“已连接”(虽然wifi /网络连接在模拟器中关闭)

如何解决这个问题,我不知道我哪里错了。

这是我的代码

public class BroadCastSampleActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.registerReceiver(this.mConnReceiver, new IntentFilter(
            ConnectivityManager.CONNECTIVITY_ACTION));
        this.registerReceiver(this.mConnReceiver, new IntentFilter(
            WifiManager.WIFI_STATE_CHANGED_ACTION));
        this.registerReceiver(this.mConnReceiver, new IntentFilter(
            BluetoothAdapter.ACTION_STATE_CHANGED));
    }

    private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            boolean noConnectivity = intent.getBooleanExtra(
                ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
            String reason = intent
                .getStringExtra(ConnectivityManager.EXTRA_REASON);
            boolean isFailover = intent.getBooleanExtra(
                ConnectivityManager.EXTRA_IS_FAILOVER, false);
            boolean noConnectivity1 = intent.getBooleanExtra(
                BluetoothAdapter.ACTION_STATE_CHANGED, false);
            boolean isfailover = intent.getBooleanExtra(
                BluetoothAdapter.EXTRA_STATE, false);
            NetworkInfo currentNetworkInfo = (NetworkInfo) intent
                .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            NetworkInfo otherNetworkInfo = (NetworkInfo) intent
                .getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
            NetworkInfo currentwifiInfo = (NetworkInfo) intent
                .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            NetworkInfo otherwifiInfo = (NetworkInfo) intent
                .getParcelableExtra(WifiManager.NETWORK_STATE_CHANGED_ACTION);
            NetworkInfo currentbluetoothinfo = (NetworkInfo) intent
                .getParcelableExtra(BluetoothAdapter.EXTRA_STATE);
            NetworkInfo otherbluetoothinfo = (NetworkInfo) intent
                .getParcelableExtra(BluetoothAdapter.ACTION_STATE_CHANGED);
            if (currentNetworkInfo.isConnected()) {
                Toast.makeText(getApplicationContext(),
                    "Internet Connected", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(),
                    " Internet Not Connected", Toast.LENGTH_LONG).show();
            }
            if (currentwifiInfo.isConnected()) {
                Toast.makeText(getApplicationContext(), "wifi Connected",
                    Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(),
                    " WIFI Not Connected", Toast.LENGTH_LONG).show();
            }
            if (currentbluetoothinfo.isAvailable()) {
                Toast.makeText(getApplicationContext(),
                    "Bluetooth Connected", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(),
                    "Bluetooth Not Connected", Toast.LENGTH_LONG).show();
            }
        }
    };
}

这是我的清单

 <uses-sdk android:minSdkVersion="10" />
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WIFI_STATE_CHANGED" />
 <uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".BroadCastSampleActivity"
        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>
当我添加蓝牙代码时,

logcat显示这些错误

05-29 06:39:27.457: E/AndroidRuntime(384): FATAL EXCEPTION: main
05-29 06:39:27.457: E/AndroidRuntime(384): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x10000000 (has extras) } in com.raghav.broadcast.BroadCastSampleActivity$1@40518c50
 05-29 06:39:27.457: E/AndroidRuntime(384):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
 05-29 06:39:27.457: E/AndroidRuntime(384):     at android.os.Handler.handleCallback(Handler.java:587)
05-29 06:39:27.457: E/AndroidRuntime(384):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-29 06:39:27.457: E/AndroidRuntime(384):  at android.os.Looper.loop(Looper.java:123)
05-29 06:39:27.457: E/AndroidRuntime(384):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-29 06:39:27.457: E/AndroidRuntime(384):  at java.lang.reflect.Method.invokeNative(Native Method)
05-29 06:39:27.457: E/AndroidRuntime(384):  at java.lang.reflect.Method.invoke(Method.java:507)
 05-29 06:39:27.457: E/AndroidRuntime(384):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-29 06:39:27.457: E/AndroidRuntime(384):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-29 06:39:27.457: E/AndroidRuntime(384):  at dalvik.system.NativeStart.main(Native Method)
05-29 06:39:27.457: E/AndroidRuntime(384): Caused by: java.lang.NullPointerException
05-29 06:39:27.457: E/AndroidRuntime(384):  at com.raghav.broadcast.BroadCastSampleActivity$1.onReceive(BroadCastSampleActivity.java:80)
 05-29 06:39:27.457: E/AndroidRuntime(384):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
05-29 06:39:27.457: E/AndroidRuntime(384):  ... 9 more

1 个答案:

答案 0 :(得分:2)

模拟器中不支持蓝牙和WiFi。你必须使用真实的设备。