Android 平台上蓝牙开发的关于 UUID 设置的注意事项
2012-05-24 18:00
399 查看
由于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
Since: API Level 10
Create an RFCOMM
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. Use
an encrypted and authenticated communication channel is desired.
This is designed to be used with
Use
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.
在蓝牙中,每个服务和服务属性都唯一地由"全球唯一标识符" (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
BluetoothSocketsocket 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. Use
createRfcommSocketToServiceRecord(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 设置的注意事项
- 关于文章“cocos2dx移植android平台-我的血泪史”需要注意事项
- 关于文章“cocos2dx移植android平台-我的血泪史”需要注意事项
- 关于Android的JNI开发SO库调java接口函数注意事项
- 关于Android应用开发的一些安全注意事项
- 关于Android应用开发的一些安全注意事项
- android开发关于隐藏文件的使用注意事项
- 关于Android应用开发的一些安全注意事项
- 关于Android应用开发的一些安全注意事项
- 关于Android 5.0 、6.0 、 7.0代码设置WIFI连接方式为静态IP或DHCP的方法及注意事项
- 关于android 经典蓝牙开发 使用UUID连接的问题
- 关于android蓝牙开发中 蓝牙可见性设置
- 微信在android平台上开发的注意事项(以官方提供的demo为例):
- 关于Android应用开发的一些安全注意事项
- 编译运行在omap4430平台的android-4.0.3注意事项
- android开发注意事项(转)
- Android开发环境配置注意事项
- 关于使用ZXing开发几点注意事项
- Android程序开发中关于设置全屏无效问题