蓝牙开发的绪论。
在单片机或者在其他领域中,蓝牙的传输是比较重要的,虽然理论只有10米的范围可以传输数据,但是被许多开发者所需求,个人在开发中也是需要蓝牙传输,浏览了许多谷歌的APi文档和博客之后,决定好好写一篇关于蓝牙开发的文章,供大家参考!
蓝牙开发的概图。
说明:
①:什么是本地蓝牙适配器 BluetoothAdapter ?
BluetoothAdapter
代表本地蓝牙适配器(蓝牙无线)。BluetoothAdapter是所有蓝牙交互的入口。使用这个类,你能够发现其他的蓝牙设备,查询已配对设备的列表,使用已知的MAC地址来实例化一个BluetoothDevice对象,并且创建一个BluetoothServerSocket对象来监听与其他设备的通信。
在以前的安卓手机是没有蓝牙适配器的,相当于没有蓝牙功能,一个没有蓝牙功能的安卓手机,怎么可以蓝牙通讯。所以先判断是否有蓝牙适配器?再往下的操作。
②:什么是 BlueToothDevice?
BluetoothDevice
代表一个远程的蓝牙设备。使用这个类通过BluetoothSocket或查询诸如名称、地址、类和配对状态等设备信息来请求跟远程设备的连接。
③:权限?
为了在你的应用程序中使用蓝牙功能,至少要声明两个蓝牙权限(BLUETOOTH和BLUETOOTH_ADMIN)中的一个。
为了执行任何蓝牙通信(如请求连接、接收连接和传输数据),你必须申请BLUETOOTH权限。
为了启动设备发现或维护蓝牙设置,你必须申请BLUETOOTH_ADMIN权限。大多数需要这个权限的应用程序,仅仅是为能够发现本地的蓝牙设备。这个权限所授予的其他能力应该不被使用,除非是电源管理的应用程序,它会在依据用户的请求来修改蓝牙设置。注意:如果你使用了BLUETOOTH_ADMIN权限,那么必须要有BLUETOOTH权限。
在清单文件中声明如下权限:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
在安卓6.0或以上还需要添加以下权限,并且是代码动态添加!
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
④:蓝牙广播接收器?
利用安卓的广播机制,扫描附近可用蓝牙设备,接收每个被发现设备的的信息,你的应用程序必须注册一个ACTION_FOUND类型的广播接收器。对应每个蓝牙设备,系统都会广播ACTION_FOUND类型的Intent。这个Intent会携带EXTRA_DEVICE和EXTRA_CLASS附加字段,这个两个字段分别包含了BluetoothDevice和BluetoothClass对象。
⑤:输入输出流?
利用Java的输入输出流操作即可,
mSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(uuid);
mOutS = mSocket.getOutputStream();
input=mSocket.getInputStream();
⑥:UUID?
UUID相当于一个蓝牙设备和另外一个蓝牙通讯的”秘钥”!不同的蓝牙通讯用不同的秘~而我们与单片机通讯的是
00001101-0000-1000-8000-00805F9B34FB,即蓝牙串口服务。
ServiceDiscoveryServerServiceClassID_UUID = ‘{00001000-0000-1000-8000-00805F9B34FB}’
BrowseGroupDescriptorServiceClassID_UUID = ‘{00001001-0000-1000-8000-00805F9B34FB}’
PublicBrowseGroupServiceClass_UUID = ‘{00001002-0000-1000-8000-00805F9B34FB}’
蓝牙串口服务
SerialPortServiceClass_UUID = ‘{00001101-0000-1000-8000-00805F9B34FB}’
LANAccessUsingPPPServiceClass_UUID = ‘{00001102-0000-1000-8000-00805F9B34FB}’
拨号网络服务
DialupNetworkingServiceClass_UUID = ‘{00001103-0000-1000-8000-00805F9B34FB}’
信息同步服务
IrMCSyncServiceClass_UUID = ‘{00001104-0000-1000-8000-00805F9B34FB}’
SDP_OBEXObjectPushServiceClass_UUID = ‘{00001105-0000-1000-8000-00805F9B34FB}’
文件传输服务
OBEXFileTransferServiceClass_UUID = ‘{00001106-0000-1000-8000-00805F9B34FB}’
IrMCSyncCommandServiceClass_UUID = ‘{00001107-0000-1000-8000-00805F9B34FB}’
SDP_HeadsetServiceClass_UUID = ‘{00001108-0000-1000-8000-00805F9B34FB}’
CordlessTelephonyServiceClass_UUID = ‘{00001109-0000-1000-8000-00805F9B34FB}’
SDP_AudioSourceServiceClass_UUID = ‘{0000110A-0000-1000-8000-00805F9B34FB}’
SDP_AudioSinkServiceClass_UUID = ‘{0000110B-0000-1000-8000-00805F9B34FB}’
SDP_AVRemoteControlTargetServiceClass_UUID = ‘{0000110C-0000-1000-8000-00805F9B34FB}’
SDP_AdvancedAudioDistributionServiceClass_UUID = ‘{0000110D-0000-1000-8000-00805F9B34FB}’
SDP_AVRemoteControlServiceClass_UUID = ‘{0000110E-0000-1000-8000-00805F9B34FB}’
VideoConferencingServiceClass_UUID = ‘{0000110F-0000-1000-8000-00805F9B34FB}’
IntercomServiceClass_UUID = ‘{00001110-0000-1000-8000-00805F9B34FB}’
蓝牙传真服务
FaxServiceClass_UUID = ‘{00001111-0000-1000-8000-00805F9B34FB}’
HeadsetAudioGatewayServiceClass_UUID = ‘{00001112-0000-1000-8000-00805F9B34FB}’
WAPServiceClass_UUID = ‘{00001113-0000-1000-8000-00805F9B34FB}’
WAPClientServiceClass_UUID = ‘{00001114-0000-1000-8000-00805F9B34FB}’
个人局域网服务
PANUServiceClass_UUID = ‘{00001115-0000-1000-8000-00805F9B34FB}’
个人局域网服务
NAPServiceClass_UUID = ‘{00001116-0000-1000-8000-00805F9B34FB}’
个人局域网服务
GNServiceClass_UUID = ‘{00001117-0000-1000-8000-00805F9B34FB}’
DirectPrintingServiceClass_UUID = ‘{00001118-0000-1000-8000-00805F9B34FB}’
ReferencePrintingServiceClass_UUID = ‘{00001119-0000-1000-8000-00805F9B34FB}’
ImagingServiceClass_UUID = ‘{0000111A-0000-1000-8000-00805F9B34FB}’
ImagingResponderServiceClass_UUID = ‘{0000111B-0000-1000-8000-00805F9B34FB}’
ImagingAutomaticArchiveServiceClass_UUID = ‘{0000111C-0000-1000-8000-00805F9B34FB}’
ImagingReferenceObjectsServiceClass_UUID = ‘{0000111D-0000-1000-8000-00805F9B34FB}’
SDP_HandsfreeServiceClass_UUID = ‘{0000111E-0000-1000-8000-00805F9B34FB}’
HandsfreeAudioGatewayServiceClass_UUID = ‘{0000111F-0000-1000-8000-00805F9B34FB}’
DirectPrintingReferenceObjectsServiceClass_UUID = ‘{00001120-0000-1000-8000-00805F9B34FB}’
ReflectedUIServiceClass_UUID = ‘{00001121-0000-1000-8000-00805F9B34FB}’
BasicPringingServiceClass_UUID = ‘{00001122-0000-1000-8000-00805F9B34FB}’
PrintingStatusServiceClass_UUID = ‘{00001123-0000-1000-8000-00805F9B34FB}’
人机输入服务
HumanInterfaceDeviceServiceClass_UUID = ‘{00001124-0000-1000-8000-00805F9B34FB}’
HardcopyCableReplacementServiceClass_UUID = ‘{00001125-0000-1000-8000-00805F9B34FB}’
蓝牙打印服务
HCRPrintServiceClass_UUID = ‘{00001126-0000-1000-8000-00805F9B34FB}’
HCRScanServiceClass_UUID = ‘{00001127-0000-1000-8000-00805F9B34FB}’
CommonISDNAccessServiceClass_UUID = ‘{00001128-0000-1000-8000-00805F9B34FB}’
VideoConferencingGWServiceClass_UUID = ‘{00001129-0000-1000-8000-00805F9B34FB}’
UDIMTServiceClass_UUID = ‘{0000112A-0000-1000-8000-00805F9B34FB}’
UDITAServiceClass_UUID = ‘{0000112B-0000-1000-8000-00805F9B34FB}’
AudioVideoServiceClass_UUID = ‘{0000112C-0000-1000-8000-00805F9B34FB}’
SIMAccessServiceClass_UUID = ‘{0000112D-0000-1000-8000-00805F9B34FB}’
PnPInformationServiceClass_UUID = ‘{00001200-0000-1000-8000-00805F9B34FB}’
GenericNetworkingServiceClass_UUID = ‘{00001201-0000-1000-8000-00805F9B34FB}’
GenericFileTransferServiceClass_UUID = ‘{00001202-0000-1000-8000-00805F9B34FB}’
GenericAudioServiceClass_UUID = ‘{00001203-0000-1000-8000-00805F9B34FB}’
GenericTelephonyServiceClass_UUID = ‘{00001204-0000-1000-8000-00805F9B34FB}’