接收广播错误蓝牙发现 [英] Receiving broadcast error for Bluetooth discovery

查看:329
本文介绍了接收广播错误蓝牙发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我使用广播接收器来查找附近的蓝牙设备。大多数时候,它工作正常,但有时我得到这个错误。

I am trying to find nearby Bluetooth devices so I'm using BroadcastReceiver. Most of the time it works fine but sometimes I get this error.

04-30 09:50:15.277: E/AndroidRuntime(847): FATAL EXCEPTION: main
04-30 09:50:15.277: E/AndroidRuntime(847): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.device.action.FOUND (has extras) } in com.waratah.app.SetMachineActivity$2@40582d20
04-30 09:50:15.277: E/AndroidRuntime(847):  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
04-30 09:50:15.277: E/AndroidRuntime(847):  at android.os.Handler.handleCallback(Handler.java:587)
04-30 09:50:15.277: E/AndroidRuntime(847):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-30 09:50:15.277: E/AndroidRuntime(847):  at android.os.Looper.loop(Looper.java:130)
04-30 09:50:15.277: E/AndroidRuntime(847):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-30 09:50:15.277: E/AndroidRuntime(847):  at java.lang.reflect.Method.invokeNative(Native Method)
04-30 09:50:15.277: E/AndroidRuntime(847):  at java.lang.reflect.Method.invoke(Method.java:507)
04-30 09:50:15.277: E/AndroidRuntime(847):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-30 09:50:15.277: E/AndroidRuntime(847):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-30 09:50:15.277: E/AndroidRuntime(847):  at dalvik.system.NativeStart.main(Native Method)
04-30 09:50:15.277: E/AndroidRuntime(847): Caused by: java.lang.NullPointerException
04-30 09:50:15.277: E/AndroidRuntime(847):  at com.waratah.app.SetMachineActivity$2.onReceive(SetMachineActivity.java:532)
04-30 09:50:15.277: E/AndroidRuntime(847):  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
04-30 09:50:15.277: E/AndroidRuntime(847):  ... 9 more

这表明NullPointerException异常,但我不明白怎么能发生这种错误,因为我检查变量,以避免例外。

It shows NullPointerException but I don't see how this error can occur because I'm checking the variables to avoid the exception.

// The BroadcastReceiver that listens for discovered devices and
// changes the title when discovery is finished
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null) {
            return;
        }
        String action = intent.getAction();
        int i;
        Machine d;

        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            if (mBtAdapter.isEnabled()) {
                if(D) Log.d(BroadcastReceiver.class.getName(), "Bluetooth is enabled");
                pd.dismiss();                   
            }
            doDiscovery();

        // when bluetooth device is found 
        } else if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
            if(D) Log.d(BroadcastReceiver.class.getName(), "found");
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device != null) {

                // Check if the device has name
                if ((device.getName() != null)||(device.getName().length() > 0)) { //LINE 532 HERE 
                    d = new Machine(device);
                    if (d.getPairingState() != BluetoothDevice.BOND_BONDED) {
                        d.setBluetoothState(true);
                        addDevice(d);
                    } else {
                        for (i = 0; i < adapter.getCount(); i++) {
                            if (adapter.getItem(i).getAddress().equals(device.getAddress())) {
                                    adapter.getItem(i).setBluetoothState(true);
                            }
                        }
                    }
                }
            }
        // When discovery is finished, change the Activity title
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            scanButton.setVisibility(View.VISIBLE);

            pb.setVisibility(View.GONE);
            scanning.setVisibility(View.GONE);
        }
    }
};

我已经注册了我所有的行动和正确注册我的接收器。只是偶尔会出现此错误。是否有可能时,有什么可接收到被触发的onReceive()函数?

I have registered all my actions and registered my receiver correctly. This error occurs only occasionally. Is it possible for the onReceive() function to be triggered when there is nothing to receive?

感谢您的帮助。

编辑:
532线是

Line 532 is

 if ((device.getName() != null)||(device.getName().length() > 0)) { 

所以这意味着设备必须是空的,但这样做的previous线检查。

so this means device must be null, but the previous line checks for this.

if (device != null) {

对了可以同时运行两个广播接收机造成这种错误的?是否收到它后的BroadcastReceiver空的意图是什么?

By the way could two broadcast receivers running at the same time cause this kind of error? Does the BroadcastReceiver null the intent after it is received?

推荐答案

我怀疑你错误地使用了||而不是和放大器;&安培;

I suspect you mistakenly used || instead of &&.

如果(!(device.getName()= NULL)||(device.getName()长()方式&gt; 0)){

VS

如果(!(device.getName()= NULL)及和放大器;(device.getName()长()0)){

至于最初写,如果device.getName()为null,则它尝试评估的第二部分,它呼吁空值长度(),从而导致您所看到的NPE。

As originally written, if device.getName() is null, then it attempts to evaluate the second part, which is calling length() on the null value, thus resulting in the NPE you are seeing.

这篇关于接收广播错误蓝牙发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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