BLE android 5.0 Crash -


i have issues android , ble, time make scan app crash , don't know reason. use startlescan() can reason? here sample of code.

here place initialize api 18

//api 18 private bluetoothadapter.lescancallback mlescancallback = new bluetoothadapter.lescancallback() {      @override     public void onlescan(final bluetoothdevice device, int rssi,             byte[] scanrecord) {          sensortag sensortag = new sensortag(device, sensortagscanner.this,                 msensortagscannercallback);          synchronized (this) {             (sensortag other : mdiscovereddevices) {                 if (sensortag.getname().equals(other.getname())) {                     log.i("sensortagscanner",                             "discovered duplicate device: "                                     + other.getname());                     return;                 }             }         }          mdiscovereddevices.add(sensortag);         log.i("sensortagscanner",                 "discovered device named " + device.getname() + ".");         mcallback.onsensordiscovered(sensortag);     } }; 

here initialize scancall , seems wrong looking error java nulle exception //api 21

@targetapi(build.version_codes.lollipop) private void initscancallback() {      if (scancallback != null)         return;     this.scancallback = new scancallback() {         @override         public void onscanresult(int callbacktype, scanresult result) {             super.onscanresult(callbacktype, result);             // whatever want         }          @override         public void onbatchscanresults(list<scanresult> results) {             super.onbatchscanresults(results);             log.d(tag, "batch scan results: ");             (scanresult result : results) {                 log.d(tag, "batch scan result: " + result.tostring());                 // whatever want             }         }          @override         public void onscanfailed(int errorcode) {             super.onscanfailed(errorcode);             // scanlistener.onscanfinished();             log.d(tag, "scan failed");         }     }; } 

here start scan

    @override     public void startscan(sensorscannercallback callback) {      mcallback = callback;      if (mbluetoothadapter == null || !mbluetoothadapter.isenabled()) {         callback.onscannerunavailable(this);         return;     }      mhandler.postdelayed(new runnable() {         @override         public void run() {             stopscan();         }     }, scan_period);      log.i("sensortagscanner", "starting scan...");      mscanning = true;      // ////////////pour api 21//////////////     if (build.version.sdk_int >= build.version_codes.lollipop) {         mbluetoothadapter.getbluetoothlescanner().startscan(scancallback);     }      // /////////pour api <18////////////////     else {         mbluetoothadapter.startlescan(mlescancallback);     } } 

try using method android 5 in snippet below. might help. if not, please show more of code , error have when crash happens.

first, need callback, initialize correctly depending on android version.

private void initscancallback(){     if(build.version.sdk_int >= build.version_codes.lollipop) {         initcallbacklollipop();     }else{         initscancallbacksupport();     } }  @targetapi(build.version_codes.lollipop) private void initcallbacklollipop(){     if(scancallback != null) return;     this.scancallback = new scancallback() {         @override         public void onscanresult(int callbacktype, scanresult result) {             super.onscanresult(callbacktype, result);             //do whatever want         }          @override         public void onbatchscanresults(list<scanresult> results) {             super.onbatchscanresults(results);             log.d(tag, "batch scan results: ");             (scanresult result: results){                 log.d(tag, "batch scan result: " + result.tostring());                 //do whatever want             }         }          @override         public void onscanfailed(int errorcode) {             super.onscanfailed(errorcode);             log.d(tag, "scan failed");         }     }; }  private void initscancallbacksupport(){     if(callback != null) return;     this.callback = new bluetoothadapter.lescancallback() {         @override         public void onlescan(final bluetoothdevice device, int rssi, final byte[] scanrecord) {             string address = device.tostring();             string name = device.getname();              // parse uuids according stackoverflow post             //list<uuid> uuids = parseuuids(scanrecord);              // todo compare detected uuids uuidfilter             //for(uuid discuuid : uuids)             //    for(uuid uuid : blescanservice.this.uuidfilter)             //        if(discuuid.equals(uuid))                         blescanservice.this.scanlistener.ondevicescanned(address, name, uuids, rssi);         }     }; } 

then can start scan according android version:

public void startscanwithservices(list<string> uuidfilters){     if(build.version.sdk_int >= build.version_codes.lollipop) {         scanlollipop(uuidfilters);     }else{         scansupport(uuidfilters);     }      // store filters later filtering     int size = uuidfilters.size();     this.uuidfilter = new arraylist<uuid>();     for(int = 0; i<size; i++){         this.uuidfilter.add(uuid.fromstring(uuidfilters.get(i)));     }      if(buildconfig.debug) log.d(tag, "ble scan started"); }  @targetapi(build.version_codes.lollipop) private void scanlollipop(list<string> uuidfilters){     if(scancallback == null)         initcallbacklollipop();      list<scanfilter> filters = new arraylist<scanfilter>();      scansettings settings = new scansettings.builder()             .setscanmode(scansettings.scan_mode_low_latency) // or balanced             .setreportdelay(0)             .build();      bluetoothadapter.getbluetoothlescanner().startscan(filters, settings, scancallback);     //bluetoothadapter.getbluetoothlescanner().startscan(scancallback); }  private void scansupport(list<string> uuidfilters){     if(callback == null)         initscancallbacksupport();      //start scan     //boolean success = bluetoothadapter.startlescan(uuids, callback);      boolean success = bluetoothadapter.startlescan(callback);      //check scan success     if(!success) {         if(buildconfig.debug) log.d(tag, "ble scan failed");         scanlistener.onscanfinished();     } } 

and can stop scan this:

public void stopscan(){     if(build.version.sdk_int >= build.version_codes.lollipop) {         // stop scan , flush pending scan         bluetoothadapter.getbluetoothlescanner().flushpendingscanresults(scancallback);         bluetoothadapter.getbluetoothlescanner().stopscan(scancallback);     }else{         bluetoothadapter.stoplescan(callback);     }      if(buildconfig.debug) log.d(tag, "ble scan stopted"); } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -