gpt4 book ai didi

android - 接收蓝牙发现的广播错误

转载 作者:行者123 更新时间:2023-11-29 22:07:47 24 4
gpt4 key购买 nike

我正在尝试查找附近的蓝牙设备,所以我正在使用 BroadcastReceiver。大多数时候它工作正常,但有时我会收到此错误。

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,但我不明白这个错误是如何发生的,因为我正在检查变量以避免异常。

// 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()函数是不是可以在没有接收到的时候触发?

感谢您的帮助。

编辑:第532行是

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

所以这意味着 device 必须为 null,但上一行对此进行了检查。

if (device != null) {

顺便问一下,两个广播接收器同时运行会导致这种错误吗? BroadcastReceiver 接收到 Intent 后是否为 null?

最佳答案

我怀疑你误用了||而不是 &&。

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

对比

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

如最初所写,如果 device.getName() 为 null,则它会尝试计算第二部分,即对 null 值调用 length(),从而导致您看到的 NPE。

关于android - 接收蓝牙发现的广播错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10376388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com