本文介绍了如何蓝牙SDP和UUID的工作? (专为Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,SDP是UUID的其他设备可以读取列表。

My understanding is that the SDP is a list of UUIDs that other devices can fetch.

根据此PDF麻省理工学院,想的更普遍的方式SDP是作为信息数据库。这是否意味着我可以添加多个值SDP?因为Android有 BluetoothDevice.fetchUuidsWithSdp(),我怎么设置设备的UUID ?

According to this PDF from MIT, "A more general way to think ofSDP is as an information database." Does this mean I can add multiple values to SDP? Since Android has BluetoothDevice.fetchUuidsWithSdp(), how do I set the UUIDs of a device?

此外,什么是一个UUID的每个部分是什么意思? UUID的样子 00000000-0000-1000-8000-00805F9B34FB ,但什么信息,这是否传递?

Also, what does each section of an UUID mean? UUIDs look like 00000000-0000-1000-8000-00805F9B34FB, but what information does this convey?

推荐答案

这是UUID可识别特定设备上的服务。所以,如果你调用 BluetoothDevice.fetchUUidsWithSdp()您的BroadcastReceiver将收到相关意向的的包含设备和服务的UUID。蓝牙规范定义了一些常用的UUID 的。

An UUID identifies a service that is available on a particular device. So if you call BluetoothDevice.fetchUUidsWithSdp() your BroadcastReceiver will receive the relevant Intent ACTION_UUID containing the device and the service UUID.The bluetooth specification defines some common UUIDs.

如果您不希望连接到其中的一个知名的服务,但意图实现自己的蓝牙应用程序,那么你必须只产生从UNIX控制台或的)标识您的应用程序/服务。你可以在java中这样创建一个UUID实例 UUID UUID = UUID.fromString(785da8ea-1220-11e5-9493-1697f925ec7b);

If you don't want to connect to one of these well known services but intent to implement your own bluetooth application, then you have to just generate your own UUID (use uuidgen from a unix console or an online generator) that identifies your application/service.You can create an UUID instance in java like this UUID uuid = UUID.fromString("785da8ea-1220-11e5-9493-1697f925ec7b");.

所以,如果你创建服务器端在Android的蓝牙应用程序,你通常做的这个

So if you create the server side for your bluetooth application on Android you typically do this

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothServerSocket serverSocket = adapter.listenUsingRfcommWithServiceRecord("YourHumanReadableServiceName", uuid);

这是你套你的UUID。 Android的蓝牙API创建SDP-项包括应用程序的UUID,并将其命名为您服务。其他设备现在可以检索这个条目。机器人蓝牙协议栈现在蓝牙渠道,你BluetoothServerSocket关联。如果您想要连接到该ServerSocket的,连接端通常连接做这个:

// 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和连接使用该信息蓝牙通道。

Android will again do the heavy lifting for you: It checks the SDP-Records on the remote device, looks up the bluetooth channel that corresponds to your service's UUID and connects using this information.

有一个共同的code段惊吓在这里上,以便通知:使用反射得到一个隐藏的API寻找与此类似code:

There is a common code snippet spooking around here on SO that advices you to use "reflection" to get to a hidden API looking similar to this code:

 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查找,检索正确的蓝牙渠道,你最终会连接到通道1。如果你有一个以上的服务的设备上运行,事情会横着走在这情况下,你会最终在调试地狱; - )

Most people try this and it "just works" in their dev environment but you should know what you do using this. You actively bypass the SDP lookup that retrieves the right bluetooth channel to be used with your service and you will end up connecting to channel 1. If you have more than one Service running on the device, things WILL go sideways in this cases and you will end up in debugging hell ;-)

我公司开发href="http://blaubot.hgross.eu/" rel="nofollow">小中间件称为Blaubot 使用蓝牙/ WIFI / NFC创建小型网络一个 device.createRfcommSocketToServiceRecord(UUID)有时会失败,只有打开蓝牙适配器关闭并重新帮助将蓝牙适配器起死回生(在某些情况下,只有一个完整的电源后)。如果发生这种情况,并且使用反射的方法,你可能不会有太大的乐趣与蓝牙。

I developed a small middleware called Blaubot to create small networks using bluetooth/wifi/nfc and experienced all sorts of problems on the devices I used to test with (12 models). It was often the case that the bluetooth stack was not fully functional anymore in cases where it got some load or after many connects/disconnects (which you usually will have, if you are developing your app). In these cases the device.createRfcommSocketToServiceRecord(uuid) would occasionally fail and only turning the bluetooth adapter off and on again helped to bring the bluetooth adapters back to life (in some cases only after a full power cycle). If this happens and you use the reflection method, you will probably not have much fun with bluetooth.

但如果你知道这一点,并在一定范围内,蓝牙连接并发呼叫保持到BluetoothAdapter和适配器将是pretty的稳定。

But if you know this and keep concurrent calls to the BluetoothAdapter within bounds, bluetooth connections and the adapters will be pretty stable.

这篇关于如何蓝牙SDP和UUID的工作? (专为Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 04:46