国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > OS > 安卓 > 正文

Android實(shí)現(xiàn)藍(lán)牙(BlueTooth)設(shè)備檢測(cè)連接

2024-09-11 17:15:20
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

無(wú)論是WIFI還是4G網(wǎng)絡(luò),建立網(wǎng)絡(luò)連接后都是訪問(wèn)互聯(lián)網(wǎng)資源,并不能直接訪問(wèn)局域網(wǎng)資源。比如兩個(gè)人在一起,A要把手機(jī)上的視頻傳給B,通常情況是打開(kāi)手機(jī)QQ,通過(guò)QQ傳送文件給對(duì)方。不過(guò)上傳視頻很耗流量,如果現(xiàn)場(chǎng)沒(méi)有可用的WIFI,手機(jī)的數(shù)據(jù)流量又不足,那又該怎么辦呢?為了解決這種鄰近傳輸文件的問(wèn)題,藍(lán)牙技術(shù)應(yīng)運(yùn)而生。藍(lán)牙技術(shù)是一種無(wú)線技術(shù)標(biāo)準(zhǔn),可實(shí)現(xiàn)設(shè)備之間的短距離數(shù)據(jù)交換。

Android為藍(lán)牙技術(shù)提供了4個(gè)工具類(lèi),分別是藍(lán)牙適配器BluetoothAdapter、藍(lán)牙設(shè)備BluetoothDevice、藍(lán)牙服務(wù)端套接字BluetoothServerSocket和藍(lán)牙客戶(hù)端套接字BluetoothSocket。

藍(lán)牙適配器BluetoothAdapter

BluetoothAdapter的作用其實(shí)跟其它的**Manger差不多,可以把它當(dāng)作藍(lán)牙管理器。下面是BluetoothAdapter的常用方法說(shuō)明。

getDefaultAdapter:靜態(tài)方法,獲取默認(rèn)的藍(lán)牙適配器對(duì)象;
enable:打開(kāi)藍(lán)牙功能;
disable:關(guān)閉藍(lán)牙功能;
isEnable:判斷藍(lán)牙功能是否打開(kāi);
startDiscovery:開(kāi)始搜索周?chē)乃{(lán)牙設(shè)備;
cancelDiscovery:取消搜索操作;
isDiscovering:判斷當(dāng)前是否正在搜索設(shè)備;
getBondedDevices:獲取已綁定的設(shè)備列表;
setName:設(shè)置本機(jī)的藍(lán)牙名稱(chēng);
getName:獲取本機(jī)的藍(lán)牙名稱(chēng);
getAddress:獲取本機(jī)的藍(lán)牙地址;
getRemoteDevice:根據(jù)藍(lán)牙地址獲取遠(yuǎn)程的藍(lán)牙設(shè)備;
getState:獲取本地藍(lán)牙適配器的狀態(tài);
listenUsingRfcommWithServiceRecord:根據(jù)名稱(chēng)和UUID創(chuàng)建并返回BluetoothServiceSocket;
listenUsingRfcommOn:根據(jù)渠道編號(hào)創(chuàng)建并返回BluetoothServiceSocket。

藍(lán)牙設(shè)備BluetoothDevice

BluetoothDevice用于指代某個(gè)藍(lán)牙設(shè)備,通常表示對(duì)方設(shè)備。BluetoothAdapter管理的是本機(jī)藍(lán)牙設(shè)備。下面是BluetoothDevice的常用方法說(shuō)明。

getName:獲得該設(shè)備的名稱(chēng); getAddress:獲得該設(shè)備的地址; getBondState:獲得該設(shè)備的綁定狀態(tài); createBond:創(chuàng)建匹配對(duì)象; createRfcommSocketToServiceRecord:根據(jù)UUID創(chuàng)建并返回一個(gè)BluetoothSocket。

藍(lán)牙服務(wù)器套接字BluetoothServiceSocket

BluetoothServiceSocket是服務(wù)端的Socket,用來(lái)接收客戶(hù)端的Socket連接請(qǐng)求。下面是常用的方法說(shuō)明。

accept:監(jiān)聽(tīng)外部的藍(lán)牙連接請(qǐng)求;
close:關(guān)閉服務(wù)端的藍(lán)牙監(jiān)聽(tīng)。

藍(lán)牙客戶(hù)端套接字BluetoothSocket

BluetoothSocket是客戶(hù)端的Socket,用于與對(duì)方設(shè)備進(jìn)行數(shù)據(jù)通信。下面是常用的方法說(shuō)明。

connect:建立藍(lán)牙的socket連接; close:關(guān)閉藍(lán)牙的socket連接; getInputStream:獲取socket連接的輸入流對(duì)象; getOutputStream:獲取socket連接的輸出流對(duì)象; getRemoteDevice:獲取遠(yuǎn)程設(shè)備信息。

layout/activity_bluetooth.xml界面布局代碼如下:界面布局代碼如下:

<LinearLayout xmlns:andro  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  android:padding="5dp">  <LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal">    <CheckBox      android:      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:button="@null"      android:checked="false"      android:drawableLeft="@drawable/ck_status_selector"      android:text="藍(lán)牙"      android:textColor="#ff000000"      android:textSize="17sp" />    <TextView      android:      android:layout_width="0dp"      android:layout_height="match_parent"      android:layout_weight="1"      android:gravity="right|center"      android:textColor="#ff000000"      android:textSize="17sp" />  </LinearLayout>  <LinearLayout    android:layout_width="match_parent"    android:layout_height="40dp"    android:orientation="horizontal">    <TextView      android:layout_width="0dp"      android:layout_height="match_parent"      android:layout_weight="4"      android:gravity="center"      android:text="名稱(chēng)"      android:textColor="#ff000000"      android:textSize="17sp" />    <TextView      android:layout_width="0dp"      android:layout_height="match_parent"      android:layout_weight="5"      android:gravity="center"      android:text="地址"      android:textColor="#ff000000"      android:textSize="17sp" />    <TextView      android:layout_width="0dp"      android:layout_height="match_parent"      android:layout_weight="2"      android:gravity="center"      android:text="狀態(tài)"      android:textColor="#ff000000"      android:textSize="17sp" />  </LinearLayout>  <ListView    android:    android:layout_width="match_parent"    android:layout_height="match_parent" /></LinearLayout>
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 通城县| 秦皇岛市| 永靖县| 连山| 文化| 永新县| 会泽县| 东至县| 军事| 勃利县| 滦平县| 勃利县| 印江| 泊头市| 旬邑县| 凉山| 福贡县| 涟水县| 溧水县| 荥经县| 乳山市| 广德县| 同德县| 封丘县| 娄底市| 崇礼县| 龙南县| 永寿县| 班戈县| 宾川县| 都安| 乐都县| 榆社县| 富蕴县| 武强县| 邢台县| 富川| 乐清市| 林州市| 兴业县| 南城县|