2014-01-30 51 views
5

我正在构建一个Android APP,它可以跟踪设备上的蓝牙连接,并在它们中的一些丢失时触发警报(无论远程设备是否超出范围或关闭其蓝牙)。Android的蓝牙UUID连接APP到ANDROID

问题是,在android文档中,他们要求您输入UUID才能建立连接。

uuid是用于唯一标识信息的字符串ID的通用唯一标识符(UUID)标准化的128位格式。它用于唯一标识您的应用程序的蓝牙服务。

public ConnectThread(BluetoothDevice device) { 
    // Use a temporary object that is later assigned to mmSocket, 
    // because mmSocket is final 
    BluetoothSocket tmp = null; 
    mmDevice = device; 

    // Get a BluetoothSocket to connect with the given BluetoothDevice 
    try { 
     // MY_UUID is the app's UUID string, also used by the server code 
     tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 
    } catch (IOException e) { } 
    mmSocket = tmp; 
} 

,因为我不是在两台设备上安装应用程序时,我没有得到我的设置自己的UUID,我想用Android的,而不是...但我不能在任何地方找到它。

也许我没有正确接近问题......你们能帮我吗? :) 在此先感谢

回答

16

您可以从BluetoothDevice类

mmDevice = device; 

    // Get a BluetoothSocket to connect with the given BluetoothDevice. This code below show how to do it and handle the case that the UUID from the device is not found and trying a default UUID. 

    // Default UUID 
    private UUID DEFAULT_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

    try { 
     // Use the UUID of the device that discovered // TODO Maybe need extra device object 
     if (mmDevice != null) 
     { 
      Log.i(TAG, "Device Name: " + mmDevice.getName()); 
      Log.i(TAG, "Device UUID: " + mmDevice.getUuids()[0].getUuid()); 
      tmp = device.createRfcommSocketToServiceRecord(mmDevice.getUuids()[0].getUuid()); 

     } 
     else Log.d(TAG, "Device is null."); 
    } 
    catch (NullPointerException e) 
    { 
     Log.d(TAG, " UUID from device is null, Using Default UUID, Device name: " + device.getName()); 
     try { 
      tmp = device.createRfcommSocketToServiceRecord(DEFAULT_UUID); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
    } 
    catch (IOException e) { } 
+0

你得到的UUID!是!真棒! :D Thaaaaanks男人,不能告诉你我已经浪费了多少时间试图弄清楚这一点..我真的很感激它! :) – feresr

+1

很高兴我浪费了自己的时间。你可以通过蓝牙来检查这个https://github.com/itzikBraun/ArduinoCar它的应用程序控制和arduino,有两个线程处理连接,也许它会帮助你更多。 – Braunster

+0

这太奇怪了,android不会提供将两个设备配对在一起的方法! (他们现在只在API 19上添加了一个。当它看起来如此基本和基本时) – feresr