Android的:如何蓝牙UUID的工作?蓝牙、工作、Android、UUID

2023-09-12 21:49:39 作者:浅歌゛勿忘初心

我一直在跑来跑去SO和互联网大在过去几个小时,试图弄清楚这一点,我很困惑。

I've been running around SO and the internet at large for the past few hours trying to figure this out, and I'm confused.

基本上,我不明白什么是蓝牙UUID表示。不要的UUID表示协议(如 RFCOMM )?如果是这样,为什么 createRfcommSocketToServiceRecord()方法需要的UUID,当他们就在自己的名字中指定RFCOMM?为何BluetoothChat样品code有一个看似随意,很难codeD UUID?

Basically, I don't understand what a bluetooth UUID denotes. Do UUIDs denote protocols (e.g. RFCOMM)? If so, why do the createRfcommSocketToServiceRecord() methods require UUIDs, when they specify rfcomm right in their names? Why does the BluetoothChat sample code have a seemingly arbitrary, hardcoded UUID?

我的问题是因为,根据this问题,我使用的是反射得到一个空指针异常,当运行4.0.4试设备连接(到外部,非Android设备)。但是,解决这个问题对我来说是行不通的。 UUID muuid = device.getUuids()[0] .getUuid(); 抛出一个例外。

My question arises because, as per this question, I'm getting a null pointer exception when devices running 4.0.4 try to connect (to an external, non-android device) using reflection. However, the solution to that question doesn't work for me. UUID muuid = device.getUuids()[0].getUuid(); raises an exception.

修改:我已经通过硬编码的UUID的串行端口服务按这个回答解决了这个问题(使用 UUID.fromString(00001101-0000-1000-8000-00805f9b34fb); )。

Edit: I've solved that problem by hardcoding the UUID for Serial port service as per this answer (using UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");).

我还困惑于为什么我需要提供一个UUID来创建使用 createInsecureRfcommSocketToServiceRecord()不安全的RFCOMM插座,而不是使用反射法。

I'm further puzzled by why I need to supply a UUID to create an unsecured rfcomm socket using createInsecureRfcommSocketToServiceRecord(), but not using the reflection method.

任何人都可以理顺我出去?

Can anyone straighten me out?

推荐答案

它通常重新presents一些常见的服务(协议)的蓝牙设备的支持。

It usually represents some common service (protocol) that bluetooth device supports.

在(与 listenUsingRfcommWithServiceRecord ),你应该指定自己的UUID,以便连接到它的客户端可以识别它创建自己的RFCOMM的服务器; 它的原因 createRfcommSocketToServiceRecord 需要一个UUID的参数之一。

When creating your own rfcomm server (with listenUsingRfcommWithServiceRecord), you should specify your own UUID so that the clients connecting to it could identify it; it is one of the reasons why createRfcommSocketToServiceRecord requires an UUID parameter.

另外,一些常见的服务有相同的UUID,只要找到一个你需要的,并使用它。

Otherwise, some common services have the same UUID, just find one you need and use it.

请参阅这里

See here