無(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>
新聞熱點(diǎn)
疑難解答
圖片精選