근처의 Bluetooth 장치를 찾으려고하므로 BroadcastReceiver를 사용하고 있습니다. 대개의 경우 잘 작동하지만 때로는이 오류가 발생합니다.Bluetooth 검색에 대한 브로드 캐스트 오류 수신
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 [email protected]
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() 함수가 트리거 될 수 있습니까?
도움 주셔서 감사합니다.
EDIT : 라인 (532)은 따라서이 장치는 널이어야 의미하지만, 이것에 대한 이전 라인 검사
if ((device.getName() != null)||(device.getName().length() > 0)) {
이다.
if (device != null) {
동시에 두 개의 브로드 캐스트 수신기가 동시에 이런 종류의 오류를 일으킬 수 있습니까? BroadcastReceiver는 수신 된 후 의도를 null입니까?
아, NullPointerException이있는 특정 행은 무엇입니까? 예외가 발생하는 532 행을 실제로 강조 표시 할 수 있습니까? – Femi
@Femi 코드 줄을 강조 표시 할 수 없어서 주석을 추가하고 532 줄을 따로 따로 인용했습니다. – Mira