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

首頁 > OS > 安卓 > 正文

Android實現藍牙(BlueTooth)設備檢測連接

2024-09-11 17:15:19
字體:
來源:轉載
供稿:網友

無論是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>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凤台县| 峨眉山市| 福贡县| 什邡市| 博罗县| 晋江市| 清原| 长宁区| 九龙坡区| 都兰县| 屏东县| 银川市| 德庆县| 安达市| 南漳县| 沂南县| 蓬莱市| 惠安县| 浠水县| 北宁市| 江北区| 峨山| 固始县| 邯郸县| 安远县| 宁阳县| 安仁县| 普宁市| 家居| 竹山县| 长兴县| 唐海县| 长寿区| 慈利县| 河西区| 黑水县| 乐业县| 瑞丽市| 中卫市| 黄陵县| 克山县|