现在的位置: 首页 > 综合 > 正文

bluetooth UUID

2018年01月18日 ⁄ 综合 ⁄ 共 2820字 ⁄ 字号 评论关闭

As previously mentioned, in order to establish a connection to a remote device using Bluetooth, you must be able to discover and reference a specific service record on that device.  This gives rise to two possible paths by which this can be done:

  1. Know the UUID of the remote service ahead of time
  2. Read the SDP responses to determine the UUID fo what you need

In the sample program provided in the SDK, the former option is used.  Because the application is a point to point link between two Android devices, and the sample works for both sides of the conversation, the UUID is hard-coded into the application.  This
method does work, and in many cases is the proper way to handle things.  But what if the device your connecting to isn’t under your control, like an embedded sensor or media device?  Isn’t this why SDP exists in the first place?

It turns out, there are some holes in the current SDK API and documentation that make discovering unknown services a little difficult, but there are solutions.

Discovery via a Hidden Function

The Android SDK does include two methods in the BluetoothDevice class:

  • public boolean fetchUuidsWithSdp()
  • public ParcelUuid[] getUuids()

Both of these methods are designed to poke the underlying service and get references to the service records.  And in the current SDK (2.2 as of this writing) they are both hidden.  Niether of these methods can be called directly, even though
they are in the code.  They have to be called using reflection, and there is no guarantee that they will stay the same in future version of the SDK since they are not public.

BluetoothDevice.getUuids() is a synchronous method, and will return an array of the UUID records to the caller directly.  BluetoothDevice.fetchUuidsWithSdp() is an asynchronous method, and you will have to register for the Broadcast Intent that the system
fires for the available UUID that is found.

String action = "android.bleutooth.device.action.UUID"; IntentFilter filter = new IntentFilter(action); registerReceiver(mReceiver, filter);

Notice the misspelling of the word “bleutooth”, which is not a typo; that is the true Intent action string.  These Intents will include two extras that you can use to get the data you need

  • android.bluetooth.device.extra.DEVICE

    • Contains the BluetoothDevice that is being queried.
  • android.bluetooth.device.extra.UUID
    • Contains an array of ParcelUuid objects for the services.

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { BluetoothDevice deviceExtra = intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE"); Parcelable[] uuidExtra
= intent.getParcelableArrayExtra("android.bluetooth.device.extra.UUID"); //Parse the UUIDs and get the one you are interested in }};

UUID via Failed Connect

The only currently exposed in the SDK that also calls fetchUuidsWithSdp() under the hood is BluetoothSocket.connect().  By creating and attempting to connect to a BluetoothSocket, you will get the same UUID Broadcast Intent, at which point you could read
it, find the services you need, and then connect again.  This is a very counterintuitive method since a BluetoothSocket requires a service’s UUID to be created!

 

 http://wiresareobsolete.com/wordpress/2010/11/android-bluetooth-rfcomm/

抱歉!评论已关闭.