Bluetooth operations for Android Development

Source: Internet
Author: User

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

 
 
  1. Package com. example. bluetooth;

  2. Import java. util. Iterator;
  3. Import java. util. Set;

  4. Import android. OS. Bundle;
  5. Import android. app. Activity;
  6. Import android. app. SearchManager. OnCancelListener;
  7. Import android. bluetooth. javasthadapter;
  8. Import android. bluetooth. systthdevice;
  9. Import android. content. BroadcastReceiver;
  10. Import android. content. Context;
  11. Import android. content. DialogInterface;
  12. Import android. content. IntentFilter;
  13. Import android. content. DialogInterface. OnClickListener;
  14. Import android. content. Intent;
  15. Import android. view. Menu;
  16. Import android. view. View;
  17. Import android. webkit. WebView. FindListener;
  18. Import android. widget. Button;
  19. Import android. widget. EditText;

  20. Public class MainActivity extends Activity
  21. {

  22. Private Button findBtn = null;
  23. Private Button scanBtn = null;
  24. Private Button setBtn = null;
  25. Private EditText edit1 = null;

  26. @ Override
  27. Protected void onCreate (Bundle savedInstanceState)
  28. {
  29. Super. onCreate (savedInstanceState );
  30. SetContentView (R. layout. activity_main );
  31. // Find the button
  32. FindBtn = (Button) findViewById (R. id. findBtn );
  33. ScanBtn = (Button) findViewById (R. id. scanBtn );
  34. SetBtn = (Button) findViewById (R. id. setBtn );
  35. // Set the listener
  36. FindBtn. setOnClickListener (new btnOnclickListener ());
  37. ScanBtn. setOnClickListener (new btnOnclickListener ());
  38. SetBtn. setOnClickListener (new btnOnclickListener ());

  39. Edit1 = (EditText) findViewById (R. id. edit1 );
  40. // Create a filter to filter broadcast ACTION_FOUND
  41. IntentFilter intentFilter = new IntentFilter (effecthdevice. ACTION_FOUND );
  42. // Create a broadcast Receiver
  43. Extends threceiver extends threceiver = new extends threceiver ();
  44. // Register the receiver
  45. RegisterReceiver (FIG, intentFilter );
  46. }

  47. Class btnOnclickListener implements android. view. View. OnClickListener
  48. {

  49. @ Override
  50. Public void onClick (View v)
  51. {
  52. // TODO Auto-generated method stub
  53. Switch (v. getId ())
  54. {
  55. Case R. id. findBtn:
  56. // Obtain the thadapter object
  57. Descrithadapter adapter = descrithadapter. getdefaadapter adapter ();
  58. // If the device is obtained
  59. If (adapter! = Null)
  60. {
  61. // Update edittext and print the device
  62. Edit1.append ("have! "+ '\ N ');
  63. // If the device is not enabled
  64. If (! Adapter. isEnabled ())
  65. {
  66. // Start a new activity and enable Bluetooth
  67. Intent intent = new Intent (effecthadapter. ACTION_REQUEST_ENABLE );
  68. StartActivity (intent );
  69. }
  70. // Obtain all paired Bluetooth devices to obtain a bluetooth device set
  71. Set <effecthdevice> devices = adapter. getBondedDevices ();
  72. If (devices. size ()> 0)
  73. {
  74. // If there is a pair of Bluetooth, print them one by one using Iteration
  75. For (Iterator iterator = devices. iterator (); iterator. hasNext ();)
  76. {
  77. Effecthdevice = (effecthdevice) iterator. next ();
  78. Edit1.append (effecthdevice. getAddress () + '\ n ');
  79. }
  80. }
  81. }
  82. Else
  83. {
  84. Edit1.append ("no device !! ");
  85. }
  86. Break;
  87. Case R. id. scanBtn:
  88. // Call startDiscovery () to scan visible devices
  89. Descrithadapter. getdefaadapter adapter (). startDiscovery ();
  90. Break;
  91. Case R. id. setBtn:
  92. // Create intent and pass parameters through intent
  93. Intent discoverableIntent = new Intent (effecthadapter. ACTION_REQUEST_DISCOVERABLE );
  94. // Set the duration of the visible State. If the duration exceeds 300 s, the system will automatically set it to 300 s.
  95. DiscoverableIntent. putExtra (effecthadapter. EXTRA_DISCOVERABLE_DURATION, 300 );
  96. // Start the activity, which comes with Android
  97. StartActivity (discoverableIntent );
  98. Break;
  99. Default:
  100. Break;
  101. }

  102. }
  103. }

  104. Private class extends threceiver extends BroadcastReceiver
  105. {

  106. @ Override
  107. Public void onReceive (Context context, Intent intent)
  108. {
  109. // TODO Auto-generated method stub
  110. // Accept intent
  111. String action = intent. getAction ();
  112. // Retrieve the remote Bluetooth device from the intent
  113. Effecthdevice device = intent. getParcelableExtra (effecthdevice. EXTRA_DEVICE );
  114. Edit1.append ("find new device:" + device. getAddress () + "\ n ");
  115. }

  116. }
  117. @ Override
  118. Public boolean onCreateOptionsMenu (Menu menu)
  119. {
  120. // Inflate the menu; this adds items to the action bar if it is present.
  121. GetMenuInflater (). inflate (R. menu. main, menu );
  122. Return true;
  123. }

  124. }
Layout File
 
 
  1. <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
  2. Xmlns: tools = "http://schemas.android.com/tools"
  3. Android: layout_width = "match_parent"
  4. Android: layout_height = "match_parent"
  5. Android: paddingBottom = "@ dimen/activity_vertical_margin"
  6. Android: paddingLeft = "@ dimen/activity_horizontal_margin"
  7. Android: paddingRight = "@ dimen/activity_horizontal_margin"
  8. Android: paddingTop = "@ dimen/activity_vertical_margin"
  9. Tools: context = ". MainActivity">

  10. <Button
  11. Android: id = "@ + id/findBtn"
  12. Android: layout_width = "fill_parent"
  13. Android: layout_height = "wrap_content"
  14. Android: text = "scanning connected Bluetooth"
  15. />
  16. <Button
  17. Android: id = "@ + id/setBtn"
  18. Android: layout_width = "fill_parent"
  19. Android: layout_height = "wrap_content"
  20. Android: layout_below = "@ + id/findBtn"
  21. Android: text = "setting Bluetooth visibility"
  22. />
  23. <Button
  24. Android: id = "@ + id/scanBtn"
  25. Android: layout_width = "fill_parent"
  26. Android: layout_height = "wrap_content"
  27. Android: layout_below = "@ + id/setBtn"
  28. Android: text = "scanning Bluetooth visible around"
  29. />
  30. <EditText
  31. Android: id = "@ + id/edit1"
  32. Android: layout_width = "fill_parent"
  33. Android: layout_height = "wrap_content"
  34. Android: layout_below = "@ + id/scanBtn"
  35. />

  36. </RelativeLayout>


Related Article

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.