歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 藍牙設備的查找和連接

Android 藍牙設備的查找和連接

日期:2017/3/1 10:52:42   编辑:Linux編程

1.權限
使用藍牙設備需要先在Manifest中開放權限,位置如下。

  1. <manifest ...>
  2. <application ...>
  3. ...
  4. </application>
  5. // 使用藍牙設備的權限
  6. <uses-permission Android:name="android.permission.BLUETOOTH" />
  7. // 管理藍牙設備的權限
  8. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  9. </manifest>
2.打開藍牙
獲得藍牙適配器(android.bluetooth.BluetoothAdapter),檢查該設備是否支持藍牙,如果支持,就打開藍牙。
  1. // 檢查設備是否支持藍牙
  2. adapter = BluetoothAdapter.getDefaultAdapter();
  3. if (adapter == null)
  4. {
  5. // 設備不支持藍牙
  6. }
  7. // 打開藍牙
  8. if (!adapter.isEnabled())
  9. {
  10. Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  11. // 設置藍牙可見性,最多300秒
  12. intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
  13. context.startActivity(intent);
  14. }
3.獲取已配對的藍牙設備(android.bluetooth.BluetoothDevice)
首次連接某藍牙設備需要先配對,一旦配對成功,該設備的信息會被保存,以後連接時無需再配對,所以已配對的設備不一定是能連接的。
  1. BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
  2. Set<BluetoothDevice> devices = adapter.getBondedDevices();
  3. for(int i=0; i<devices.size(); i++)
  4. {
  5. BluetoothDevice device = (BluetoothDevice) devices.iterator().next();
  6. System.out.println(device.getName());
  7. }
4.搜索周圍的藍牙設備
適配器搜索藍牙設備後將結果以廣播形式傳出去,所以需要自定義一個繼承廣播的類,在onReceive方法中獲得並處理藍牙設備的搜索結果。
  1. // 設置廣播信息過濾
  2. IntentFilter intentFilter = new IntentFilter();
  3. intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
  4. intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
  5. intentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
  6. intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
  7. // 注冊廣播接收器,接收並處理搜索結果
  8. context.registerReceiver(receiver, intentFilter);
  9. // 尋找藍牙設備,android會將查找到的設備以廣播形式發出去
  10. adapter.startDiscovery();
自定義廣播類
  1. private BroadcastReceiver receiver = new BroadcastReceiver() {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. String action = intent.getAction();
  5. if (BluetoothDevice.ACTION_FOUND.equals(action)) {
  6. BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  7. System.out.println(device.getName());
  8. }
  9. }
  10. }
5.藍牙設備的配對和狀態監視
  1. private BroadcastReceiver receiver = new BroadcastReceiver() {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. String action = intent.getAction();
  5. if (BluetoothDevice.ACTION_FOUND.equals(action)) {
  6. // 獲取查找到的藍牙設備
  7. BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  8. System.out.println(device.getName());
  9. // 如果查找到的設備符合要連接的設備,處理
  10. if (device.getName().equalsIgnoreCase(name)) {
  11. // 搜索藍牙設備的過程占用資源比較多,一旦找到需要連接的設備後需要及時關閉搜索
  12. adapter.cancelDiscovery();
  13. // 獲取藍牙設備的連接狀態
  14. connectState = device.getBondState();
  15. switch (connectState) {
  16. // 未配對
  17. case BluetoothDevice.BOND_NONE:
  18. // 配對
  19. try {
  20. Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
  21. createBondMethod.invoke(device);
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25. break;
  26. // 已配對
  27. case BluetoothDevice.BOND_BONDED:
  28. try {
  29. // 連接
  30. connect(device);
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. break;
  35. }
  36. }
  37. } else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
  38. // 狀態改變的廣播
  39. BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  40. if (device.getName().equalsIgnoreCase(name)) {
  41. connectState = device.getBondState();
  42. switch (connectState) {
  43. case BluetoothDevice.BOND_NONE:
  44. break;
  45. case BluetoothDevice.BOND_BONDING:
  46. break;
  47. case BluetoothDevice.BOND_BONDED:
  48. try {
  49. // 連接
  50. connect(device);
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. break;
  55. }
  56. }
  57. }
  58. }
  59. }
6.藍牙設備的連接
  1. private void connect(BluetoothDevice device) throws IOException {
  2. // 固定的UUID
  3. final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
  4. UUID uuid = UUID.fromString(SPP_UUID);
  5. BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid);
  6. socket.connect();
  7. }
Copyright © Linux教程網 All Rights Reserved