Android藍芽操作

2021-09-07 21:19:24 字數 3252 閱讀 6897

藍芽是一種支援裝置短距離傳輸資料的無線技術。android在2.0以後提供了這方面的支援。 

從查詢藍芽裝置到能夠相互通訊要經過幾個基本步驟(本機做為伺服器): 

1.設定許可權 

在manifest中配置 

<

uses-permission

android:name="android.permission.bluetooth"

/>

<

uses-permission

android:name="android.permission.bluetooth_admin"

/>

2.啟動藍芽 

首先要檢視本機是否支援藍芽,獲取bluetoothadapter藍芽介面卡物件 

bluetoothadapter mbluetoothadapter = bluetoothadapter.getdefaultadapter();  

if(mbluetoothadapter == null)  

if(!mbluetoothadapter.isenabled())  

//......

public

void onactivityresult(int requestcode, int resultcode, intent data)  

}  }  

3。發現藍芽裝置 

這裡可以細分為幾個方面 

(1)使本機藍芽處於可見(即處於易被搜尋到狀態),便於其他裝置發現本機藍芽 

//使本機藍芽在300秒內可被搜尋

private

void ensurediscoverable()   

}  

(2)查詢已經配對的藍芽裝置,即以前已經配對過的裝置 

setpaireddevices = mbluetoothadapter.getbondeddevices();  

if (paireddevices.size() > 0)   

} else   

(3)通過mbluetoothadapter.startdiscovery();搜尋裝置,要獲得此搜尋的結果需要註冊 

乙個broadcastreceiver來獲取。先註冊再獲取資訊,然後處理 

//註冊,當乙個裝置被發現時呼叫onreceive

intentfilter filter = new intentfilter(bluetoothdevice.action_found);  

this.registerreceiver(mreceiver, filter);  

//當搜尋結束後呼叫onreceive

filter = new intentfilter(bluetoothadapter.action_discovery_finished);  

this.registerreceiver(mreceiver, filter);  

//.......

private broadcastreceiver mreceiver = new broadcastreceiver()   

}else

if (bluetoothadapter.action_discovery_finished.equals(action))   

}  }  

};  

4.建立連線 

查詢到裝置 後,則需要建立本機與其他裝置之間的連線。 

一般用本機搜尋其他藍芽裝置時,本機可以作為乙個服務端,接收其他裝置的連線。 

啟動乙個伺服器端的執行緒,死迴圈等待客戶端的連線,這與serversocket極為相似。 

這個執行緒在準備連線之前啟動 

//uuid可以看做乙個埠號

private

static

final uuid my_uuid =  

uuid.fromstring("fa87c0d0-afac-11de-8a39-0800200c9a66");  

//像乙個伺服器一樣時刻監聽是否有連線建立

private

class acceptthread extends thread catch (ioexception e)   

serversocket = temp;  

}  public

void run() catch (ioexception e)   

}  if(socket!=null)  

}  //取消監聽

public

void cancel() catch (ioexception e)   

}  }  

搜尋到裝置後可以獲取裝置的位址,通過此位址獲取乙個bluetoothdeviced物件,可以看做客戶端,通過此物件device.createrfcommsockettoservicerecord(my_uuid);同乙個uuid可與伺服器建立連線獲取另乙個socket物件,由此服務端與客戶端各有乙個socket物件,此時 

他們可以互相交換資料了。 

創立客戶端socket可建立執行緒 

//另乙個裝置去連線本機,相當於客戶端

private

class connectthread extends thread catch (ioexception e)   

}  public

void run() catch (ioexception e)  catch (ioexception e1)   

connetionfailed();  //連線失敗

return;  

}  //此時可以新建乙個資料交換執行緒,把此socket傳進去

}  public

void cancel()  catch (ioexception e)   

}  }  

5.建立資料通訊執行緒,進行讀取資料 

//建立連線後,進行資料通訊的執行緒

private

class connectedthread extends thread catch (ioexception e)   

}  public

void run() catch (ioexception e)   

}  }  

public

void write(byte buffer)  catch (ioexception e)   

}  public

void cancel()  catch (ioexception e)   

}  }  

到這裡,藍芽通訊的基本操作已經全部完成。 

**:

android筆記 藍芽操作2

關於設定藍芽的可見性和掃瞄周圍藍芽裝置的方法,需要用到廣播接收器 package tjj.bluetooth2 import android.bluetooth.bluetoothadapter import android.bluetooth.bluetoothdevice import andr...

Android 藍芽開發基礎操作

android name android.permission.bluetooth admin android name android.permission.bluetooth android name android.permission.access coarse location bluet...

android 藍芽檔案

利用android 傳送檔案需要解決一下幾個問題。1,傳送方選中檔案時如何獲取檔案位址?以下這段 作用是返回你選擇檔案的uri,因此你要重寫 onactivityresult來獲取返回的uri。但是這裡有有兩種情況存在先看下圖。當你選擇檔案管理的時候選擇其中的乙個檔案返回的uri為 file sto...