2

我被我的应用程序卡住了,我使用广播接收器在 wifi、蓝牙打开/关闭时显示 toast

当我添加蓝牙代码时,应用程序崩溃了,在此之前,对于 wifi 和网络连接,它在模拟器中显示“已连接”(尽管模拟器中的 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
4

1 回答 1

2

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

于 2012-05-29T07:29:35.730 回答