android - How to find available bluetooth devices in range? -
i trying find available bluetooth devices in range. getting 1 device using in thread in run method. checked many links problem on not resolve issue. here code
public void run() { if(service != null) { intentfilter filter = new intentfilter(bluetoothdevice.action_found); filter.addaction(bluetoothadapter.action_discovery_finished); service.registerreceiver(this.breceiver, filter); bluetooth.startdiscovery(); } } class bluetoothreceiver extends broadcastreceiver { public void onreceive(context context, intent intent) { set<bluetoothdevice> paireddevices = bluetooth.getbondeddevices(); string action = intent.getaction(); if (paireddevices.size() > 0) { (bluetoothdevice device : paireddevices) { int rssi = intent.getshortextra(bluetoothdevice.extra_rssi,short.min_value); log.d(tag, device.getname()); } } if(bluetoothdevice.action_found.equals(action)) { bluetoothdevice device = intent.getparcelableextra(bluetoothdevice.extra_device); string uuid = intent.getstringextra(bluetoothdevice.extra_uuid); int rssi = intent.getshortextra(bluetoothdevice.extra_rssi,short.min_value); log.d(tag, device.getname()); } } }
in addition want rssi value of each found device, please ignore syntax
this how search bluetooth devices in activity
, show name , mac-address in listview
. apart displaying devices in listview
, can practically discovered bluetoothdevice
object.
findbluetoothactivity.java
public class findbluetoothactivity extends activity { private bluetoothadapter mbtadapter; private listview mlvdevices; private arraylist<string> mdevicelist = new arraylist<string>(); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_find_bluetooth); mlvdevices = (listview) findviewbyid(r.id.lvdevices); intentfilter filter = new intentfilter(bluetoothdevice.action_found); registerreceiver(mbtreceiver, filter); // getting bluetooth adapter mbtadapter = bluetoothadapter.getdefaultadapter(); if(mbtadapter != null) { mbtadapter.startdiscovery(); toast.maketext(this, "starting discovery...", toast.length_short).show(); } else { toast.maketext(this, "bluetooth disabled or not available.", toast.length_short).show(); } } @override protected void ondestroy() { super.ondestroy(); if (mbtadapter != null) { mbtadapter.canceldiscovery(); } unregisterreceiver(mbtreceiver); } private final broadcastreceiver mbtreceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { string action = intent.getaction(); if (bluetoothdevice.action_found.equals(action)) { bluetoothdevice device = intent.getparcelableextra(bluetoothdevice.extra_device); mdevicelist.add(device.getaddress() + ", " + device.getname()); // mac address arrayadapter<string> adapter = new arrayadapter<string>(getapplicationcontext(), android.r.layout.simple_list_item_1, mdevicelist); mlvdevices.setadapter(adapter); } } }; }
layout .xml file:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".findbluetoothactivity" > <listview android:id="@+id/lvdevices" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignparentleft="true" android:layout_alignparenttop="true" > </listview> </relativelayout>
android manifest.xml file:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.bluetoothexample" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="11" android:targetsdkversion="18" /> <uses-permission android:name="android.permission.bluetooth"/> <uses-permission android:name="android.permission.bluetooth_admin"/> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/theme.holo.light" > <activity android:name="com.example.bluetoothexample.findbluetoothactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
additional information:
- make sure bluetooth enabled on device
- add permissions
android.permission.bluetooth
,android.permission.bluetooth_admin
manifest.xml file - make sure unregister broadcast-receiver when destroying
activity
- keep in mind bluetooth devices in range need discoverable found application. enabling bluetooth on them alone may not enough. case kind of "discoverable" mode needs enabled user before device can discovered other devices.
- be aware range in devices discoverable via bluetooth around 10m indoors , around 50m outdoors
Comments
Post a Comment