Bluetooth operation for Android Development (2)-Modify the visibility of the local Bluetooth device and scan the available Bluetooth devices

Source: Internet
Author: User

1. Modify the visibility of the local Bluetooth device

2. Scan the available Bluetooth devices around

Eg:

I. configuration file adroidmanifest. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. se7en "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 8 "/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <activity Android: Name = ". mainactivity "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <ca Tegory Android: Name = "android. intent. category. launcher "/> </intent-filter> </activity> </Application> <uses-Permission Android: Name =" android. permission. bluetooth "/> <! -To manage a bluetooth device and modify its visibility, you need the following permissions: <uses-Permission Android: Name = "android. Permission. effecth_admin"/> </manifest>

Ii. layout file: Main. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/Hello"/> <button Android: Id = "@ + ID/discoverbutton" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "set visibility"/> <button Android: Id = "@ + ID/scanbutton" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "Start scan"/> </linearlayout>

3. mainactivity:

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. view. view; import android. view. view. onclicklistener; import android. widget. button; public class mainactivity extends activity {private button discoverbutton = NULL; private button scanbutton = NULL; private javasthadapter adapter = NULL; private extends threceiver = NULL; /** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); adapter = effecthadapter. getdefaadapter adapter (); discoverbutton = (button) findviewbyid (R. id. discoverbutton); scanbutton = (button) findviewbyid (R. id. scanbutton); // modify the visibility of the bluetooth device discoverbutton. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view) {intent discoverintent = new intent (descrithadapter. action_request_discoverable); // sets the Bluetooth visibility. 500 indicates the visible time (unit: seconds). If the value is greater than 300, the default value is 300discoverintent. putextra (effecthadapter. extra_discoverable_duration, 500); startactivity (discoverintent) ;}}); scanbutton. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {// start to scan the peripheral bluetooth device. This method is called asynchronously and returned as a broadcast mechanism, therefore, you need to create a broadcastreceiver to obtain the information adapter. startdisdevice () ;}}); // sets the filter intentfilter = new intentfilter (descrithdevice. action_found); // create a receiver for Bluetooth broadcast information (); // register a broadcast receiver (registerreceiver, intentfilter );} private class extends threceiver extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {// obtain the scanned remote Bluetooth device. Then thdevice device = intent. getparcelableextra (effecthdevice. extra_device); system. out. println (device. getaddress ());}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.