Android ble app crash if bluetooth is turned off in android lollipop -
hello try scan bluetooth ble device android lollipop. working fine if turned off bluetotooth run app crash , give pop enable bluetooth.ideally should give pop enable bluetooth if turned off.
this method, should enable bluetooth:
private void enablebluetooth() { if(bluetoothadapter == null) { //bluetoothstate.settext("bluetooth not supported"); } else if(!bluetoothadapter.isenabled()) { //bluetoothadapter.enable(); intent enablebtintent = new intent(bluetoothadapter.action_request_enable); activity.startactivityforresult(enablebtintent, request_enable_bt); } }
code start scan
public void handlescanstart(view view) { founddevices.clear(); btarrayadapter.clear(); ble.startblescan(); scanbutton.setenabled(false); stopscanbutton.setenabled(true); }
enable start scan
public void startblescan() { if(getscanning()) { return; } enablebluetooth(); scanning = true; scanfilter.builder filterbuilder = new scanfilter.builder(); //todo default, scans devices scansettings.builder settingsbuilder = new scansettings.builder(); settingsbuilder.setscanmode(scansettings.scan_mode_low_latency); list<scanfilter> filters = new arraylist<scanfilter>(); filters.add(filterbuilder.build()); bluetoothlescanner.startscan(filters, settingsbuilder.build(), scancallback); log.d(tag, "bluetooth scanning..."); }
below log file
04-29 18:09:11.415: e/androidruntime(26155): fatal exception: main 04-29 18:09:11.415: e/androidruntime(26155): process: com.android.androidble5, pid: 26155 04-29 18:09:11.415: e/androidruntime(26155): java.lang.illegalstateexception: not execute method of activity 04-29 18:09:11.415: e/androidruntime(26155): @ android.view.view$1.onclick(view.java:4020) 04-29 18:09:11.415: e/androidruntime(26155): @ android.view.view.performclick(view.java:4780) 04-29 18:09:11.415: e/androidruntime(26155): @ android.view.view$performclick.run(view.java:19866) 04-29 18:09:11.415: e/androidruntime(26155): @ android.os.handler.handlecallback(handler.java:739) 04-29 18:09:11.415: e/androidruntime(26155): @ android.os.handler.dispatchmessage(handler.java:95) 04-29 18:09:11.415: e/androidruntime(26155): @ android.os.looper.loop(looper.java:135) 04-29 18:09:11.415: e/androidruntime(26155): @ android.app.activitythread.main(activitythread.java:5254) 04-29 18:09:11.415: e/androidruntime(26155): @ java.lang.reflect.method.invoke(native method) 04-29 18:09:11.415: e/androidruntime(26155): @ java.lang.reflect.method.invoke(method.java:372) 04-29 18:09:11.415: e/androidruntime(26155): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) 04-29 18:09:11.415: e/androidruntime(26155): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) 04-29 18:09:11.415: e/androidruntime(26155): caused by: java.lang.reflect.invocationtargetexception 04-29 18:09:11.415: e/androidruntime(26155): @ java.lang.reflect.method.invoke(native method) 04-29 18:09:11.415: e/androidruntime(26155): @ java.lang.reflect.method.invoke(method.java:372) 04-29 18:09:11.415: e/androidruntime(26155): @ android.view.view$1.onclick(view.java:4015) 04-29 18:09:11.415: e/androidruntime(26155): ... 10 more 04-29 18:09:11.415: e/androidruntime(26155): caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.bluetooth.le.bluetoothlescanner.startscan(java.util.list, android.bluetooth.le.scansettings, android.bluetooth.le.scancallback)' on null object reference 04-29 18:09:11.415: e/androidruntime(26155): @ com.android.androidble5.bluetoothutility.startblescan(bluetoothutility.java:204) 04-29 18:09:11.415: e/androidruntime(26155): @ com.android.androidble5.myactivity.handlescanstart(myactivity.java:247)
the correct approach before starting scan have check bluetoothlescanner object null or not check bluetoothadapter
sample
if(bluetoothlescanner != null){ bluetoothlescanner.startscan(filters,settingsbuilder.build(),scancallback); }
Comments
Post a Comment