Android 蓝牙开发(整理大全)
来源:互联网 发布:知乎 李云迪前妻 编辑:程序博客网 时间:2024/04/28 08:32
Android蓝牙开发
鉴于国内Android蓝牙开发的例子很少,以及蓝牙开发也比较少用到,所以找的资料不是很全。
(一):
由于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.
(二):
- Android 蓝牙开发(整理大全)
- Android 编程技巧之 ----- 蓝牙开发大全
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android 蓝牙开发之搜索、配对、连接、通信大全
- 蓝牙----Android的蓝牙开发
- Android 蓝牙开发-蓝牙通信
- Android蓝牙开发【三】蓝牙Hid开发
- Android蓝牙开发
- Android蓝牙应用开发
- Android蓝牙开发
- Android 蓝牙开发详述
- 初涉Android蓝牙开发
- 初涉Android蓝牙开发
- 初涉Android蓝牙开发
- QQ2013协议分析(一)准备工作
- NFSv 4.0 的变化
- nyoj-23-取石子(一)
- spring+hibernate下Log4j的配置
- 黑马程序员 一道内存分配的面试题
- Android 蓝牙开发(整理大全)
- 利润分配核算
- 常用js表单验证
- windows XP 提示“用户配置文件服务登录失败”错误消息
- Ubuntu apt-get 总结
- VS2010/MFC编程入门之四十四(MFC常用类:定时器Timer)
- Android make的一些基本命令
- HDU 1319 Prime Cuts(素数。。。)
- linux中的kill命令 及 强制终止进程的方法