最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 藍(lán)牙開(kāi)發(fā)實(shí)例解析

 更新時(shí)間:2016年08月11日 11:44:38   投稿:lqh  
本文主要介紹Android 藍(lán)牙開(kāi)發(fā),這里提供實(shí)例代碼和詳細(xì)解析實(shí)現(xiàn)方法,對(duì)開(kāi)發(fā)Android藍(lán)牙開(kāi)發(fā)的朋友提供簡(jiǎn)單示例,有需要的朋友可以參考下

在使用手機(jī)時(shí),藍(lán)牙通信給我們帶來(lái)很多方便。那么在Android手機(jī)中怎樣進(jìn)行藍(lán)牙開(kāi)發(fā)呢?本文以實(shí)例的方式講解Android藍(lán)牙開(kāi)發(fā)的知識(shí)。

       1、使用藍(lán)牙的響應(yīng)權(quán)限

XML/HTML代碼

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

        2、配置本機(jī)藍(lán)牙模塊

       在這里首先要了解對(duì)藍(lán)牙操作一個(gè)核心類(lèi)BluetoothAdapter。

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();  
  
//直接打開(kāi)系統(tǒng)的藍(lán)牙設(shè)置面板  
  
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
  
startActivityForResult(intent, 0x1);  
  
//直接打開(kāi)藍(lán)牙  
  
adapter.enable();  
  
//關(guān)閉藍(lán)牙  
  
adapter.disable();  
  
//打開(kāi)本機(jī)的藍(lán)牙發(fā)現(xiàn)功能(默認(rèn)打開(kāi)120秒,可以將時(shí)間最多延長(zhǎng)至300秒)  
  
Intent discoveryIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);  
  
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);//設(shè)置持續(xù)時(shí)間(最多300秒) 

         3、搜索藍(lán)牙設(shè)備

       使用BluetoothAdapter的startDiscovery()方法來(lái)搜索藍(lán)牙設(shè)備。

       startDiscovery()方法是一個(gè)異步方法,調(diào)用后會(huì)立即返回。該方法會(huì)進(jìn)行對(duì)其他藍(lán)牙設(shè)備的搜索,該過(guò)程會(huì)持續(xù)12秒。該方法調(diào)用后,搜索過(guò)程實(shí)際上是在一個(gè)System Service中進(jìn)行的,所以可以調(diào)用cancelDiscovery()方法來(lái)停止搜索(該方法可以在未執(zhí)行discovery請(qǐng)求時(shí)調(diào)用)。

       請(qǐng)求Discovery后,系統(tǒng)開(kāi)始搜索藍(lán)牙設(shè)備,在這個(gè)過(guò)程中,系統(tǒng)會(huì)發(fā)送以下三個(gè)廣播:

       ACTION_DISCOVERY_START:開(kāi)始搜索

       ACTION_DISCOVERY_FINISHED:搜索結(jié)束

       ACTION_FOUND:找到設(shè)備,這個(gè)Intent中包含兩個(gè)extra fields:EXTRA_DEVICE和EXTRA_CLASS,分別包含BluetooDevice和BluetoothClass。

       我們可以自己注冊(cè)相應(yīng)的BroadcastReceiver來(lái)接收響應(yīng)的廣播,以便實(shí)現(xiàn)某些功能。

// 創(chuàng)建一個(gè)接收ACTION_FOUND廣播的BroadcastReceiver  
  
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {  
  
 public void onReceive(Context context, Intent intent) {  
  
  String action = intent.getAction();  
  
  // 發(fā)現(xiàn)設(shè)備  
  
  if (BluetoothDevice.ACTION_FOUND.equals(action)) {  
  
   // 從Intent中獲取設(shè)備對(duì)象  
  
   BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);  
  
   // 將設(shè)備名稱(chēng)和地址放入array adapter,以便在ListView中顯示  
  
   mArrayAdapter.add(device.getName() + "\n" + device.getAddress());  
  }  
 }  
};  
  
// 注冊(cè)BroadcastReceiver  
  
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);  
  
