無論是WIFI還是4G網(wǎng)絡(luò),建立網(wǎng)絡(luò)連接后都是訪問互聯(lián)網(wǎng)資源,并不能直接訪問局域網(wǎng)資源。比如兩個人在一起,A要把手機上的視頻傳給B,通常情況是打開手機QQ,通過QQ傳送文件給對方。不過上傳視頻很耗流量,如果現(xiàn)場沒有可用的WIFI,手機的數(shù)據(jù)流量又不足,那又該怎么辦呢?為了解決這種鄰近傳輸文件的問題,藍牙技術(shù)應(yīng)運而生。藍牙技術(shù)是一種無線技術(shù)標準,可實現(xiàn)設(shè)備之間的短距離數(shù)據(jù)交換。
Android為藍牙技術(shù)提供了4個工具類,分別是藍牙適配器BluetoothAdapter、藍牙設(shè)備BluetoothDevice、藍牙服務(wù)端套接字BluetoothServerSocket和藍牙客戶端套接字BluetoothSocket。
藍牙適配器BluetoothAdapter
BluetoothAdapter的作用其實跟其它的**Manger差不多,可以把它當作藍牙管理器。下面是BluetoothAdapter的常用方法說明。
getDefaultAdapter:靜態(tài)方法,獲取默認的藍牙適配器對象;
enable:打開藍牙功能;
disable:關(guān)閉藍牙功能;
isEnable:判斷藍牙功能是否打開;
startDiscovery:開始搜索周圍的藍牙設(shè)備;
cancelDiscovery:取消搜索操作;
isDiscovering:判斷當前是否正在搜索設(shè)備;
getBondedDevices:獲取已綁定的設(shè)備列表;
setName:設(shè)置本機的藍牙名稱;
getName:獲取本機的藍牙名稱;
getAddress:獲取本機的藍牙地址;
getRemoteDevice:根據(jù)藍牙地址獲取遠程的藍牙設(shè)備;
getState:獲取本地藍牙適配器的狀態(tài);
listenUsingRfcommWithServiceRecord:根據(jù)名稱和UUID創(chuàng)建并返回BluetoothServiceSocket;
listenUsingRfcommOn:根據(jù)渠道編號創(chuàng)建并返回BluetoothServiceSocket。
藍牙設(shè)備BluetoothDevice
BluetoothDevice用于指代某個藍牙設(shè)備,通常表示對方設(shè)備。BluetoothAdapter管理的是本機藍牙設(shè)備。下面是BluetoothDevice的常用方法說明。
getName:獲得該設(shè)備的名稱; getAddress:獲得該設(shè)備的地址; getBondState:獲得該設(shè)備的綁定狀態(tài); createBond:創(chuàng)建匹配對象; createRfcommSocketToServiceRecord:根據(jù)UUID創(chuàng)建并返回一個BluetoothSocket。藍牙服務(wù)器套接字BluetoothServiceSocket
BluetoothServiceSocket是服務(wù)端的Socket,用來接收客戶端的Socket連接請求。下面是常用的方法說明。
accept:監(jiān)聽外部的藍牙連接請求;
close:關(guān)閉服務(wù)端的藍牙監(jiān)聽。
藍牙客戶端套接字BluetoothSocket
BluetoothSocket是客戶端的Socket,用于與對方設(shè)備進行數(shù)據(jù)通信。下面是常用的方法說明。
connect:建立藍牙的socket連接; close:關(guān)閉藍牙的socket連接; getInputStream:獲取socket連接的輸入流對象; getOutputStream:獲取socket連接的輸出流對象; getRemoteDevice:獲取遠程設(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="藍牙" 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="名稱" 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>
新聞熱點
疑難解答
圖片精選