Bluetooth communication-if local Bluetooth is allowed to be discovered by other nearby bluetooth devices

Source: Internet
Author: User

If the local Bluetooth device can be found by other nearby bluetooth devices, you can use the following method with comments in the code.

Of course, you need to set up your Bluetooth device to be detected by nearby bluetooth devices (usually 2 minutes)

In settings-Bluetooth-select the detection check box. My data is android4.1.1. Different mobile phone manufacturers or versions may vary.

Eg:

Java: code:

Package COM. example. enabling_discoverability_bluetooth; import android. OS. bundle; import android. app. activity; import android. bluetooth. export thadapter; import android. content. intent; import android. view. menu; import android. widget. toast; public class mainactivity extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. acti Vity_main); effecthadapter mblustmthadapter = effecthadapter. getdefaadapter adapter (); If (mblustmthadapter = NULL) {toast. maketext (this, "no Bluetooth hardware or driver is found on the local machine! ", Toast. length_short ). show (); finish () ;}// if the local Bluetooth device is allowed to be detected by other nearby bluetooth devices, enable the bluetooth device // settings so that your mobile phone is detected by other Bluetooth devices // if you need to confirm the operation, you do not need to obtain the underlying Bluetooth service instance, you can use an intent to pass the action_request_discoverable parameter. // you can use startactivityforresult to forcibly obtain a result and override onactivityresult () to obtain the execution result, // The returned results include result_ OK and result_cancelled, respectively, indicating that intent mintent = new intent (cancthadapter. action_request_discoverable); startactivityforresult (mintent, 1) ;}@ overrideprotected void onactivityresult (INT requestcode, int resultcode, intent data) {// todo auto-generated method stubsuper. onactivityresult (requestcode, resultcode, data); If (requestcode = 1) {If (resultcode = result_ OK) {toast. maketext (this, "Allow local Bluetooth to be detected by other nearby bluetooth devices", toast. length_short ). show ();} else if (resultcode = result_canceled) {toast. maketext (this, "cannot be detected by other nearby bluetooth devices", toast. length_short ). show (); finish () ;}}@ overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. main, menu); Return true ;}}

Configuration file:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.enabling_discoverability_bluetooth"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <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="@style/AppTheme" >        <activity            android:name="com.example.enabling_discoverability_bluetooth.MainActivity"            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>

Running result:

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.