Android開發之藍芽(Bluetooth)操作(一)--掃描已經配對的藍牙裝置

來源:互聯網
上載者:User

標籤:tde   android   called   請求   通訊協定   開發   設定   str   com   

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

一. 什麼是藍芽(Bluetooth)?

 

1.1  BuleTooth是目前使用最廣泛的無線通訊協議

 

1.2  主要針對短距離裝置通訊(10m)

 

1.3  常用於串連耳機,滑鼠和移動通訊裝置等.

 

二. 與藍芽相關的API

 

2.1 BluetoothAdapter:

代表了本地的藍芽適配器

 

2.2 BluetoothDevice

代表了一個遠端Bluetooth裝置

 

三. 掃描已經配對的藍牙裝置(1)

 

註:必須部署在真實手機上,模擬器無法實現

 

首先需要在AndroidManifest.xml 聲明藍芽許可權

 

<user-permission Android:name="android.permission.BLUETOOTH" />

 

配對藍芽需要手動操作:

 

1. 開啟設定--> 無線網路 --> 藍芽 勾選開啟

 

2. 開啟藍芽設定  掃描周圍已經開啟的藍牙裝置(可以與自己的膝上型電腦進行配對),點擊進行配對

 

 

 電腦上會彈出提示視窗: 添加裝置

 

 顯示計算與裝置之間的配對碼,要求確認是否配對

 

 手機上也會顯示類似的提示. 

 

四. 掃描已經配對的藍牙裝置(2)

 

4.1 獲得BluetoothAdapter對象

4.2 判斷當前行動裝置中是否擁有藍芽

4.3 判斷當前行動裝置中藍芽是否已經開啟

4.4 得到所有已經配對的藍牙裝置對象

 

 

實現代碼如下:

MainActivity:

 

[java] view plain copy 
  1. import java.util.Iterator;  
  2. import java.util.Set;  
  3.   
  4. import android.app.Activity;  
  5. import android.bluetooth.BluetoothAdapter;  
  6. import android.bluetooth.BluetoothDevice;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     private Button button = null;  
  15.     /** Called when the activity is first created. */  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.           
  21.         button = (Button)findViewById(R.id.buttonId);  
  22.         button.setOnClickListener(new OnClickListener(){  
  23.   
  24.             @Override  
  25.             public void onClick(View v) {  
  26.                 //獲得BluetoothAdapter對象,該API是android 2.0開始支援的  
  27.                 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();  
  28.                 //adapter不等於null,說明本機有藍牙裝置  
  29.                 if(adapter != null){  
  30.                     System.out.println("本機有藍牙裝置!");  
  31.                     //如果藍牙裝置未開啟  
  32.                     if(!adapter.isEnabled()){  
  33.                         Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
  34.                         //請求開啟藍牙裝置  
  35.                         startActivity(intent);  
  36.                     }  
  37.                     //獲得已配對的遠程藍牙裝置的集合  
  38.                     Set<BluetoothDevice> devices = adapter.getBondedDevices();  
  39.                     if(devices.size()>0){  
  40.                         for(Iterator<BluetoothDevice> it = devices.iterator();it.hasNext();){  
  41.                             BluetoothDevice device = (BluetoothDevice)it.next();  
  42.                             //列印出遠程藍牙裝置的物理地址  
  43.                             System.out.println(device.getAddress());  
  44.                         }  
  45.                     }else{  
  46.                         System.out.println("還沒有已配對的遠程藍牙裝置!");  
  47.                     }  
  48.                 }else{  
  49.                     System.out.println("本機沒有藍牙裝置!");  
  50.                 }  
  51.             }  
  52.         });  
  53.     }  
  54. }  

Android開發之藍芽(Bluetooth)操作(一)--掃描已經配對的藍牙裝置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.