你好,游客 登录 注册 搜索
背景:
阅读新闻

Android 平台上蓝牙开发的关于 UUID 设置的注意事项

[日期:2012-09-03] 来源:Linux社区  作者:leonzhouwei [字体: ]

由于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)
Since: API Level 10Create 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.

linux
相关资讯       Android平台 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款