android - Error while Listing scanned Bluetooth Low Energy Device -
i developing android application scans , displays bluetooth low energy devices in listview. problem able display 1 item in listview if there many ble devices.i using arraylist , arrayadapter essential.below part of code, can tell me error , how can overcome it.
public class scanlist extends activity { public listview scandevicelist; private bluetoothadapter mbluetoothadapter; private handler mhandler; private arrayadapter<bluetoothdevice> adapter1; context context; activity activity; public arraylist<bluetoothdevice> devices; public string mdeviceaddress,mdevicename; private static final int request_enable_bt = 1; // stops scanning after 10 seconds. private static final long scan_period = 10000; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.scanned_device); scandevicelist = (listview) findviewbyid(r.id.scandevicelist); mhandler = new handler(); final bluetoothmanager bluetoothmanager = (bluetoothmanager) getsystemservice(context.bluetooth_service); mbluetoothadapter = bluetoothmanager.getadapter(); } @override protected void onresume() { super.onresume(); if (!mbluetoothadapter.isenabled()) { intent enablebtintent = new intent(bluetoothadapter.action_request_enable); startactivityforresult(enablebtintent, request_enable_bt); } scanledevice(true); } public void scanledevice(final boolean enable) { if (enable) { // stops scanning after pre-defined scan period. mhandler.postdelayed(new runnable() { @override public void run() { mscanning = false; mbluetoothadapter.stoplescan(mlescancallback); invalidateoptionsmenu(); } }, scan_period); mscanning = true; mbluetoothadapter.startlescan(mlescancallback); } else { mscanning = false; mbluetoothadapter.stoplescan(mlescancallback); } invalidateoptionsmenu(); } public bluetoothadapter.lescancallback mlescancallback = new bluetoothadapter.lescancallback() { @override public void onlescan(final bluetoothdevice device, int rssi, byte[] scanrecord) { runonuithread(new runnable() { @override public void run() { devices = new arraylist<bluetoothdevice>(); devices.add(device); adapter1 = new arrayadapter<bluetoothdevice>(scandevicelist.getcontext(), android.r.layout.simple_list_item_1,devices); scandevicelist.setadapter(adapter1); } }); } }; }
each time scan , onlescan return 1 item (although many becons advertising)
devices = new arraylist<bluetoothdevice>(); public bluetoothadapter.lescancallback mlescancallback = new bluetoothadapter.lescancallback() { @override public void onlescan(final bluetoothdevice device, int rssi, byte[] scanrecord) { runonuithread(new runnable() { @override public void run() { devices.add(device); } });
Comments
Post a Comment