Bluetooth operations for Android Development
Bluetooth operation
1. What is Bluetooth?
1. A wireless communication protocol widely used by Bluetooth
2. Mainly for short-range communication (10 m)
3. It is often used in mobile communication devices such as headphones, mouse, and keyboard.
2. Bluetooth-related APIs
1. effecthadapter: represents a local Bluetooth device.
2. effecthdevice: represents a remote Bluetooth device.
3. settings required for Bluetooth
You must declare the Bluetooth operation permission in AndroidManifest.
<Uses-permission android: name = "android. permission. BLUETOOTH"/>
<Uses-permission android: name = "android. permission. effecth_admin"/>
4. Scan the paired bluetooth device
1. Obtain the thadapter object
Descrithadapter adapter = descrithadapter. getdefaadapter adapter ()
2. Determine whether the current device has a bluetooth device.
If (adapter! = Null ){}
3. Determine whether the bluetooth device on the current device is enabled.
Use the adapter. isEnabled () method
4. Get all paired Bluetooth devices
Set <effecthdevice> devices = adapter. getBondedDevices ();
5. Set the visibility of Bluetooth devices
1. Create an intent object:
Intent discoverableIntent = new Intent (effecthadapter. ACTION_REQUEST_DISCOVERABLE );
2. Set the visibility time. The maximum value is 300 s. If the value exceeds 300 s, the system will automatically set it to s.
DiscoverableIntent. putExtra (effecthadapter. EXTRA_DISCOVERABLE_DURATION, 300 );
3. Start the Android built-in activity and pass the Parameter
StartActivity (discoverableIntent );
6. Scan for visible Bluetooth
1. Call startDiscovery () to scan visible devices
Descrithadapter. getdefaadapter adapter (). startDiscovery ();
2. A broadcast is sent every time the system is scanned.
A. Create A filter to filter broadcast ACTION_FOUND.
IntentFilter intentFilter = new IntentFilter (effecthdevice. ACTION_FOUND );
B. Create a broadcast Receiver
Extends threceiver extends threceiver = new extends threceiver ();
C. register the receiver
RegisterReceiver (FIG, intentFilter );
D. retrieve data from broadcast
String action = intent. getAction ();
// Retrieve the remote Bluetooth device from the intent
Effecthdevice device = intent. getParcelableExtra (effecthdevice. EXTRA_DEVICE );
VII. Source Code
MainActivity. java
- Package com. example. bluetooth;
- Import java. util. Iterator;
- Import java. util. Set;
- Import android. OS. Bundle;
- Import android. app. Activity;
- Import android. app. SearchManager. OnCancelListener;
- Import android. bluetooth. javasthadapter;
- Import android. bluetooth. systthdevice;
- Import android. content. BroadcastReceiver;
- Import android. content. Context;
- Import android. content. DialogInterface;
- Import android. content. IntentFilter;
- Import android. content. DialogInterface. OnClickListener;
- Import android. content. Intent;
- Import android. view. Menu;
- Import android. view. View;
- Import android. webkit. WebView. FindListener;
- Import android. widget. Button;
- Import android. widget. EditText;
- Public class MainActivity extends Activity
- {
- Private Button findBtn = null;
- Private Button scanBtn = null;
- Private Button setBtn = null;
- Private EditText edit1 = null;
- @ Override
- Protected void onCreate (Bundle savedInstanceState)
- {
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. activity_main );
- // Find the button
- FindBtn = (Button) findViewById (R. id. findBtn );
- ScanBtn = (Button) findViewById (R. id. scanBtn );
- SetBtn = (Button) findViewById (R. id. setBtn );
- // Set the listener
- FindBtn. setOnClickListener (new btnOnclickListener ());
- ScanBtn. setOnClickListener (new btnOnclickListener ());
- SetBtn. setOnClickListener (new btnOnclickListener ());
- Edit1 = (EditText) findViewById (R. id. edit1 );
- // Create a filter to filter broadcast ACTION_FOUND
- IntentFilter intentFilter = new IntentFilter (effecthdevice. ACTION_FOUND );
- // Create a broadcast Receiver
- Extends threceiver extends threceiver = new extends threceiver ();
- // Register the receiver
- RegisterReceiver (FIG, intentFilter );
- }
- Class btnOnclickListener implements android. view. View. OnClickListener
- {
- @ Override
- Public void onClick (View v)
- {
- // TODO Auto-generated method stub
- Switch (v. getId ())
- {
- Case R. id. findBtn:
- // Obtain the thadapter object
- Descrithadapter adapter = descrithadapter. getdefaadapter adapter ();
- // If the device is obtained
- If (adapter! = Null)
- {
- // Update edittext and print the device
- Edit1.append ("have! "+ '\ N ');
- // If the device is not enabled
- If (! Adapter. isEnabled ())
- {
- // Start a new activity and enable Bluetooth
- Intent intent = new Intent (effecthadapter. ACTION_REQUEST_ENABLE );
- StartActivity (intent );
- }
- // Obtain all paired Bluetooth devices to obtain a bluetooth device set
- Set <effecthdevice> devices = adapter. getBondedDevices ();
- If (devices. size ()> 0)
- {
- // If there is a pair of Bluetooth, print them one by one using Iteration
- For (Iterator iterator = devices. iterator (); iterator. hasNext ();)
- {
- Effecthdevice = (effecthdevice) iterator. next ();
- Edit1.append (effecthdevice. getAddress () + '\ n ');
- }
- }
- }
- Else
- {
- Edit1.append ("no device !! ");
- }
- Break;
- Case R. id. scanBtn:
- // Call startDiscovery () to scan visible devices
- Descrithadapter. getdefaadapter adapter (). startDiscovery ();
- Break;
- Case R. id. setBtn:
- // Create intent and pass parameters through intent
- Intent discoverableIntent = new Intent (effecthadapter. ACTION_REQUEST_DISCOVERABLE );
- // Set the duration of the visible State. If the duration exceeds 300 s, the system will automatically set it to 300 s.
- DiscoverableIntent. putExtra (effecthadapter. EXTRA_DISCOVERABLE_DURATION, 300 );
- // Start the activity, which comes with Android
- StartActivity (discoverableIntent );
- Break;
- Default:
- Break;
- }
- }
- }
- Private class extends threceiver extends BroadcastReceiver
- {
- @ Override
- Public void onReceive (Context context, Intent intent)
- {
- // TODO Auto-generated method stub
- // Accept intent
- String action = intent. getAction ();
- // Retrieve the remote Bluetooth device from the intent
- Effecthdevice device = intent. getParcelableExtra (effecthdevice. EXTRA_DEVICE );
- Edit1.append ("find new device:" + device. getAddress () + "\ n ");
- }
- }
- @ Override
- Public boolean onCreateOptionsMenu (Menu menu)
- {
- // Inflate the menu; this adds items to the action bar if it is present.
- GetMenuInflater (). inflate (R. menu. main, menu );
- Return true;
- }
- }
Layout File
- <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
- Xmlns: tools = "http://schemas.android.com/tools"
- Android: layout_width = "match_parent"
- Android: layout_height = "match_parent"
- Android: paddingBottom = "@ dimen/activity_vertical_margin"
- Android: paddingLeft = "@ dimen/activity_horizontal_margin"
- Android: paddingRight = "@ dimen/activity_horizontal_margin"
- Android: paddingTop = "@ dimen/activity_vertical_margin"
- Tools: context = ". MainActivity">
- <Button
- Android: id = "@ + id/findBtn"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: text = "scanning connected Bluetooth"
- />
- <Button
- Android: id = "@ + id/setBtn"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: layout_below = "@ + id/findBtn"
- Android: text = "setting Bluetooth visibility"
- />
- <Button
- Android: id = "@ + id/scanBtn"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: layout_below = "@ + id/setBtn"
- Android: text = "scanning Bluetooth visible around"
- />
- <EditText
- Android: id = "@ + id/edit1"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: layout_below = "@ + id/scanBtn"
- />
- </RelativeLayout>