android 學習歷程之--藍牙

藍牙基礎知識學習筆記一

藍牙API都在android.bluetooth 包中,這裏詳細描述藍牙連接的類和接口 :

BluetoothAdapter
表示本地藍牙適配器(藍牙無線電)。 BluetoothAdapter是所有的藍牙互動的入口點 。用這種方法,你可以發現其他藍牙設備,查詢(配對)設備清單,實例化一個BluetoothDevice使用一個已知的MAC地址,並創建一個 BluetoothServerSocket聽取來自其它設備的通信 。
BluetoothDevice
表示遠程藍牙設備。使用這個請求通過與遠程設備的連接 BluetoothSocket或有關設備等作爲其名稱,地址,類別,和粘接狀態查詢信息 。
BluetoothSocket
表示藍牙接口插座(類似一個TCP 套接字)。這是的連接點,它允許應用程序交換數據與其他藍牙設備通過InputStream和OutputStream。
BluetoothServerSocket
代表一個打開的服務器套接字傳入的請求(類似一個TCP監聽ServerSocket的)。爲了連接兩個Android設備,一個設備必須打開這個類的一個服務器套接字。當遠程藍牙設備請求連接到這個設備, BluetoothServerSocket將返回的連接 BluetoothSocket時,連接被接受 。
BluetoothClass
描述藍牙設備的一般特點和功能。這是一個只讀的屬性,定義設備的主要和次要設備類和它的服務設置。但是,這並不能可靠地描述所有的藍牙配置文件和設備支持的服務,但作爲一個設備類型的提示非常有用。
BluetoothProfile
藍牙配置文件的接口。Bluetooth配置文件 是基於藍牙的設備之間的通信的無線接口規範 。一個例子是免提模式。對於更多的配置文件的討論,請參閱資料工作
BluetoothHeadset
提供手機使用藍牙耳機的支持。這既包括藍牙耳機和免提(V1.5)型材。
BluetoothA2dp
定義有多高品質的音頻,可以從一個設備傳輸到另一個通過藍牙連接。“A2DP”代表高級音頻傳輸模式。
BluetoothProfile.ServiceListener
通知BluetoothProfile IPC的客戶端已連接或斷開服務(即運行一個特定的配置文件,內部服務)接口 。

我們怎麼根據提供的接口和類來實現自己的藍牙應用程序呢?google android 給你個大概的指南:

The Android platform includes support for the Bluetooth network stack, which allows a device to wirelessly exchange data with other Bluetooth devices. The application framework provides access to the Bluetooth functionality through the Android Bluetooth APIs. These APIs let applications wirelessly connect to other Bluetooth devices, enabling point-to-point and multipoint wireless features.

Using the Bluetooth APIs, an Android application can perform the following: (使用藍牙API的步驟)

  • Scan for other Bluetooth devices
  • Query the local Bluetooth adapter for paired Bluetooth devices
  • Establish RFCOMM channels
  • Connect to other devices through service discovery
  • Transfer data to and from other devices
  • Manage multiple connections
根據提示:
一、首先在自己的Android應用程序的AndroidManifest.xml中添加使用藍牙的權限;
<manifest ... >
  <uses-permission android:name="android.permission.BLUETOOTH" />  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
</manifest>
二、創建Bluetoothadapter 本地藍牙設備
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBlBluetoothAdapter == null){
Log.i(TAG, "本機器不支持藍牙...");
}else{
//判斷本機藍牙是否已經開啓
if(!mBlBluetoothAdapter.isEnabled()){
//沒有開啓藍牙,則提示用戶是否開啓藍牙功能
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableBtIntent);
// startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}

//3  查詢配對設備
Set<BluetoothDevice> devices = mBlBluetoothAdapter.getBondedDevices();
if(devices.size() > 0){
//遍歷已連接的遠程藍牙設備
for(Iterator<BluetoothDevice> iterator = devices.iterator(); iterator.hasNext();){
//得到遠程連接設備
BluetoothDevice device = iterator.next();
Log.i(TAG, "遠程設備的mac地址:"+device.getAddress()+" 名稱"+device.getName()+" 狀態"+device.getBondState());
}
}

//4  註冊// Register the BroadcastReceiver 用於發現遠程藍牙設備
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);

//5  設置可被發現Enabling discoverability  
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//有效持續時間300秒,默認是120秒,最高限制300s。儘管你設置持續時間大於300s,它也只能持續300s
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);


}
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章