生活随笔
收集整理的這篇文章主要介紹了
Android 蓝牙开发浅析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
Android Bluetooth API 翻譯http://wenku.baidu.com/view/518414b069dc5022aaea007b.html?from=rec&pos=2&weight=167&lastweight=111&count=5
?
由于近期正在開發一個通過藍牙進行數據傳遞的模塊,在參考了有關資料,并詳細閱讀了Android的官方文檔后,總結了Android中藍牙模塊的使用。
?
1. 使用藍牙的響應權限
查看源碼 打印?
| 1 | <STRONG>??? <uses-permission android:name="android.permission.BLUETOOTH" />?? |
| 2 | ????<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />? </STRONG> |
?
?
2. 配置本機藍牙模塊
在這里首先要了解對藍牙操作一個核心類BluetoothAdapter
查看源碼 打印?
| 01 | BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();?? |
| 03 | Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);?? |
| 04 | startActivityForResult(intent, 0x1);?? |
| 09 | //打開本機的藍牙發現功能(默認打開120秒,可以將時間最多延長至300秒)?? |
| 10 | discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);//設置持續時間(最多300秒)Intent discoveryIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); |
?
3.搜索藍牙設備
使用BluetoothAdapter的startDiscovery()方法來搜索藍牙設備
startDiscovery()方法是一個異步方法,調用后會立即返回。該方法會進行對其他藍牙設備的搜索,該過程會持續12秒。該方法調用后,搜索過程實際上是在一個System Service中進行的,所以可以調用cancelDiscovery()方法來停止搜索(該方法可以在未執行discovery請求時調用)。
請求Discovery后,系統開始搜索藍牙設備,在這個過程中,系統會發送以下三個廣播:
ACTION_DISCOVERY_START:開始搜索
ACTION_DISCOVERY_FINISHED:搜索結束
ACTION_FOUND:找到設備,這個Intent中包含兩個extra fields:EXTRA_DEVICE和EXTRA_CLASS,分別包含BluetooDevice和BluetoothClass。
我們可以自己注冊相應的BroadcastReceiver來接收響應的廣播,以便實現某些功能
查看源碼 打印?
| 01 | // 創建一個接收ACTION_FOUND廣播的BroadcastReceiver?? |
| 02 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() {?? |
| 03 | ????public void onReceive(Context context, Intent intent) {?? |
| 04 | ????????String action = intent.getAction();?? |
| 06 | ????????if (BluetoothDevice.ACTION_FOUND.equals(action)) {?? |
| 07 | ????????????// 從Intent中獲取設備對象?? |
| 08 | ????????????BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);?? |
| 09 | ????????????// 將設備名稱和地址放入array adapter,以便在ListView中顯示?? |
| 10 | ????????????mArrayAdapter.add(device.getName() + "\n" + device.getAddress());?? |
| 14 | // 注冊BroadcastReceiver?? |
| 15 | IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);?? |
| 16 | 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)
查看源碼 打印?
| 01 | private class AcceptThread extends Thread { |
| 02 | ????private final BluetoothServerSocket mmServerSocket; |
| 04 | ????public AcceptThread() { |
| 05 | ????????// Use a temporary object that is later assigned to mmServerSocket, |
| 06 | ????????// because mmServerSocket is final |
| 07 | ????????BluetoothServerSocket tmp = null; |
| 09 | ????????????// MY_UUID is the app's UUID string, also used by the client code |
| 10 | ????????????tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID); |
| 11 | ????????} catch (IOException e) { } |
| 12 | ????????mmServerSocket = tmp; |
| 15 | ????public void run() { |
| 16 | ????????BluetoothSocket socket = null; |
| 17 | ????????// Keep listening until exception occurs or a socket is returned |
| 20 | ????????????????socket = mmServerSocket.accept(); |
| 21 | ????????????} catch (IOException e) { |
| 24 | ????????????// If a connection was accepted |
| 25 | ????????????if (socket != null) { |
| 26 | ????????????????// Do work to manage the connection (in a separate thread) |
| 27 | ????????????????manageConnectedSocket(socket); |
| 28 | ????????????????mmServerSocket.close(); |
| 34 | ????/** Will cancel the listening socket, and cause the thread to finish */ |
| 35 | ????public void cancel() { |
| 37 | ????????????mmServerSocket.close(); |
| 38 | ????????} catch (IOException e) { } |
客戶端的實現 ?
通過搜索得到服務器端的BluetoothService
調用BluetoothService的listenUsingRfcommWithServiceRecord(String, UUID)方法獲取BluetoothSocket(該UUID應該同于服務器端的UUID)
調用BluetoothSocket的connect()方法(該方法為block方法),如果UUID同服務器端的UUID匹配,并且連接被服務器端accept,則connect()方法返回
注意:在調用connect()方法之前,應當確定當前沒有搜索設備,否則連接會變得非常慢并且容易失敗
查看源碼 打印?
| 01 | <STRONG>??? private class ConnectThread extends Thread {?? |
| 02 | ????????private final BluetoothSocket mmSocket;?? |
| 03 | ????????private final BluetoothDevice mmDevice;?? |
| 05 | ????????public ConnectThread(BluetoothDevice device) {?? |
| 06 | ????????????// Use a temporary object that is later assigned to mmSocket,?? |
| 07 | ????????????// because mmSocket is final?? |
| 08 | ????????????BluetoothSocket tmp = null;?? |
| 09 | ????????????mmDevice = device;?? |
| 11 | ????????????// Get a BluetoothSocket to connect with the given BluetoothDevice?? |
| 13 | ????????????????// MY_UUID is the app's UUID string, also used by the server code?? |
| 14 | ????????????????tmp = device.createRfcommSocketToServiceRecord(MY_UUID);?? |
| 15 | ????????????} catch (IOException e) { }?? |
| 16 | ????????????mmSocket = tmp;?? |
| 19 | ????????public void run() {?? |
| 20 | ????????????// Cancel discovery because it will slow down the connection?? |
| 21 | ????????????mBluetoothAdapter.cancelDiscovery();?? |
| 24 | ????????????????// Connect the device through the socket. This will block?? |
| 25 | ????????????????// until it succeeds or throws an exception?? |
| 26 | ????????????????mmSocket.connect();?? |
| 27 | ????????????} catch (IOException connectException) {?? |
| 28 | ????????????????// Unable to connect; close the socket and get out?? |
| 29 | ????????????????try {?? |
| 30 | ????????????????????mmSocket.close();?? |
| 31 | ????????????????} catch (IOException closeException) { }?? |
| 32 | ????????????????return;?? |
| 35 | ????????????// Do work to manage the connection (in a separate thread)?? |
| 36 | ????????????manageConnectedSocket(mmSocket);?? |
| 39 | ????????/** Will cancel an in-progress connection, and close the socket */?? |
| 40 | ????????public void cancel() {?? |
| 42 | ????????????????mmSocket.close();?? |
| 43 | ????????????} catch (IOException e) { }?? |
連接管理(數據通信) ?
分別通過BluetoothSocket的getInputStream()和getOutputStream()方法獲取InputStream和OutputStream
使用read(bytes[])和write(bytes[])方法分別進行讀寫操作
注意:read(bytes[])方法會一直block,知道從流中讀取到信息,而write(bytes[])方法并不是經常的block(比如在另一設備沒有及時read或者中間緩沖區已滿的情況下,write方法會block)
查看源碼 打印?
| 01 | <STRONG>??? private class ConnectedThread extends Thread {?? |
| 02 | ????????private final BluetoothSocket mmSocket;?? |
| 03 | ????????private final InputStream mmInStream;?? |
| 04 | ????????private final OutputStream mmOutStream;?? |
| 06 | ????????public ConnectedThread(BluetoothSocket socket) {?? |
| 07 | ????????????mmSocket = socket;?? |
| 08 | ????????????InputStream tmpIn = null;?? |
| 09 | ????????????OutputStream tmpOut = null;?? |
| 11 | ????????????// Get the input and output streams, using temp objects because?? |
| 12 | ????????????// member streams are final?? |
| 14 | ????????????????tmpIn = socket.getInputStream();?? |
| 15 | ????????????????tmpOut = socket.getOutputStream();?? |
| 16 | ????????????} catch (IOException e) { }?? |
| 18 | ????????????mmInStream = tmpIn;?? |
| 19 | ????????????mmOutStream = tmpOut;?? |
| 22 | ????????public void run() {?? |
| 23 | ????????????byte[] buffer = new byte[1024];? // buffer store for the stream?? |
| 24 | ????????????int bytes; // bytes returned from read()?? |
| 26 | ????????????// Keep listening to the InputStream until an exception occurs?? |
| 27 | ????????????while (true) {?? |
| 28 | ????????????????try {?? |
| 29 | ????????????????????// Read from the InputStream?? |
| 30 | ????????????????????bytes = mmInStream.read(buffer);?? |
| 31 | ????????????????????// Send the obtained bytes to the UI Activity?? |
| 32 | ????????????????????mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)?? |
| 33 | ????????????????????????????.sendToTarget();?? |
| 34 | ????????????????} catch (IOException e) {?? |
| 35 | ????????????????????break;?? |
| 40 | ????????/* Call this from the main Activity to send data to the remote device */?? |
| 41 | ????????public void write(byte[] bytes) {?? |
| 43 | ????????????????mmOutStream.write(bytes);?? |
| 44 | ????????????} catch (IOException e) { }?? |
| 47 | ????????/* Call this from the main Activity to shutdown the connection */?? |
| 48 | ????????public void cancel() {?? |
| 50 | ????????????????mmSocket.close();?? |
| 51 | ????????????} catch (IOException e) { }?? |
?
?
轉自:http://blog.csdn.net/gd920129/article/details/7487761
?
總結
以上是生活随笔為你收集整理的Android 蓝牙开发浅析的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。