歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:藍牙測試

Android開發教程:藍牙測試

日期:2017/3/1 11:12:14   编辑:Linux編程

軟件平台:Windows 7 + Eclipse + SDK

設計思路:

配合倒計時定時器實現藍牙打開,可見,掃描三個功能

源代碼:

main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent" android:orientation="vertical">
  5. <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:text="TextView" android:layout_height="wrap_content"></TextView>
  6. <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1">
  7. <Button android:id="@+id/button1" android:text="OFF" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  8. </LinearLayout>
  9. <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2">
  10. <Button android:id="@+id/button2" android:text="開啟可見 " android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  11. <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="設備不可見 "></TextView>
  12. </LinearLayout>
  13. <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout3">
  14. <Button android:id="@+id/button3" android:text="掃描:OFF" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  15. <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止掃描 "></TextView>
  16. </LinearLayout>
  17. <ListView android:id="@+id/listView1" android:layout_height="wrap_content" android:layout_width="match_parent"></ListView>
  18. </LinearLayout>

test_bluetooth.java:

  1. package com.test_bluetooth;
  2. import java.util.Set;
  3. import android.app.Activity;
  4. import android.bluetooth.BluetoothAdapter;
  5. import android.bluetooth.BluetoothDevice;
  6. import android.content.BroadcastReceiver;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.IntentFilter;
  10. import android.os.Bundle;
  11. import android.os.CountDownTimer;
  12. import android.view.View;
  13. import android.widget.ArrayAdapter;
  14. import android.widget.Button;
  15. import android.widget.ListView;
  16. import android.widget.TextView;
  17. public class test_bluetooth extends Activity implements View.OnClickListener
  18. {
  19. private static final int REQUEST_ENABLE_BT = 2;
  20. TextView txt;
  21. TextView txt_see;
  22. TextView txt_scan;
  23. BluetoothAdapter mBluetoothAdapter;
  24. ArrayAdapter<String> mArrayAdapter;
  25. Button btn_switch;
  26. Button btn_see;
  27. Button btn_scan;
  28. ListView list;
  29. CountDownTimer see_timer;
  30. CountDownTimer scan_timer;
  31. /** Called when the activity is first created. */
  32. @Override
  33. public void onCreate(Bundle savedInstanceState)
  34. {
  35. super.onCreate(savedInstanceState);
  36. setContentView(R.layout.main);
  37. txt = (TextView)findViewById(R.id.textView1);
  38. txt_see = (TextView)findViewById(R.id.textView2);
  39. txt_scan = (TextView)findViewById(R.id.textView3);
  40. //綁定XML中的ListView,作為Item的容器
  41. list = (ListView) findViewById(R.id.listView1);
  42. //獲取藍牙適配器
  43. mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  44. mArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
  45. if (mBluetoothAdapter == null)
  46. {
  47. // Device does not support Bluetooth
  48. txt.setText("fail");
  49. //退出程序
  50. test_bluetooth.this.finish();
  51. }
  52. btn_switch = (Button)findViewById(R.id.button1);
  53. btn_switch.setOnClickListener(this);
  54. btn_see = (Button)findViewById(R.id.button2);
  55. btn_see.setOnClickListener(this);
  56. btn_see.setEnabled(false);
  57. btn_scan = (Button)findViewById(R.id.button3);
  58. btn_scan.setOnClickListener(this);
  59. btn_scan.setText("掃描:OFF");
  60. btn_scan.setEnabled(false);
  61. //判斷藍牙是否已經被打開
  62. if (mBluetoothAdapter.isEnabled())
  63. {
  64. //打開
  65. btn_switch.setText("ON");
  66. btn_see.setEnabled(true);
  67. btn_scan.setEnabled(true);
  68. }
  69. see_timer = new CountDownTimer(120000,1000)
  70. {
  71. @Override
  72. public void onTick( long millisUntilFinished)
  73. {
  74. txt_see.setText( "剩余可見時間" + millisUntilFinished / 1000 + "秒");
  75. }
  76. @Override
  77. public void onFinish()
  78. {
  79. //判斷藍牙是否已經被打開
  80. if (mBluetoothAdapter.isEnabled())
  81. {
  82. btn_see.setEnabled(true);
  83. txt_see.setText( "設備不可見");
  84. }
  85. }
  86. };
  87. scan_timer = new CountDownTimer(12000,1000)
  88. {
  89. @Override
  90. public void onTick( long millisUntilFinished)
  91. {
  92. txt_scan.setText( "剩余掃描時間" + millisUntilFinished / 1000 + "秒");
  93. }
  94. @Override
  95. public void onFinish()
  96. {
  97. //判斷藍牙是否已經被打開
  98. if (mBluetoothAdapter.isEnabled())
  99. {
  100. btn_scan.setEnabled(true);
  101. //關閉掃描
  102. mBluetoothAdapter.cancelDiscovery();
  103. btn_scan.setText("掃描:OFF");
  104. txt_scan.setText( "停止掃描");
  105. }
  106. }
  107. };
  108. }
  109. @Override
  110. protected void onDestroy() {
  111. super.onDestroy();
  112. android.os.Process.killProcess(android.os.Process.myPid());
  113. }
  114. @Override
  115. public void onClick(View v)
  116. {
  117. // TODO Auto-generated method stub
  118. switch (v.getId())
  119. {
  120. case R.id.button1:
  121. {
  122. String str = btn_switch.getText().toString();
  123. if (str == "OFF")
  124. {
  125. if (!mBluetoothAdapter.isEnabled())
  126. {
  127. //打開藍牙
  128. Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  129. startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  130. txt.setText("s1");
  131. btn_see.setEnabled(true);
  132. btn_scan.setText("掃描:OFF");
  133. btn_scan.setEnabled(true);
  134. }
  135. }
  136. else
  137. {
  138. //關閉藍牙
  139. mBluetoothAdapter.disable();
  140. btn_switch.setText("OFF");
  141. mArrayAdapter.clear();
  142. list.setAdapter(mArrayAdapter);
  143. btn_see.setEnabled(false);
  144. btn_scan.setEnabled(false);
  145. }
  146. break;
  147. }
  148. case R.id.button2:
  149. {
  150. //開啟可見
  151. Intent enableBtIntent_See = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
  152. startActivityForResult(enableBtIntent_See, 3);
  153. txt.setText("s1");
  154. btn_see.setEnabled(false);
  155. see_timer.start();
  156. break;
  157. }
  158. case R.id.button3:
  159. {
  160. String str = btn_scan.getText().toString();
  161. if (str == "掃描:OFF")
  162. {
  163. txt.setText("s5");
  164. if (mBluetoothAdapter.isEnabled())
  165. {
  166. //開始掃描
  167. mBluetoothAdapter.startDiscovery();
  168. txt.setText("s6");
  169. btn_scan.setText("掃描:ON");
  170. // Create a BroadcastReceiver for ACTION_FOUND
  171. final BroadcastReceiver mReceiver = new BroadcastReceiver()
  172. {
  173. @Override
  174. public void onReceive(Context context, Intent intent)
  175. {
  176. // TODO Auto-generated method stub
  177. String action = intent.getAction();
  178. // When discovery finds a device
  179. mArrayAdapter.clear();
  180. if (BluetoothDevice.ACTION_FOUND.equals(action))
  181. {
  182. // Get the BluetoothDevice object from the Intent
  183. BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  184. // Add the name and address to an array adapter to show in a ListView
  185. mArrayAdapter.add(device.getName() + ":" + device.getAddress());
  186. }
  187. list.setAdapter(mArrayAdapter);
  188. }
  189. };
  190. // Register the BroadcastReceiver
  191. IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
  192. registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
  193. scan_timer.start();
  194. }
  195. }
  196. else
  197. {
  198. //關閉掃描
  199. mBluetoothAdapter.cancelDiscovery();
  200. btn_scan.setText("掃描:OFF");
  201. scan_timer.cancel();
  202. txt_scan.setText( "停止掃描");
  203. }
  204. break;
  205. }
  206. default:
  207. break;
  208. }
  209. }
  210. public void onActivityResult(int requestCode, int resultCode, Intent data)
  211. {
  212. switch (requestCode)
  213. {
  214. case REQUEST_ENABLE_BT:
  215. // When the request to enable Bluetooth returns
  216. if (resultCode == Activity.RESULT_OK)
  217. {
  218. // Bluetooth is now enabled, so set up a chat session
  219. btn_switch.setText("ON");
  220. txt.setText("s4");
  221. //獲取藍牙列表
  222. Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
  223. mArrayAdapter.clear();
  224. // If there are paired devices
  225. if (pairedDevices.size() > 0)
  226. {
  227. //txt.setText("s3");
  228. // Loop through paired devices
  229. for (BluetoothDevice device : pairedDevices)
  230. {
  231. // Add the name and address to an array adapter to show in a ListView
  232. mArrayAdapter.add(device.getName() + ":" + device.getAddress());
  233. }
  234. list.setAdapter(mArrayAdapter);
  235. }
  236. } else
  237. {
  238. finish();
  239. }
  240. }
  241. }
  242. }

效果圖:

Copyright © Linux教程網 All Rights Reserved