Android藍芽開發流程

2021-07-09 03:15:15 字數 2022 閱讀 7898

首先,要操作藍芽,先要在androidmanifest.xml裡加入許可權

然後,看下api,android所有關於藍芽開發的類都在android.bluetooth包下,只有八個類:

bluetoothadapter,bluetoothclass,bluetoothclass.device,bluetoothclass.device.major,bluetoothclass.service,bluetoothdevice,bluetoothserversocket,bluetoothsocket

而我們需要用到的就只有幾個而已:

1.bluetoothadapter

顧名思義,藍芽介面卡,直到我們建立bluetoothsocket連線之前,都要不斷操作它bluetoothadapter裡的方法很多,常用的有以下幾個:

disable() 關閉藍芽

enable() 開啟藍芽,這個方法開啟藍芽不會彈出提示,更多的時候我們需要問下使用者是否開啟,以下這兩行**同樣是開啟藍芽,不過會提示使用者:

intemtenabler = new intent(bluetoothadapter.action_request_enable);

startactivityforresult(enabler,recode);同startactivity(enabler);

getaddress()獲取本地藍芽位址

getdefaultadapter() 獲取預設bluetoothadapter,實際上,也只有這一種方法獲取bluetoothadapter

getname() 獲取本地藍芽名稱

getremotedevice(string address)根據藍芽位址獲取遠端藍芽裝置

getstate() 獲取本地藍芽介面卡當前狀態

isdiscovering()判斷當前是否正在查詢裝置,是返回true

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

listenusingrfcommwithservicerecord(string name,uuid uuid)根據名稱,uuid建立並返回bluetoothserversocket,這是建立bluetoothsocket伺服器端的第一步

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

2.bluetoothdevice

這個類描述了乙個藍芽裝置

createrfcommsockettoservicerecord(uuiduuid)根據uuid建立並返回乙個bluetoothsocket getstate()

藍芽狀態這裡要說一下,只有在bluetoothadapter.state_on狀態下才可以監聽,具體可以看android api;

這個方法也是我們獲取bluetoothdevice的目的--建立bluetoothsocket

這個類其他的方法,如getaddress(),getname(),同bluetoothadapter

3.bluetoothserversocket

既然是socket,方法就應該都差不多,這個類一種只有三個方法,兩個過載的accept(),accept(int timeout)兩者的區別在於後面的方法指定了過時時間,需要注意的是,執行這兩個方法的時候,直接接收到客戶端的請求,都會阻塞執行緒,應該放在新執行緒裡執行。

還有一點需要注意的是,這兩個方法都返回乙個bluetoothsocket,最後的連線也是伺服器端與客戶端的兩個bluetoothsocket的連線

4.bluetoothsocket

跟bluetoothserversocket相對,是客戶端一共5個方法,不出意外,都會用到

close(),關閉

connect(),連線

getinputstream(),獲取輸入流

getoutputstream(),獲取輸出流

getremotedevice()獲取遠端裝置,這裡指的是獲取bluetoothsocket指定連線的那個遠端藍芽裝置

獲取本地藍芽介面卡

開啟藍芽

搜尋裝置

建立連線

資料傳遞,無非是通過流的形式

Android藍芽開發

藍芽開發,也就那麼幾個操作,開啟 搜尋 配對 連線 通訊。其他的東西都是建立在這麼幾個之上。下面就來說說這幾個基本操作。使用前,首先加入藍芽的兩個基本許可權。然後他們基本圍繞這麼幾個類來用。這些類是什麼,慢慢來說。bluetoothadapter bluetoothadapter bluetooth...

外接藍芽開發流程

2 開發流程 1 config.ini放入 device mediatek mt8127 3rd goc config.ini 2 把鈴聲檔案,放入 device mediatek mt8127 3rd goc ring.2 把gocsdk放入 device mediatek mt8127 3rd ...

外接藍芽開發流程

2 開發流程 1 config.ini放入 device mediatek mt8127 3rd goc config.ini 2 把鈴聲檔案,放入 device mediatek mt8127 3rd goc ring.2 把gocsdk放入 device mediatek mt8127 3rd ...