registerReceiver(mReceiver, filter); // 不要忘了之后解除綁定 

        4、藍(lán)牙Socket通信

       如果打算建議兩個(gè)藍(lán)牙設(shè)備之間的連接,則必須實(shí)現(xiàn)服務(wù)器端與客戶(hù)端的機(jī)制。當(dāng)兩個(gè)設(shè)備在同一個(gè)RFCOMM channel下分別擁有一個(gè)連接的BluetoothSocket,這兩個(gè)設(shè)備才可以說(shuō)是建立了連接。

       服務(wù)器設(shè)備與客戶(hù)端設(shè)備獲取BluetoothSocket的途徑是不同的。服務(wù)器設(shè)備是通過(guò)accepted一個(gè)incoming connection來(lái)獲取的,而客戶(hù)端設(shè)備則是通過(guò)打開(kāi)一個(gè)到服務(wù)器的RFCOMM channel來(lái)獲取的。

       服務(wù)器端的實(shí)現(xiàn)

       通過(guò)調(diào)用BluetoothAdapter的listenUsingRfcommWithServiceRecord(String, UUID)方法來(lái)獲取BluetoothServerSocket(UUID用于客戶(hù)端與服務(wù)器端之間的配對(duì))。

       調(diào)用BluetoothServerSocket的accept()方法監(jiān)聽(tīng)連接請(qǐng)求,如果收到請(qǐng)求,則返回一個(gè)BluetoothSocket實(shí)例(此方法為block方法,應(yīng)置于新線程中)。

       如果不想在accept其他的連接,則調(diào)用BluetoothServerSocket的close()方法釋放資源(調(diào)用該方法后,之前獲得的BluetoothSocket實(shí)例并沒(méi)有close。但由于RFCOMM一個(gè)時(shí)刻只允許在一條channel中有一個(gè)連接,則一般在accept一個(gè)連接后,便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) { }  
 }  
} 

        客戶(hù)端的實(shí)現(xiàn)

       通過(guò)搜索得到服務(wù)器端的BluetoothService。

       調(diào)用BluetoothService的listenUsingRfcommWithServiceRecord(String, UUID)方法獲取BluetoothSocket(該UUID應(yīng)該同于服務(wù)器端的UUID)。

       調(diào)用BluetoothSocket的connect()方法(該方法為block方法),如果UUID同服務(wù)器端的UUID匹配,并且連接被服務(wù)器端accept,則connect()方法返回。

       注意:在調(diào)用connect()方法之前,應(yīng)當(dāng)確定當(dāng)前沒(méi)有搜索設(shè)備,否則連接會(huì)變得非常慢并且容易失敗。

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) { }  
  }  
} 

       5、連接管理(數(shù)據(jù)通信)

       分別通過(guò)BluetoothSocket的getInputStream()和getOutputStream()方法獲取InputStream和OutputStream。

       使用read(bytes[])和write(bytes[])方法分別進(jìn)行讀寫(xiě)操作。

       注意:read(bytes[])方法會(huì)一直block,知道從流中讀取到信息,而write(bytes[])方法并不是經(jīng)常的block(比如在另一設(shè)備沒(méi)有及時(shí)read或者中間緩沖區(qū)已滿(mǎn)的情況下,write方法會(huì)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) { }  
  
 }  
  
}  

 以上就Android 藍(lán)牙的開(kāi)發(fā)簡(jiǎn)單示例代碼,后續(xù)繼續(xù)整理相關(guān)資料,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論

德庆县| 交口县| 北海市| 青铜峡市| 南通市| 洛阳市| 屏南县| 新巴尔虎左旗| 崇信县| 买车| 文成县| 准格尔旗| 梁山县| 武夷山市| 定兴县| 湖州市| 万宁市| 嘉义县| 德化县| 郸城县| 岗巴县| 聂拉木县| 灵丘县| 彭泽县| 罗定市| 延川县| 高碑店市| 英超| 临武县| 湟中县| 兴安县| 长泰县| 固阳县| 佳木斯市| 万荣县| 陆丰市| 长沙市| 普陀区| 弥勒县| 太和县| 阿合奇县|