Android 平台上蓝牙开发的关于 UUID 设置的注意事项
来源:互联网 发布:windows 体验指数7.9 编辑:程序博客网 时间:2024/05/06 18:56
由于android蓝牙的通信都需要用到UUID,如果由手机发起搜索,当搜索到电脑的蓝牙时,能够得到蓝牙的地址(address),但通信时需要得到BluetoothSocket,而BluetoothSocket则需要电脑蓝牙的UUID,请问这个是怎么样得到的呢?
在蓝牙中,每个服务和服务属性都唯一地由"全球唯一标识符" (UUID)来校验。正如它的名字所暗示的,每一个这样的标识符都要在时空上保证唯一。UUID类可表现为短整形(16或32位)和长整形(128 位)UUID。他提供了分别利用String和16位或32位数值来创建类的构造函数,提供了一个可以比较两个UUID(如果两个都是128位)的方法,还有一个可以转换一个UUID为一个字符串的方法。UUID实例是不可改变的(immutable),只有被UUID标示的服务可以被发现。
在Linux下你用一个命令uuidgen -t可以生成一个UUID值;在Windows下则执行命令uuidgen 。UUID看起来就像如下的这个形式:2d266186-01fb-47c2-8d9f-10b8ec891363。当使用生成的UUID去创建一个 UUID对象,你可以去掉连字符。
我搞定了电脑和android手机的蓝牙通信问题:
首先解答几个问题
1.两边的UUID必须是一样的,这是一个服务的唯一标识,而且这个UUID的值必须是
00001101-0000-1000-8000-00805F9B34FB。为什么呢?因为这个是android的API上面说明的,用于普通蓝牙适配器和android手机蓝牙模块连接的,请大家自己看一下android有关bluetooth的API。
2.在连接的时候,如果电脑作为server(一直监听是否有服务连接),android手机作为client(主动和电脑建立连接),则需要在手机端调用这样一行代码:mmSocket.connect();
其中mmSocket是一个BluetoothSocket类,在这句话之前请确定你已经把手机和电脑进行了配对,而且那些乱七八糟的设置都搞定了。
http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html
public BluetoothSocket createInsecureRfcommSocketToServiceRecord (UUID uuid)
Create an RFCOMM BluetoothSocket
socket ready to start an insecure outgoing connection to this remote device using SDP lookup of uuid.
The communication channel will not have an authenticated link key i.e it will be subject to man-in-the-middle attacks. For Bluetooth 2.1 devices, the link key will be encrypted, as encryption is mandatory. For legacy devices (pre Bluetooth 2.1 devices) the link key will be not be encrypted. UsecreateRfcommSocketToServiceRecord(UUID)
if an encrypted and authenticated communication channel is desired.
This is designed to be used with listenUsingInsecureRfcommWithServiceRecord(String, UUID)
for peer-peer Bluetooth applications.
Use connect()
to initiate the outgoing connection. This will also perform an SDP lookup of the given uuid to determine which channel to connect to.
The remote device will be authenticated and communication on this socket will be encrypted.
Hint: If you are connecting to a Bluetooth serial board then try using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. However if you are connecting to an Android peer then please generate your own unique UUID.
- Android 平台上蓝牙开发的关于 UUID 设置的注意事项
- Android 平台上蓝牙开发的关于 UUID 设置的注意事项
- Android 平台上蓝牙开发的关于 UUID 设置的注意事项
- 关于android 经典蓝牙开发 使用UUID连接的问题
- android 蓝牙的UUID
- Android低功耗蓝牙应用开发获取的服务UUID
- 关于蓝牙服务对应的UUID码
- Android各种蓝牙设备的UUID
- Android各种蓝牙设备的UUID
- Android各种蓝牙设备的UUID
- Android各种蓝牙设备的UUID
- Android各种蓝牙设备的UUID
- 对Android蓝牙UUID的理解
- Android 蓝牙开发 UUID列表
- 蓝牙连接的uuid
- 蓝牙的uuid标准
- 关于Android蓝牙开发的一些经验之谈
- 关于android蓝牙开发的一些总结
- 树的各种算法
- XML CDATA是什么?
- Revit 2013 API新功能讲座--免费网络视频讲座5月31日下午2:00 (已结束,等待录制的视频下载)
- 编译程序和解释程序
- 解析html下的xml文件
- Android 平台上蓝牙开发的关于 UUID 设置的注意事项
- httpclient模拟登陆微博问题
- Linux下常用I/O模型
- 交叉编译python3.2
- 微博文摘——黑客的操作系统你敢用不?
- ISV客户博客系列:Windows Azure上的Softlibrary 和 Kern4Cloud
- C++中类的声明
- Model与UIControl之间的模型关系
- 比Detours更给力的Hook库