Bluetooth API for Android development and learning

Source: Internet
Author: User

1. Enable the bluetooth device explicitly

Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startActivityForResult(intent,1);
2. enable/disable the bluetooth device implicitly

Descrithadapter mAdapter = descrithadapter. getdefaadapter adapter (); // enable the bluetooth device mAdapter. enable (); // disable the bluetooth device mAdapter. disable ();
3. Obtain the paired bluetooth device

Set
 
  
MDevices = mAdapter. getBondedDevices (); // obtain the paired device if (mDevices. size ()> 0) {for (effecthdevice mDevice: mDevices) {mView. append (mDevice. getName () + ":" + mDevice. getAddress () + "\ n ");}}
 
4. Search for Bluetooth devices

Private final BroadcastReceiver receiver ER = new BroadcastReceiver () {@ Override public void onReceive (Context mContext, Intent intent) {if (incluthdevice. ACTION_FOUND.equals (intent. getAction () {descrithdevice mDevice = intent. getParcelableExtra (effecthdevice. EXTRA_DEVICE); if (mDevice. getBondState ()! = Effecthdevice. BOND_BONDED) {mView. append (mDevice. getName () + ":" + mDevice. getAddress () + "\ n") ;}} else if (descrithadapter. ACTION_DISCOVERY_FINISHED.equals (intent. getAction () {MainActivity. this. setProgressBarIndeterminateVisibility (false) ;}}; // The Bluetooth action listener broadcasts IntentFilter mFilter = new IntentFilter (descrithdevice. ACTION_FOUND); this. registerReceiver (receiver er, mFilter); // Bluetooth action listener ends broadcasting mFilter = new IntentFilter (receiver thadapter. ACTION_DISCOVERY_FINISHED); this. registerReceiver (receiver, mFilter );


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.