Android 4.3: How to connect to multiple Bluetooth Low Energy devices? -
task:
i developing android application need simultaneously connect multiple (identical, can differentiated via id) ble chip devices in order send , receive updates. have used tutorials on google's official web page:
http://developer.android.com/guide/topics/connectivity/bluetooth-le.html
this has helped me create devicescanactivity java class allows me scan , list of available ble devices in close proximity, applications of type do. found out 7 external slaves can connected same master device simultaneously. however, implementing communication still unclear me. useful link is:
android 4.3: how connect multiple bluetooth low energy devices
however, not provide enough detail me understand how such implementation works.
i have been researching topic while , not able find example implementations. aware question has been asked lot of times there not seem working solution/ demo available online make things more clear me. appreciate lot if can point me resource/ working solution explain in detail steps necessary modify existing java class in order implement functionality.
devicescanactivity.java:
import android.app.activity; import android.app.listactivity; import android.app.progressdialog; import android.bluetooth.bluetoothadapter; import android.bluetooth.bluetoothdevice; import android.bluetooth.bluetoothmanager; import android.content.context; import android.content.intent; import android.content.sharedpreferences; import android.content.pm.packagemanager; import android.os.bundle; import android.os.handler; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.listview; import android.widget.textview; import android.widget.toast; import java.util.arraylist; /** * activity scanning , displaying available bluetooth le devices. */ public class devicescanactivity extends listactivity { private ledevicelistadapter mledevicelistadapter; private bluetoothadapter mbluetoothadapter; private boolean mscanning; private handler mhandler; private static final int request_enable_bt = 1; // stops scanning after 10 seconds. private static final long scan_period = 10000; private static final string config_file = "file"; private int _countclick = 0; private boolean experimentermode = false; private string paireddeviceaddress = ""; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); getactionbar().settitle(r.string.title_devices); mhandler = new handler(); // use check determine whether ble supported on device. can // selectively disable ble-related features. if (!getpackagemanager().hassystemfeature(packagemanager.feature_bluetooth_le)) { toast.maketext(this, r.string.ble_not_supported, toast.length_short).show(); finish(); } // initializes bluetooth adapter. api level 18 , above, reference // bluetoothadapter through bluetoothmanager. final bluetoothmanager bluetoothmanager = (bluetoothmanager) getsystemservice(context.bluetooth_service); mbluetoothadapter = bluetoothmanager.getadapter(); // checks if bluetooth supported on device. if (mbluetoothadapter == null) { toast.maketext(this, r.string.error_bluetooth_not_supported, toast.length_short).show(); finish(); return; } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); if (!mscanning) { menu.finditem(r.id.menu_stop).setvisible(false); menu.finditem(r.id.menu_scan).setvisible(true); menu.finditem(r.id.menu_refresh).setactionview(null); } else { menu.finditem(r.id.menu_stop).setvisible(true); menu.finditem(r.id.menu_scan).setvisible(false); menu.finditem(r.id.menu_refresh).setactionview( r.layout.actionbar_indeterminate_progress); } return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.menu_scan: mledevicelistadapter.clear(); scanledevice(true); break; case r.id.menu_stop: scanledevice(false); break; case android.r.id.home: //onbackpressed(); _countclick++; if(_countclick>8) { toggleexperimentermode(); } return true; } return super.onoptionsitemselected(item); } @override protected void onresume() { super.onresume(); // ensures bluetooth enabled on device. if bluetooth not enabled, // fire intent display dialog asking user grant permission enable it. if (!mbluetoothadapter.isenabled()) { if (!mbluetoothadapter.isenabled()) { intent enablebtintent = new intent(bluetoothadapter.action_request_enable); startactivityforresult(enablebtintent, request_enable_bt); } } // restore preferences sharedpreferences settings = getsharedpreferences(config_file, 0); experimentermode = settings.getboolean("experimentermode", false); paireddeviceaddress = settings.getstring("paireddeviceaddress", ""); // initializes list view adapter. mledevicelistadapter = new ledevicelistadapter(); setlistadapter(mledevicelistadapter); scanledevice(true); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { // user chose not enable bluetooth. if (requestcode == request_enable_bt && resultcode == activity.result_canceled) { finish(); return; } super.onactivityresult(requestcode, resultcode, data); } @override protected void onpause() { super.onpause(); scanledevice(false); mledevicelistadapter.clear(); } @override protected void onlistitemclick(listview l, view v, int position, long id) { final bluetoothdevice device = mledevicelistadapter.getdevice(position); if (device == null) return; connecttodevice(device); } private void connecttodevice(bluetoothdevice device){ final intent intent = new intent(this, devicecontrolactivity.class); intent.putextra(devicecontrolactivity.extras_device_name, device.getname()); intent.putextra(devicecontrolactivity.extras_device_address, device.getaddress()); if (mscanning) { mbluetoothadapter.stoplescan(mlescancallback); mscanning = false; } sharedpreferences settings = getsharedpreferences(config_file, 0); sharedpreferences.editor editor = settings.edit(); editor.putstring("paireddeviceaddress", device.getaddress()); editor.commit(); startactivity(intent); } //--------------------------------------------------- private void scanledevice(final boolean enable) { progressdialog progress; 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(); } private void toggleexperimentermode(){ _countclick=0; experimentermode = !experimentermode; sharedpreferences settings = getsharedpreferences(config_file, 0); sharedpreferences.editor editor = settings.edit(); editor.putboolean("experimentermode", experimentermode); editor.commit(); string t = experimentermode ? "you in experimenter mode":"you in user mode"; toast.maketext(this, t, toast.length_short).show(); } // adapter holding devices found through scanning. private class ledevicelistadapter extends baseadapter { private arraylist<bluetoothdevice> mledevices; private layoutinflater minflator; public ledevicelistadapter() { super(); mledevices = new arraylist<bluetoothdevice>(); minflator = devicescanactivity.this.getlayoutinflater(); } public void adddevice(bluetoothdevice device) { if(!mledevices.contains(device)) { mledevices.add(device); } if (!experimentermode && ! paireddeviceaddress.isempty()) { if (device.getaddress().equals(paireddeviceaddress)) { connecttodevice(device); } } } public bluetoothdevice getdevice(int position) { return mledevices.get(position); } public void clear() { mledevices.clear(); } @override public int getcount() { return mledevices.size(); } @override public object getitem(int i) { return mledevices.get(i); } @override public long getitemid(int i) { return i; } @override public view getview(int i, view view, viewgroup viewgroup) { viewholder viewholder; // general listview optimization code. if (view == null) { view = minflator.inflate(r.layout.listitem_device, null); viewholder = new viewholder(); viewholder.deviceaddress = (textview) view.findviewbyid(r.id.device_address); viewholder.devicename = (textview) view.findviewbyid(r.id.device_name); view.settag(viewholder); } else { viewholder = (viewholder) view.gettag(); } bluetoothdevice device = mledevices.get(i); final string devicename = device.getname(); if (devicename != null && devicename.length() > 0) viewholder.devicename.settext(devicename); else viewholder.devicename.settext(r.string.unknown_device); viewholder.deviceaddress.settext(device.getaddress()); return view; } } // device scan callback. private bluetoothadapter.lescancallback mlescancallback = new bluetoothadapter.lescancallback() { @override public void onlescan(final bluetoothdevice device, int rssi, byte[] scanrecord) { runonuithread(new runnable() { @override public void run() { mledevicelistadapter.adddevice(device); mledevicelistadapter.notifydatasetchanged(); } }); } }; static class viewholder { textview devicename; textview deviceaddress; } }
current information:
- android 4.3: how connect multiple bluetooth low energy devices
- https://developer.android.com/guide/topics/connectivity/bluetooth-le.html
- does android 4.3 support multiple ble device connections?
- ble multiple connection
- https://labs.hybris.com/2014/10/06/connecting-to-multiple-ble-devices/
- https://e2e.ti.com/support/wireless_connectivity/f/538/t/225935
all of these pages contain directions, not provide enough detail on how multiple connections can implemented.
ui target representation:
the idea 5.1.1 android nexus 7 device able connect @ 5 ble chip slaves , send/receive updates.
Comments
Post a Comment