The previous article has written how to turn on a bluetooth device and show that a pair has been successfully paired with a bluetooth device, http://blog.csdn.net/liuzuyi200/article/details/37740401
This article mainly describes how to search for Bluetooth devices.
(2) to search for a bluetooth device, you need to execute the startdiscovery () method, which lasts about 12 seconds.
If (mblustmthadapter. isdiscovering () {mblustmthadapter. canceldiscovery () ;}// start searching for the bluetooth device. The searched bluetooth device returns mblustmthadapter. startdiscovery () through broadcasting ();
To execute this method, you must register a broadcastreceiver with the intent property of descrithdevice. action_found.
private final BroadcastReceiver Receiver = new BroadcastReceiver(){ public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device =intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() != BluetoothDevice.BOND_BONDED) { textview1.append(device.getName() + ":" + device.getAddress() + "\n\n"); } }}};
Register broadcastreceiver with the following statement, which is used at the start and end of the call respectively.
IntentFilter Filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver( Receiver , Filter); Filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver( Receiver , Filter);
If the call ends, you must cancel the registration.
Protected void ondestroy () {// todo auto-generated method stub super. ondestroy (); // unregisterreceiver (receiver) is deregistered );}
The following figure shows the layout file and source code of the paired bluetooth device in textview.
Activity_main.xml
<ScrollView 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"><LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <TextView android:id="@+id/Textview1" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_weight="1" /></LinearLayout></ScrollView>
Mainactivity
Package COM. liuzuyi. bluetooth; import Java. util. set; import android. app. activity; import android. bluetooth. export thadapter; import android. bluetooth. export thdevice; import android. content. broadcastreceiver; import android. content. context; import android. content. intent; import android. content. intentfilter; import android. OS. bundle; import android. OS. parcelable; import android. view. menu; import android. view. me Nuitem; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. textview; import android. widget. toast; public class mainactivity extends activity {Private Static final int request_enable_bt = 1; private button; private textview textview1; protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentvi EW (R. layout. activity_main); button = (button) findviewbyid (R. id. button1); textview1 = (textview) findviewbyid (R. id. textview1); button. setonclicklistener (new blue (); intentfilter filter = new intentfilter (effecthdevice. action_found); registerreceiver (receiver, filter); filter = new intentfilter (descrithadapter. action_discovery_finished); registerreceiver (receiver, filter);} private final broadcas Treceiver receiver ER = new broadcastreceiver () {public void onreceive (context, intent) {string action = intent. getaction (); If (descrithdevice. action_found.equals (Action) {descrithdevice device = intent. getparcelableextra (effecthdevice. extra_device); If (device. getbondstate ()! = Effecthdevice. bond_bonded) {textview1.append (device. getname () + ":" + device. getaddress () + "\ n") ;}}}; protected void ondestroy () {// todo auto-generated method stub super. ondestroy (); // unregisterreceiver (reporter ER);} class blue implements onclicklistener {@ overridepublic void onclick (view v) {descrithadapter mbluw.thadapter = descrithadapter. getdefaadapter adapter (); If (mbluw.thad Apter = NULL) {toast. maketext (mainactivity. This, "this device does not support Bluetooth transmission! ", Toast. length_short). Show ();} else {toast. maketext (mainactivity. This," this device supports Bluetooth transmission! ", Toast. length_short). Show (); If (! Mblustmthadapter. isenabled () {intent enableintent = new intent (effecthadapter. action_request_enable); enableintent. putextra (effecthadapter. extra_discoverable_duration, 300); startactivityforresult (enableintent, request_enable_bt); toast. maketext (mainactivity. this, "the bluetooth device is on! ", Toast. length_short ). show ();} If (mbluw.thadapter. isdiscovering () {mbluw.thadapter. canceldiscovery ();} // start searching for the bluetooth device. The searched bluetooth device returns mbluw.thadapter through broadcast. startdiscovery (); set <symbol thdevice> symbol reddevices = mbluw.thadapter. getbondeddevices (); If (Response reddevices. size ()> 0) {for (FIG: FIG) {textview1.append (fig. getname () + ":" + descrithdevice. getaddress () + "\ n ");}}}}}}
Note: click the button to display the name and MAC address of an unpaired bluetooth device in textview. If you want to display all Bluetooth devices, remove the if statement in the onreceive method.