Android Bluetooth蓝牙技术初体验

时间:2022-09-20 12:04:51

一:bluetooth包简介

android平台提供了一个android.bluetooth的包,里面实现蓝牙设备之间通信的蓝牙api。总共有8个类,常用的四个类如下:

bluetoothadapter类

代表了一个本地的蓝牙适配器。它是所有蓝牙交互的入口点。利用它你可以发现其他蓝牙设备,查询绑定了的设备,使用已知的mac地址实例化一个蓝牙设备和建立一个bluetoothserversocket(作为服务器端)来监听来自其他设备的连接。

bluetoothdevice类

代表了一个远端的蓝牙设备,使用它请求远端蓝牙设备连接或者获取远端蓝牙设备的名称、地址、种类和绑定状态(其信息是封装在bluetoothsocket中)。

bluetoothsocket类

代表了一个蓝牙套接字的接口(类似于tcp中的套接字),它是应用程序通过输入、输出流与其他蓝牙设备通信的连接点。

blueboothserversocket类

代表打开服务连接来监听可能到来的连接请求(属于server端),为了连接两个蓝牙设备必须有一个设备作为服务器打开一个服务套接字。当远端设备发起连接连接请求的时候,并且已经连接到了的时候,blueboothserversocket类将会返回一个bluetoothsocket。

二:常用类的使用

bluetoothadapter:蓝牙适配器

canceldiscovery()取消探索,当我们正在搜索设备的时候调用这个方法将不再继续搜索
disable()关闭蓝牙
enable()打开蓝牙,这个方法打开蓝牙不会弹出提示,更多的时候我们需要问下用户是否打开,以下两行代码同样是打开蓝牙,但会提示用户:

intentenabler = new intent(bluetoothadapter.action_request_enable);
startactivity(enabler);
getaddress()获取本地蓝牙地址
getdefaultadapter()获取默认bluetoothadapter,实际上,也只有这一种方法获取bluetoothadapter
getname()获取本地蓝牙名称
getremotedevice(string address)根据蓝牙地址获取远程蓝牙设备
getstate()获取本地蓝牙适配器当前状态
isdiscovering()判断当前是否正在查找设备,是则返回true
isenabled()判断蓝牙是否打开,已打开返回true,否则返回false
listenusingrfcommwithservicerecord(string name,uuid uuid)根据名称,uuid创建并返回bluetoothserversocket,这是创建bluetoothsocket服务器端的第一步
startdiscovery()开始搜索,这是搜索的第一步

bluetoothdevice:远程蓝牙设备

createrfcommsockettoservicerecord(uuiduuid)根据uuid创建并返回一个bluetoothsocket,这个方法也是我们获取bluetoothdevice
的目的——创建bluetoothsocket

这个类其他的方法,如getaddress()、getname()等,同bluetoothadapter。

bluetoothsocket:客户端

?
1
2
3
4
5
6
7
//这个类一共有6个方法
close()关闭
connect()连接
isconnected()判断是否连接
getinptustream()获取输入流
getoutputstream()获取输出流
getremotedevice()获取bluetoothsocket指定连接的远程蓝牙设备

bluetoothserversocket:服务端

//这个类一共有4个方法
accept()
accept(int timeout)
close()关闭
getchannel()返回这个套接字绑定的通道

两者的区别在于后面的方法指定了过时时间,需要注意的是,执行这两个方法的时候,直到接收到了客户端的请求(或是过期之后),都会阻塞线程,应该放在新线程里运行。还有一点需要注意的是,这两个方法都返回一个bluetoothsocket,最后的连接也是服务器端与客户端的两个bluetoothsocket的连接

三:数据传输

蓝牙数据传输——服务器端

1、获得bluetoothadapter。
2、通过bluetoothadapter.listenusingrfcommwithservicerecord(name,uuid uuid)方法创建bluetoothserversocket对象。
3、通过luetoothserversocket.accept()方法返回一个bluetoothsocket对象。由于该方法处于阻塞状态,需要开启线程来处理。
4、通过bluetoothsocket.getinputstream()和bluetoothsocket.getoutputstream()方法获得读写数据的inputstream和outputstream对象。
5、通过inputstream.read()方法来读数据。通过outputstream.write()方法来写数据。

蓝牙数据传输——客户端

1、获得bluetoothadapter。
2、通过bluetoothadapter.getremotedevice(string address)获得指定地址的bluetoothdevice对象。
3、通过bluetoothdevice.createrfcommsockettoservicerecord (uuid uuid)方法创建bluetoothsocket对象。
4、通过bluetoothsocket.connect()方法来连接蓝牙设备。
5、通过bluetoothsocket.getinputstream()和bluetoothsocket.getoutputstream()方法获得读写数据的inputstream和outputstream对象。
6、通过inputstream.read()方法来读数据。通过outputstream.write()方法来写数据。

四:tip

uuid

?
1
2
3
4
5
// uuid:全局唯一标识符,格式为:8-4-4-4-12
// 两个蓝牙设备进行连接需要使用同一个uuid
 
<uses-permissionandroid:name="android.permission.bluetooth_admin" />
<uses-permissionandroid:name="android.permission.bluetooth" />

以上所述是小编给大家分享的android bluetooth蓝牙技术初体验的相关内容,下篇将给大家介绍android bluetooth蓝牙技术使用流程详解,感兴趣的朋友可以点击了解详情。