Android藍芽開發(二):相關的API簡介

來源:互聯網
上載者:User

標籤:android   style   io   ar   color   os   sp   for   on   

一、BluetoothAdapter--藍芽適配器(本機)

 BluetoothAdapter裡的方法很多,常用的有以下幾個:

(1)cancelDiscovery() 根據字面意思,是取消發現,也就是說當我們正在搜尋裝置的時候調用這個方法將不再繼續搜尋

(2)disable()關閉藍芽

(3)enable()開啟藍芽,這個方法開啟藍芽不會彈出提示,

更多的時候我們需要問下使用者是否開啟,以下這兩行代碼同樣是開啟藍芽,不過會提示使用者:

 Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
 startActivityForResult(enableIntent, REQUEST_ENABLE_BT);//也可以寫成startActivity(enableIntent);

(4)getAddress()擷取本地藍芽地址

(5)getDefaultAdapter()擷取預設BluetoothAdapter,實際上,也只有這一種方法擷取BluetoothAdapter

(6)getName()擷取本地藍芽名稱

(7)getRemoteDevice(String address)根據藍芽地址擷取遠程藍牙裝置

(8)getState()擷取本地藍芽適配器目前狀態(感覺可能調試的時候更需要)

(9)isDiscovering()判斷當前是否正在尋找裝置,是返回true

(10)isEnabled()判斷藍芽是否開啟,已開啟返回true,否則,返回false

(11)listenUsingRfcommWithServiceRecord(String name,UUID uuid)根據名稱,UUID建立並返回BluetoothServerSocket,這是建立BluetoothSocket伺服器端的第一步

(12)startDiscovery()開始搜尋,這是搜尋的第一步

 

二、BluetoothDevice--藍牙裝置(遠程)

      createRfcommSocketToServiceRecord(UUIDuuid)根據UUID建立並返回一個BluetoothSocket

這個方法也是我們擷取BluetoothDevice的目的——建立BluetoothSocket

這個類其他的方法,如getAddress(),getName(),同BluetoothAdapter

 

三、BluetoothSocket--藍芽socket介面

 

四、BluetoothServerSocket--表示一個開放的伺服器socket,監聽進入的串連請求

 

五、bluetooth包下還有4個類,BluetoothClass 、BluetoothClass.Device、BluetoothClass.Device.Major、BluetoothClass.Service

Android藍芽開發(二):相關的API簡介

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.