2014-01-30 4 views
5

장치에 블루투스 연결 트랙을 유지하고 원격 장치가 범위를 벗어나 있거나 꺼져 있는지 여부와 관계하여 일부가 손실 될 경우를 대비하여 알람을 발생시키는 Android APP를 구축 중입니다. 그 블루투스).안드로이드 블루투스 UUID와 APP를 ANDROID로 연결

그건 안드로이드 문서에서 연결을 위해 UUID를 물어 보는 것입니다.

uuid는 정보를 고유하게 식별하는 데 사용되는 문자열 ID에 대한 UUID (Universally Unique Identifier) ​​표준화 된 128 비트 형식입니다. 응용 프로그램의 Bluetooth 서비스를 고유하게 식별하는 데 사용됩니다.

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를 설정하지 않는, 내가 대신 안드로이드의를 사용하려면 ...하지만 난 아무데도 그것을 찾을 수 없습니다.

어쩌면 내가 문제에 올바르게 접근하지 못하고 있습니다 ... 저를 도와 줄 수 있습니까? :) 사전

답변

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

즐거움 ive는 이것에 meny 시간 자신을 낭비했다. 당신은이 https://github.com/itzikBraun/ArduinoCar 그것의 애플 리케이션 제어 및 arduino를 통해 블루투스를 통해 연결을 처리하는 두 개의 스레드가 있는지 확인할 수 있습니다 아마 더 당신을 도울 것입니다. – Braunster

+0

안드로이드가 두 장치를 함께 연결하는 방법을 제공하지 않는다는 것이 너무 이상합니다!. (그들은 API 19에서 단지 하나만 추가했습니다. 기본적이고 근본적인 것처럼 보일 때) – feresr

관련 문제