Bluetooth search for andriod Bluetooth Development

Source: Internet
Author: User

Bluetooth search for andriod Bluetooth Development

Bluetooth search is implemented through broadcast. The key code is as follows:

1. Used to discover broadcast recipients of Bluetooth

/*** Receiver * called when bluetooth device search is complete */private BroadcastReceiver _ foundReceiver = new BroadcastReceiver () {public void onReceive (Context context, Intent intent) {effecthdevice device = intent. getParcelableExtra (effecthdevice. EXTRA_DEVICE); // Add the result to the LIST _ devices. add (device); DeviceInfo info = new DeviceInfo (); info. setmDeviceName (device. getName (); info. setmDeviceMacAddr (device. getAddress (); infos. add (info); info = null; // display the list of showDevices ();}};

Private receiver _ discoveryReceiver = new BroadcastReceiver () {@ Overridepublic void onReceive (Context context, Intent intent) {// uninstall the registered receiver unregisterReceiver (_ foundReceiver); unregisterReceiver (this ); _ discoveryFinished = true ;}};


2. Obtain the Bluetooth adapter

_ Bluetooth = descrithadapter. getdefaadapter adapter (); // determines whether the device supports bluetooth. if (_ bluetooth = null) {Toast. makeText (getApplicationContext (), "the current device does not support Bluetooth", 300 ). show (); return;} // determine if Bluetooth is enabled if (! _ Bluetooth. isEnabled () {// request to enable Bluetooth Intent = new intent (effecthadapter. ACTION_REQUEST_ENABLE); // sets the Bluetooth visibility, up to 300 seconds // intent. putExtra (effecthadapter. EXTRA_DISCOVERABLE_DURATION, 300); startActivity (intent );

3. register the receiver

// Register the receiver IntentFilter discoveryFilter = new IntentFilter (descrithadapter. ACTION_DISCOVERY_FINISHED); registerReceiver (_ discoveryReceiver, discoveryFilter); IntentFilter foundFilter = new IntentFilter (descrithdevice. ACTION_FOUND); registerReceiver (_ foundReceiver, foundFilter); // a dialog box is displayed, searching for the bluetooth device SamplesUtils. indeterminate (DiscoveryActivity. this, _ handler, "Searching for Bluetooth devices... ", _ discoveryWorkder, new OnDismissListener () {public void onDismiss (DialogInterface Diener) {for (; _ bluetooth. isDiscovering ();) {_ bluetooth. cancelDiscovery () ;}_ discoveryFinished = true ;}}, false );


If (list. indexOf (s) =-1) {// prevent repeated list. add (s );


Tool class:

class SamplesUtils{public static void indeterminate(Context context, Handler handler, String message, final Runnable runnable, OnDismissListener dismissListener){try{indeterminateInternal(context, handler, message, runnable, dismissListener, true);}catch (Exception e){; // nop.}}public static void indeterminate(Context context, Handler handler, String message, final Runnable runnable, OnDismissListener dismissListener,boolean cancelable){try{indeterminateInternal(context, handler, message, runnable, dismissListener, cancelable);}catch (Exception e){; // nop.}}private static ProgressDialog createProgressDialog(Context context, String message){ProgressDialog dialog = new ProgressDialog(context);dialog.setIndeterminate(false);dialog.setMessage(message);return dialog;}private static void indeterminateInternal(Context context, final Handler handler, String message, final Runnable runnable,OnDismissListener dismissListener, boolean cancelable){final ProgressDialog dialog = createProgressDialog(context, message);dialog.setCancelable(cancelable);if (dismissListener != null){dialog.setOnDismissListener(dismissListener);}dialog.show();new Thread() {@Overridepublic void run(){runnable.run();handler.post(new Runnable() {public void run(){try{dialog.dismiss();}catch (Exception e){; // nop.}}});};}.start();}}



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.