因爲近期正在開發一個經過藍牙進行數據傳遞的模塊,在參考了有關資料,並詳細閱讀了Android的官方文檔後,總結了Android中藍牙模塊的使用。 html
【更新】以前承諾的藍牙通信模塊的源碼已經放出,詳情請點擊一下連接 java
http://blog.csdn.net/gd920129/article/details/7552110
android
1. 使用藍牙的響應權限 服務器
- <uses-permission android:name="android.permission.BLUETOOTH" />
- <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
2. 配置本機藍牙模塊 app
在這裏首先要了解對藍牙操做一個核心類BluetoothAdapter 異步
- BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
- //直接打開系統的藍牙設置面板
- Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(intent, 0x1);
- //直接打開藍牙
- adapter.enable();
- //關閉藍牙
- adapter.disable();
- //打開本機的藍牙發現功能(默認打開120秒,能夠將時間最多延長至300秒)
- Intent discoveryIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
- discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);//設置持續時間(最多300秒)
3.搜索藍牙設備 socket
使用BluetoothAdapter的startDiscovery()方法來搜索藍牙設備 this
startDiscovery()方法是一個異步方法,調用後會當即返回。該方法會進行對其餘藍牙設備的搜索,該過程會持續12秒。該方法調用後,搜索過程其實是在一個System Service中進行的,因此能夠調用cancelDiscovery()方法來中止搜索(該方法能夠在未執行discovery請求時調用)。 spa
請求Discovery後,系統開始搜索藍牙設備,在這個過程當中,系統會發送如下三個廣播: .net
ACTION_DISCOVERY_START:開始搜索
ACTION_DISCOVERY_FINISHED:搜索結束
ACTION_FOUND:找到設備,這個Intent中包含兩個extra fields:EXTRA_DEVICE和EXTRA_CLASS,分別包含BluetooDevice和BluetoothClass。
咱們能夠本身註冊相應的BroadcastReceiver來接收響應的廣播,以便實現某些功能
- // 建立一個接收ACTION_FOUND廣播的BroadcastReceiver
- private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- // 發現設備
- if (BluetoothDevice.ACTION_FOUND.equals(action)) {
- // 從Intent中獲取設備對象
- BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
- // 將設備名稱和地址放入array adapter,以便在ListView中顯示
- mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
- }
- }
- };
- // 註冊BroadcastReceiver
- IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
- registerReceiver(mReceiver, filter); // 不要忘了以後解除綁定
4. 藍牙Socket通訊
若是打算建議兩個藍牙設備之間的鏈接,則必須實現服務器端與客戶端的機制。當兩個設備在同一個RFCOMM channel下分別擁有一個鏈接的BluetoothSocket,這兩個設備才能夠說是創建了鏈接。
服務器設備與客戶端設備獲取BluetoothSocket的途徑是不一樣的。服務器設備是經過accepted一個incoming connection來獲取的,而客戶端設備則是經過打開一個到服務器的RFCOMM channel來獲取的。
服務器端的實現
經過調用BluetoothAdapter的listenUsingRfcommWithServiceRecord(String, UUID)方法來獲取BluetoothServerSocket(UUID用於客戶端與服務器端之間的配對)
調用BluetoothServerSocket的accept()方法監聽鏈接請求,若是收到請求,則返回一個BluetoothSocket實例(此方法爲block方法,應置於新線程中)
若是不想在accept其餘的鏈接,則調用BluetoothServerSocket的close()方法釋放資源(調用該方法後,以前得到的BluetoothSocket實例並無close。但因爲RFCOMM一個時刻只容許在一條channel中有一個鏈接,則通常在accept一個鏈接後,便close掉BluetoothServerSocket)
- private class AcceptThread extends Thread {
- private final BluetoothServerSocket mmServerSocket;
-
- public AcceptThread() {
- // Use a temporary object that is later assigned to mmServerSocket,
- // because mmServerSocket is final
- BluetoothServerSocket tmp = null;
- try {
- // MY_UUID is the app's UUID string, also used by the client code
- tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
- } catch (IOException e) { }
- mmServerSocket = tmp;
- }
-
- public void run() {
- BluetoothSocket socket = null;
- // Keep listening until exception occurs or a socket is returned
- while (true) {
- try {
- socket = mmServerSocket.accept();
- } catch (IOException e) {
- break;
- }
- // If a connection was accepted
- if (socket != null) {
- // Do work to manage the connection (in a separate thread)
- manageConnectedSocket(socket);
- mmServerSocket.close();
- break;
- }
- }
- }
-
- /** Will cancel the listening socket, and cause the thread to finish */
- public void cancel() {
- try {
- mmServerSocket.close();
- } catch (IOException e) { }
- }
- }
客戶端的實現
經過搜索獲得服務器端的BluetoothService
調用BluetoothService的listenUsingRfcommWithServiceRecord(String, UUID)方法獲取BluetoothSocket(該UUID應該同於服務器端的UUID)
調用BluetoothSocket的connect()方法(該方法爲block方法),若是UUID同服務器端的UUID匹配,而且鏈接被服務器端accept,則connect()方法返回
注意:在調用connect()方法以前,應當肯定當前沒有搜索設備,不然鏈接會變得很是慢而且容易失敗
- private class ConnectThread extends Thread {
- private final BluetoothSocket mmSocket;
- private final BluetoothDevice mmDevice;
-
- public ConnectThread(BluetoothDevice device) {
- // Use a temporary object that is later assigned to mmSocket,
- // because mmSocket is final
- BluetoothSocket tmp = null;
- mmDevice = device;
-
- // Get a BluetoothSocket to connect with the given BluetoothDevice
- try {
- // MY_UUID is the app's UUID string, also used by the server code
- tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
- } catch (IOException e) { }
- mmSocket = tmp;
- }
-
- public void run() {
- // Cancel discovery because it will slow down the connection
- mBluetoothAdapter.cancelDiscovery();
-
- try {
- // Connect the device through the socket. This will block
- // until it succeeds or throws an exception
- mmSocket.connect();
- } catch (IOException connectException) {
- // Unable to connect; close the socket and get out
- try {
- mmSocket.close();
- } catch (IOException closeException) { }
- return;
- }
-
- // Do work to manage the connection (in a separate thread)
- manageConnectedSocket(mmSocket);
- }
-
- /** Will cancel an in-progress connection, and close the socket */
- public void cancel() {
- try {
- mmSocket.close();
- } catch (IOException e) { }
- }
- }
鏈接管理(數據通訊)
分別經過BluetoothSocket的getInputStream()和getOutputStream()方法獲取InputStream和OutputStream
使用read(bytes[])和write(bytes[])方法分別進行讀寫操做
注意:read(bytes[])方法會一直block,知道從流中讀取到信息,而write(bytes[])方法並非常常的block(好比在另外一設備沒有及時read或者中間緩衝區已滿的狀況下,write方法會block)
- private class ConnectedThread extends Thread {
- private final BluetoothSocket mmSocket;
- private final InputStream mmInStream;
- private final OutputStream mmOutStream;
-
- public ConnectedThread(BluetoothSocket socket) {
- mmSocket = socket;
- InputStream tmpIn = null;
- OutputStream tmpOut = null;
-
- // Get the input and output streams, using temp objects because
- // member streams are final
- try {
- tmpIn = socket.getInputStream();
- tmpOut = socket.getOutputStream();
- } catch (IOException e) { }
-
- mmInStream = tmpIn;
- mmOutStream = tmpOut;
- }
-
- public void run() {
- byte[] buffer = new byte[1024]; // buffer store for the stream
- int bytes; // bytes returned from read()
-
- // Keep listening to the InputStream until an exception occurs
- while (true) {
- try {
- // Read from the InputStream
- bytes = mmInStream.read(buffer);
- // Send the obtained bytes to the UI Activity
- mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
- .sendToTarget();
- } catch (IOException e) {
- break;
- }
- }
- }
-
- /* Call this from the main Activity to send data to the remote device */
- public void write(byte[] bytes) {
- try {
- mmOutStream.write(bytes);
- } catch (IOException e) { }
- }
-
- /* Call this from the main Activity to shutdown the connection */
- public void cancel() {
- try {
- mmSocket.close();
- } catch (IOException e) { }
- }
- }