Android 藍芽2.0的使用方法詳解_Android

來源:互聯網
上載者:User

本文為大家分享了Android操作藍芽2.0的使用方法,供大家參考,具體內容如下

1.Android操作藍芽2.0的使用流程
(1)找到裝置uuid
(2)擷取藍芽適配器,使得藍芽處於可發現模式,擷取下位機的socket,並且與上位機建立建立串連,擷取擷取輸入資料流和輸出資料流,兩個流都不為空白時,表示串連成功。否則是串連失敗。
(3).與下位機的socket開始通訊。
(4).通訊結束後,中斷連線(關閉流,關閉socket)

2接下來接直接上代碼:
2.1找到裝置uuid(一般廠商都會給開發人員提供)

複製代碼 代碼如下:
UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

2.2與藍牙裝置建立串連

BluetoothAdapter myBluetoothAdapter = null;//藍芽適配器 BluetoothServerSocket mBThServer = null;//上位機<span style="font-family: Arial, Helvetica, sans-serif;">ServerSocket</span> BluetoothSocket mBTHSocket = null;//下位機的socket InputStream mmInStream = null;//輸入資料流 OutputStream mmOutStream = null;//輸出資料流 <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">    </span>
myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();//擷取適配器 Set<BluetoothDevice> pairedDevices = myBluetoothAdapter     .getBondedDevices();//擷取適配器下的所有藍牙裝置 if (pairedDevices.size() > 0) {   for (Iterator<BluetoothDevice> iterator = pairedDevices       .iterator(); iterator.hasNext();) {     BluetoothDevice device = (BluetoothDevice) iterator         .next();     if (DEVICE_NAME1.equals(device.getName())         || DEVICE_NAME2.equals(device.getName())         || DEVICE_NAME3.equals(device.getName())         || DEVICE_NAME4.equals(device.getName())) {       try {         myBluetoothAdapter.enable();//將適配器設定可用         Intent discoverableIntent = new Intent(             BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);// 使得藍芽處於可發現模式,期間150s         discoverableIntent             .putExtra(                 BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,                 150);         mBTHSocket = device             .createRfcommSocketToServiceRecord(MY_UUID);//擷取下位機的socket          int sdk = Integer.parseInt(Build.VERSION.SDK);         if (sdk >= 10) {           mBTHSocket = device               .createInsecureRfcommSocketToServiceRecord(MY_UUID);         } else {           mBTHSocket = device               .createRfcommSocketToServiceRecord(MY_UUID);         }          mBThServer = myBluetoothAdapter             .listenUsingRfcommWithServiceRecord(                 "myServerSocket", MY_UUID);監聽可用的裝置         mBTHSocket.connect(); // 建立串連         mmInStream = mBTHSocket.getInputStream();// 擷取輸入資料流         mmOutStream = mBTHSocket.getOutputStream();// 擷取輸出資料流        } catch (IOException e) {         ett.setText("裝置串連異常!");       }       if ((mmInStream != null) && (mmInStream != null))// 二者不為空白時,表示串連成功,否則串連失敗       {         ett.setText("裝置串連成功!");       } else {         ett.setText("裝置串連失敗!");       }       break;     }   } } 

2.3開始發送資料,並且讀取資料(位元組數組)                    

 if ((mmInStream == null) || (mmInStream == null)) {   Readflage = -2;// 串連異常   return; } mmOutStream.write(cmd_find);//寫入尋找指令 Thread.sleep(200); int datalen = mmInStream.read(recData);//讀取資料 

 注意:cmd_find和recData都是位元組數組byte[].
以上代碼就一次發送指令和讀取資料的步驟。很簡單吧

2.4中斷連線

if ((mmInStream == null) || (mmInStream == null)) {   return; } //關閉流和socket mmOutStream.close(); mmInStream.close(); mBTHSocket.close(); mBThServer.close(); 

最後總結一下,基本就3大步,第一建立串連,第二發送資料讀取資料,第三步中斷連線。今天就這些了,以後會寫關於藍芽4.0 ble 在Android中的使用,這兩個還是有很多不同的,大家請期待。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.