首页 > 系统 > Android > 正文

Android实现蓝牙(BlueTooth)设备检测连接

2020-07-28 13:57:11
字体:
来源:转载
供稿:网友

无论是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>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表