Android藍牙開發淺析

1. 使用藍牙的響應權限 html

<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

 

2打開藍牙 android

在這裏首先要了解藍牙操做的一個核心類BluetoothAdapter,對藍牙操做首先就須要有一個BluetoothAdapter實例。經常使用的幾個方法以下: 服務器

  •   getDefaultAdapter()——獲取本地的藍牙設備  
  •   enable()——打開藍牙設備
  •   disable()——關閉藍牙設備
  •   startDiscovery()——藍牙設備開始搜索周邊設備
  •       cancelDiscovery()——取消本地藍牙設備的搜索操做,若是本地設備正在進行搜索,那麼調用該方法後將中止搜索操做。
  •   getAddress()——獲取藍牙設備的MAC地址。
  •   getName()——獲取本地藍牙的名稱
  •   getRemoteDevice(String address)——根據遠程設備的MAC地址來獲取遠程設備
複製代碼
 1 // 獲取本地的藍牙適配器實例  2 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();  3 if(adapter!=null)  4  {  5 if(!adapter.isEnabled())  6  {  7 //經過這個方法來請求打開咱們的藍牙設備  8 Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  9  startActivityForResult(intent); 10 11 //不作提示,強行打開 12 //mAdapter.enable(); 13  } 14  } 15 else 16  { 17 System.out.println("本地設備驅動異常!"); 18  } 19
複製代碼

 

3.搜索藍牙設備 app

這裏能夠細分爲幾個方面 
(1)若是要使本機藍牙可以被其餘手機藍牙發現,則經過調用startActivityForResult(Intent, int) 方法,其中,intent帶有ACTION_REQUEST_DISCOVERABLE的請求。 異步

If you would like to make the local device discoverable to other devices, call startActivityForResult(Intent, int) with the ACTION_REQUEST_DISCOVERABLE action Intent.
複製代碼
1 //使本機藍牙在300秒內可被搜索 2 private void ensureDiscoverable() { 3 if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { 4 //打開本機的藍牙發現功能(默認打開120秒,能夠將時間最多延長至300秒)  5 Intent discoveryIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 6 discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);//設置持續時間(最多300秒) 7  startActivity(discoverableIntent); 8  } 9 }
複製代碼

 

(2)查找已經配對的藍牙設備,即之前已經配對過的設備   socket

複製代碼
1 Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 2 if (pairedDevices.size() > 0) { 3  findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE); 4 for (BluetoothDevice device : pairedDevices) { 5 mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 6  } 7 } else { 8 mPairedDevicesArrayAdapter.add("沒有找到已匹對的設備"); 9 }
複製代碼

 

(3)使用BluetoothAdapter的startDiscovery()方法來搜索藍牙設備。 this

複製代碼
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來獲取,咱們能夠本身註冊相應的BroadcastReceiver來接收相應的廣播,以便實現某些功能 spa

複製代碼
 1 // 建立一個接收ACTION_FOUND廣播的BroadcastReceiver   2 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {  3 public void onReceive(Context context, Intent intent) {  4 String action = intent.getAction();  5 // 發現設備   6 if (BluetoothDevice.ACTION_FOUND.equals(action)) {  7 // 從Intent中獲取設備對象   8 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  9 // 將設備名稱和地址放入array adapter,以便在ListView中顯示  10 mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 11  } 12  } 13 }; 14 // 註冊BroadcastReceiver  15 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 16 registerReceiver(mReceiver, filter); // 不要忘了以後解除綁定 
複製代碼

 

 4.配對 線程

配對操做呢,通常都是發現設備後,由咱們人工來進行選擇後系統自動去配對。 code

5. 藍牙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

