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