Previously written blog has been removed, do not know what reason, may be too few fonts, I do not like how to say, directly on the source is better
I believe that programmers do this, unless the code is difficult to understand and will explain,
First look at the following:
Look at the Java code
public class Mainactivity extends Activity {private ListView lv;private bluetoothadapter adapter;private myreciver myreci Ver;private arraylist<string> list;private arrayadapter<string> adapters; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); LV = (ListView) Findviewbyid (r.id.lv);//2, get adapter adapter = Bluetoothadapter.getdefaultadapter (); list = new arraylist< String> (); adapters = new Arrayadapter<string> (this,android. R.layout.simple_list_item_1,android. r.id.text1,list); Lv.setadapter (adapters);} public void Da (view view) {///3, turn on Bluetooth-----Two ways: Call system mode 2, Force open if (!adapter.isenabled ()) {///determine whether to open intent intent = new Intent ( bluetoothadapter.action_request_enable);//request was found Startactivityforresult (intent, 100);//Open will return the resource}//if (! Adapter.isenabled ()) {//adapter.enable ();//forcibly open//}//4, set visible intent intent = new Intent (Bluetoothadapter.action_ request_discoverable);//Set Visible Intent.putextra (Bluetoothadapter.extra_discoVerable_duration, 300);//sec startactivity (intent);} @Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {//TODO auto-generated method stub Super.onactivityresult (Requestcode, ResultCode, data); if (RESULTCODE==ACTIVITY.RESULT_OK) {Toast.maketext (this, " Already open ", 0). Show ();} Else{toast.maketext (This, "failed", 0). Show ();}} public void Sao (view view) {list.clear ();//5, scan bluetooth intentfilter intentfilter = new Intentfilter (bluetoothdevice.action_ FOUND); myreciver = new Myreciver (); Registerreceiver (Myreciver, intentfilter);//Start scanning adapter.startdiscovery ();} public void Guan (view view) {if (adapter.isenabled ()) {adapter.disable ();//Turn off Bluetooth}}public void cha (view view) {// Get paired set<bluetoothdevice> devices = Adapter.getbondeddevices (); List.clear (); for (Bluetoothdevice bluetoothdevice:devices) {List.add (Bluetoothdevice.getname () + "\ n" +bluetoothdevice.getaddress ()); Adapters.notifydatasetchanged ();}} Class Myreciver extends broadcastreceiver{@Overridepublic void OnReceive (Context contexT, Intent Intent) {//When the system discovers the device sends a broadcast, receives the broadcast, gets the Bluetooth device bluetoothdevice device = Intent.getparcelableextra ( Bluetoothdevice.extra_device), if (!list.contains (device.getname () + "\ n" +device.getaddress ())) {List.add ( Device.getname () + "\ n" +device.getaddress ()); adapters.notifydatasetchanged ();}}}
The above basic have comments, do not understand can contact me
The use of Bluetooth is required permission
<android:name= "Android.permission.BLUETOOTH"/> < Android:name = "Android.permission.BLUETOOTH_ADMIN"/>
Basic operation of Android Bluetooth