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();}}