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