複製代碼
 1 private class AcceptThread extends Thread {  2 private final BluetoothServerSocket mmServerSocket;  3  4 public AcceptThread() {  5 // Use a temporary object that is later assigned to mmServerSocket,  6 // because mmServerSocket is final   7 BluetoothServerSocket tmp = null;  8 try {  9 // 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; 13  } 14 15 public void run() { 16 BluetoothSocket socket = null; 17 // Keep listening until exception occurs or a socket is returned  18 while (true) { 19 try { 20 socket = mmServerSocket.accept(); 21 } catch (IOException e) { 22 break; 23  } 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(); 29 break; 30  } 31  } 32  } 33 34 /** Will cancel the listening socket, and cause the thread to finish */ 35 public void cancel() { 36 try { 37  mmServerSocket.close(); 38 } catch (IOException e) { } 39  } 40 }
複製代碼

 


客戶端的實現

經過搜索獲得服務器端的BluetoothService

調用BluetoothService的createRfcommSocketToServiceRecord(UUID)方法獲取BluetoothSocket(該UUID應該同於服務器端的UUID)

調用BluetoothSocket的connect()方法(該方法爲block方法),若是UUID同服務器端的UUID匹配,而且鏈接被服務器端accept,則connect()方法返回

注意:在調用connect()方法以前,應當肯定當前沒有搜索設備,不然鏈接會變得很是慢而且容易失敗

 

複製代碼
 1 private class ConnectThread extends Thread {  2 private final BluetoothSocket mmSocket;  3 private final BluetoothDevice mmDevice;  4  5 public ConnectThread(BluetoothDevice device) {  6 // Use a temporary object that is later assigned to mmSocket,  7 // because mmSocket is final   8 BluetoothSocket tmp = null;  9 mmDevice = device; 10 11 // Get a BluetoothSocket to connect with the given BluetoothDevice  12 try { 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; 17  } 18 19 public void run() { 20 // Cancel discovery because it will slow down the connection  21  mBluetoothAdapter.cancelDiscovery(); 22 23 try { 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; 33  } 34 35 // Do work to manage the connection (in a separate thread)  36  manageConnectedSocket(mmSocket); 37  } 38 39 /** Will cancel an in-progress connection, and close the socket */ 40 public void cancel() { 41 try { 42  mmSocket.close(); 43 } catch (IOException e) { } 44  } 45 }
複製代碼

 


鏈接管理(數據通訊)

當你成功地鏈接了兩臺(或多臺)設備時,每一個設備都有一個已鏈接的BluetoothSocket。這時你能夠在設備之間共享數據,樂趣纔剛開始。 使用BluetoothSocket,傳輸二進制數據的過程是簡單的:

  1. 分別經過getInputStream()和getOutputStream()得到管理數據傳輸的InputStream和OutputStream。
  2. 經過read(byte[])和write(byte[])從流中讀取或寫入數據。

 首先,你必須使用一個線程專門用於數據的讀或寫。這是很是重要的,由於read(byte[])和write(byte[])方法都是阻塞調用。read(byte[])將會阻塞到流中有數據可讀。write(byte[])通常不會阻塞,但當遠程設備的中間緩衝區已滿而對方沒有及時地調用read(byte[])時將會一直阻塞。因此,你的線程中的主循環將一直用於從InputStream中讀取數據。

 

複製代碼
 1 private class ConnectedThread extends Thread {  2 private final BluetoothSocket mmSocket;  3 private final InputStream mmInStream;  4 private final OutputStream mmOutStream;  5  6 public ConnectedThread(BluetoothSocket socket) {  7 mmSocket = socket;  8 InputStream tmpIn = null;  9 OutputStream tmpOut = null; 10 11 // Get the input and output streams, using temp objects because 12 // member streams are final  13 try { 14 tmpIn = socket.getInputStream(); 15 tmpOut = socket.getOutputStream(); 16 } catch (IOException e) { } 17 18 mmInStream = tmpIn; 19 mmOutStream = tmpOut; 20  } 21 22 public void run() { 23 byte[] buffer = new byte[1024]; // buffer store for the stream  24 int bytes; // bytes returned from read() 25 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; 36  } 37  } 38  } 39 40 /* Call this from the main Activity to send data to the remote device */ 41 public void write(byte[] bytes) { 42 try { 43  mmOutStream.write(bytes); 44 } catch (IOException e) { } 45  } 46 47 /* Call this from the main Activity to shutdown the connection */ 48 public void cancel() { 49 try { 50  mmSocket.close(); 51 } catch (IOException e) { } 52  } 53 }
複製代碼
相關文章
相關標籤/搜索