Android藍芽開發SPP協議通訊

2021-07-11 07:42:59 字數 1450 閱讀 6714

最近專案中使用到藍芽spp協議通訊,然後在網上看了很多資料,進行學習使用,為了加深印象,做個簡單的整理。

1、使用藍芽許可權

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

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

2、藍芽核心類bluetoothadapter

代表本地的藍芽介面卡裝置。bluetoothadapter類讓使用者能執行基本的藍芽任務。例如:

為了得到這個代表本地藍芽介面卡的 bluetoothadapter類,呼叫getdefaultadapter()這一靜態方法。這是所有藍芽動作使用的第一步。當擁有本地介面卡以後,

使用者可以獲得一系列的bluetoothdevice物件,這些物件代表所有擁有getbondeddevice()方法的已經匹配的裝置;用 startdiscovery()方法來開始裝置的搜尋;或者建立乙個bluetoothserversocket類,通過 listenusingrfcommwithservicerecord(string, uuid)方法來監聽新來的連線請求。

開啟系統的藍芽設定面板

intent intent = new intent(bluetoothadapter.action_request_enable);

startactivity(intent);

開啟藍芽

adapter.enable();

關閉藍芽

adapter.disable();

設定對別的裝置可見

intent discoveryintent = new intent(bluetoothadapter.action_request_discoverable); 

discoverableintent.putextra(bluetoothadapter.extra_discoverable_duration, 300);//設定持續時間(最多300秒)

搜尋藍芽

adapter.startdiacovery(),當在**中呼叫了該方法時,系統會傳送廣播action_found:找到裝置,我們可以在**中註冊乙個廣播,用來接受系統傳送的廣播。

// 註冊broadcastreceiver 

intentfilter filter = new intentfilter(bluetoothdevice.action_found); 

registerreceiver(mreceiver, filter); // 不要忘了之後解除繫結 

// 建立乙個接收action_found廣播的broadcastreceiver 

private final broadcastreceiver mreceiver = new broadcastreceiver()  

} }; 



Android藍芽開發

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

Android藍芽開發流程

首先,要操作藍芽,先要在androidmanifest.xml裡加入許可權 然後,看下api,android所有關於藍芽開發的類都在android.bluetooth包下,只有八個類 bluetoothadapter,bluetoothclass,bluetoothclass.device,blue...

Android 藍芽開發 《一》

關於android藍芽的開發 關於藍芽,官方文件上面提到可以用來 1.掃瞄其他的藍芽裝置 2.查詢當地的藍芽介面卡,對藍芽進行配對 3.建立rfcomn 序列 協議 通道 4.通過服務發現連線其他裝置 5.和其他裝置進行資料傳輸 6.管理多個連線 藍芽基礎 blutoothadapter 藍芽介面卡...