- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的理解是,SDP 是其他设备可以获取的 UUID 列表。
根据麻省理工学院的这个 PDF,“一种更通用的思考方式SDP 就像一个信息数据库。”这是否意味着我可以向 SDP 添加多个值?由于 Android 具有 BluetoothDevice.fetchUuidsWithSdp()
,我该如何设置设备的 UUID?
此外,UUID 的每个部分是什么意思? UUID 看起来像 00000000-0000-1000-8000-00805F9B34FB
,但这传达了什么信息?
最佳答案
UUID 标识在特定设备上可用的服务。因此,如果您调用 BluetoothDevice.fetchUUidsWithSdp()
,您的 BroadcastReceiver 将收到相关的 Intent ACTION_UUID。包含设备和服务 UUID。蓝牙规范defines some common UUIDs .
如果您不想连接到这些众所周知的服务之一,但打算实现您自己的蓝牙应用程序,那么您必须生成自己的 UUID(从 unix 控制台或 online generator 使用 uuidgen)标识您的应用程序/服务。您可以在 java 中创建 UUID 实例,如下所示 UUID uuid = UUID.fromString("785da8ea-1220-11e5-9493-1697f925ec7b");
。
因此,如果您在 Android 上为蓝牙应用程序创建服务器端,您通常会做 this
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothServerSocket serverSocket = adapter.listenUsingRfcommWithServiceRecord("YourHumanReadableServiceName", uuid);
这是您“设置”您的 UUID 的地方。 Android 蓝牙 API 为您创建包含应用程序 UUID 和名称的 SDP 条目。其他设备现在可以检索此条目。 Android 的蓝牙堆栈现在会将蓝牙 channel 关联到您的 BluetoothServerSocket。如果你想连接到这个 ServerSocket,连接端通常会做 this 连接。 :
// you will most likely already have this instance from a discovery or paired device list
BluetoothDevice serverDevice = adapter.getRemoteDevice(bluetoothMacAddress);
// connect to your ServerSocket using the uuid
BluetoothSocket socket = serverDevice.createRfcommSocketToServiceRecord(uuid);
socket.connect();
Android 将再次为您完成繁重的工作:它检查远程设备上的 SDP 记录,查找与您服务的 UUID 相对应的蓝牙 channel ,并使用此信息进行连接。
SO 上有一个常见的代码 fragment ,它建议您使用“反射”来获取类似于此代码的隐藏 API:
try {
// this is the way to go
socket = device.createRfcommSocketToServiceRecord(uuid);
socket.connect( );
} catch ( IOException exception ) {
// don't do that! You will bypass SDP and things will go sideways.
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(device, 1);
socket.connect();
}
大多数人都尝试过这个并且它在他们的开发环境中“正常工作”但是你应该知道你用它做什么。您主动绕过 SDP 查找,检索正确的蓝牙 channel 以与您的服务一起使用,您最终将连接到 channel 1。如果您的设备上运行了多个服务,在这种情况下,事情会发生变化,您将最终陷入调试 hell ;-)
我开发了一个 small middleware called Blaubot使用蓝牙/wifi/nfc 创建小型网络,并在我用来测试的设备(12 个型号)上遇到了各种问题。通常情况下,如果蓝牙堆栈有一些负载或经过多次连接/断开连接(如果您正在开发您的应用程序,您通常会遇到这种情况),则蓝牙堆栈不再完全正常运行。在这些情况下,device.createRfcommSocketToServiceRecord(uuid)
偶尔会失败,只有关闭蓝牙适配器并再次打开才有助于使蓝牙适配器恢复正常(在某些情况下仅在完全电源循环后) .如果发生这种情况并且您使用反射方法,您可能不会有太多的蓝牙乐趣。
但如果您知道这一点并在一定范围内保持对 BluetoothAdapter 的并发调用,蓝牙连接和适配器将非常稳定。
关于android - 蓝牙 SDP 和 UUID 如何工作? (专门针对安卓),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30813854/
我试图再次将反射的 UUID 转换回实际的 UUID 对象,但找不到方法,当我打印反射值时它看起来是正确的,但在尝试转换时我找不到方法。 package main import ( "fmt"
我想知道 UUID 是否是唯一的,即使它们是在不同的系统上生成的,这些系统可能采用不同的算法。例如,如果您在 MySQL 和 .Net 中生成了一堆 UUID,碰撞的可能性会更高,还是所有系统都使用完
是否可以一个接一个地创建两个重复的 UUID?我不熟悉 UUID 是如何生成的,但我猜想如果您在同一毫秒内从同一 MAC 地址创建了两个单独的 UUID,那么它们将完全相同。这是真的吗? 我想我是在问
当我使用 python uuid 模块中的 UUID() 函数检查我们的测试 uuid 之一时,我遇到了这种奇怪的行为。 从 uuid 导入 UUID uuid1 = UUID('00000000-0
开始使用 java.util.UUID。我的问题是如果我有两个 UUID 变量,比如 u1 和 u2,并且我想检查它们是否相等,我可以安全地使用表达式 u1 == u2 还是必须编写 u1 .equa
我浏览了 python UUID 模块的文档。 >>> uuid.uuid4() UUID('82fe5629-6680-4b13-a4e3-7a082f10e038') >>> uuid.uuid4
我正在创建一个程序,我在其中大量使用 UUID 来识别用户和组等内容。鉴于 UUID 已经被占用的可能性极低,我是否应该担心发生碰撞的可能性? 最佳答案 这在很大程度上取决于 A)您的要求 B)底层实
您应该使用哪个版本的 UUID?我看到很多帖子解释了每个版本的含义,但我很难弄清楚什么最适合哪些应用程序。 最佳答案 有两种不同的方式生成 UUID。 如果您只需要一个唯一 ID,则需要版本 1 或版
我知道我们可以轻松提取 uuid 版本号。有没有可靠的方法来提取时间戳、MAC 地址等信息? 谢谢! 最佳答案 符合标准的 UUID 可能是多种变体之一,它看起来像这样: AAAAAAAA-BBBB-
我可以干净地使用私有(private) UUID 变体/版本吗? 我使用我基本上认为是大整数的随机 UUID。现在,我想生成一个“私有(private)”UUID,它不基于众所周知的 5 个变体/版本
我已阅读 man 页面,但我不明白 name 和 namespace 的用途。 For version 3 and version 5 UUIDs the additional command lin
我目前正在项目中使用 boost::uuids::uuid,并且我想序列化包含 boost::uuids::uuid 的对象。我尝试了下面的简单示例,但出现错误: /usr/include/boost
我正在使用 Datastax Java 驱动程序在 Cassandra 数据库中执行基本的插入语句。我的主键列是uuid类型。从我在官方文档中看到的,在 Cassandra 中调用 uuid() 函数
会抛出异常吗? UUID() 是否会悄无声息地失败?是否有任何情况下“myStatus”来自 myStatus = True myUUID = uuid.UUID( someWeirdValue )
在我的 Android 应用程序中,我有这种采用 UUID 的方法。不幸的是,当我这样做时: OverviewEvent overviewevent = eventAdapter.getOvervie
我有一个简单的 mongo 迁移框架,它正在执行一些传递给它的脚本。 现在我想将我的 LUUID 迁移到 UUID。我写了以下内容: function fixIds(collectionName) {
我有一个非常奇怪的问题是我得到一个有效的 UUID 不是一个有效的 UUID,例如: 'fd31b6b5-325d-4b65-b496-d7e4d16c8a93' is not a valid UUI
我正在测试 Goa对于一个 API。我想使用 uuid 作为 ID 数据类型。我在 controller.go 中修改了以下函数: // Show runs the show action. func
我有一个包含 uuid 和系统列的表。我需要一个查询来仅返回具有 system=1 的 uuid,而不返回具有 system= 1 和 2 的 uuid 最佳答案 SELECT * FROM
我很想了解在 Avro 中编码一种非常特定类型的数据的最佳实践:UUID。 最佳答案 到目前为止,我发现的唯一方法是定义自定义 UUID: { "namespace" : "your.namesp
我是一名优秀的程序员,十分优秀!