开发者

Android Bluetooth Where can I get UUID?

I want to connect 3 devices via Bluetooth.

As for example I use BluetoothChat. So How I understand I should use different UUID for this devices. I have been trying to connect via such UUID=766c82f0-e1b4-11df-85ca-0800200c9a66, which I 've get it from Web UUID generator. But it doesn't work开发者_如何学编程 at all.

I have succesfully connected (to 1 device) if I used UUID=00001101-0000-1000-8000-00805F9B34FB

Where can I get UUID?


if you are using linux or mac, enter "uuidgen" this command without quotes in terminal, you will get an unique UUID, use that in your android project.


UUid is used to uniquely identify applications.Each application have a unique uuid .so use the same uuid for each device


In order to connect with your targetted devices, you need to know what are you connecting to. It'll be more helpful to list your device targets.

The UUID can be obtained from this link, http://www.bluecove.org/bluecove/apidocs/javax/bluetooth/UUID.html

Here you need to know what bluetooth profile is being used in each of your target device. You mentioned that "UUID=00001101-0000-1000-8000-00805F9B34FB" works.

This is due to your device is having a SPP Bluetooth profile. SPP stands for Serial Port Profile.

You could also lookup on Bluetoothdevice.getuuids http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#getUuids()


You must get the Bluetooth UUID to establish a connection to the device,

you can invoke the method getUuids() using reflection:

    try {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    Method getUuidsMethod = BluetoothAdapter.class.getDeclaredMethod("getUuids", null);
    ParcelUuid[] uuids = (ParcelUuid[]) getUuidsMethod.invoke(adapter, null);

         if(uuids != null) {
             for (ParcelUuid uuid : uuids) {
                 Log.d(TAG, "UUID: " + uuid.getUuid().toString());
             }
         }else{
             Log.d(TAG, "Uuids not found, be sure to enable Bluetooth!");
         }

    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }

Don´t forget to add the permission:

   <uses-permission android:name="android.permission.BLUETOOTH"/>

and you must enable bluetooth to get Uuids, for example:

UUID: 0000110f-0000-1000-8000-00805f9b12fb
UUID: 0000111d-0000-1000-8000-00805f9b12fb
UUID: 0000111a-0000-1000-8000-00805f9b12fb


Imagine, that u have a one or more services. Each service have its own UUID. UUID=00001101-0000-1000-8000-00805F9B34FB is special one for SPP. Some devices (for example, Bluetooth serial board) will not work if u not set SPP UUID. But for peer-to-peer connection between Android devices such as smartphones u may use your own generated UUID. Devices must set same UUID to found each other and connect. UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx where x=[0,...,9]|[A,...,F]. Good idea is to set xxxxxxxx-xxxx-xxxx-xxxx- to Your generated unique id. Second part xxxxxxxxxxxx may be set to Your server MAC address without ":". On client side u may construct UUID from known generated unique id (embedded to Your app) and server MAC address without ":". You can get server MAC address during Bluetooth device discovery.


you have to do a service discovery with the device you are trying to connect with, Get the UUID that it returns (which will be corresponding to the service that is running on the device and accepting connections) and then connect to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