2016-10-10 2 views
2

배경 : iOS 장치와 임베디드 리눅스 보드 사이에 OOB 페어링 (USB를 통해). 현재 리눅스 쪽에서 블루투스 링크 키 (향후 연결에 필요함)와 mac 주소을 받고 있습니다.
ps : 귀하가이 주제에 익숙하다면 - this question도 참조하십시오.
BlueZ : MAC 주소로 장치 이름을 얻으십시오.

나의 현재 구현은, (자세한 내용은 - 위의 질문에 대한 링크를 참조) 장치의 이름으로 MAC 주소를 사용하는 두 번째 bluetoothd 서비스를 다시 시작하면 해결되고있다.


질문 : 만 BlueZ의 도움으로 그 맥 갖는 기기의 블루투스 이름을 얻을 수있는 방법이 있나요? 나는 이것을 BlueZ의 DBus 인터페이스에 대한 액세스와 함께 C 코드에서 사용할 것이다.

답변

1

AFAIK, MAC 주소에서 이름을 찾는 직접적인 DBus API 또는 메소드가 없습니다. 그러나 이것은 디바이스의 "org.freedesktop.DBus.Properties"인터페이스에서 "GetManagedObjects"메소드를 사용하여 수행 할 수 있습니다.

다음은 적절한 오류 처리 및 변수가 추가 된 경우 작동해야하는 의사 코드입니다. DBus XML은 참조 용으로 소스의 맨 위에 추가됩니다.

#if 0 
dbus-send --system --dest=org.bluez --type=method_call --print-reply /org/bluez/hci0/dev_44_D8_84_02_A3_17 org.freedesktop.DBus.Introspectable.Introspect 
method return sender=:1.1 -> dest=:1.7 reply_serial=2 
    string "<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" 
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> 
<node> 
    <interface name="org.freedesktop.DBus.Introspectable"> 
     <method name="Introspect"> 
      <arg name="xml" type="s" direction="out"/> 
     </method> 
    </interface> 
    <interface name="org.bluez.Device1"> 
     <method name="Disconnect"></method> 
     <method name="Connect"></method> 
     <method name="ConnectProfile"> 
      <arg name="UUID" type="s" direction="in"/> 
     </method> 
     <method name="DisconnectProfile"> 
      <arg name="UUID" type="s" direction="in"/> 
     </method> 
     <method name="Pair"></method> 
     <method name="CancelPairing"></method> 

     <property name="Address" type="s" access="read"></property> 
     <property name="Name" type="s" access="read"></property> 
     <property name="Alias" type="s" access="readwrite"></property> 
     <property name="Class" type="u" access="read"></property> 
     <property name="Appearance" type="q" access="read"></property> 
     <property name="Icon" type="s" access="read"></property> 
     <property name="Paired" type="b" access="read"></property> 
     <property name="Trusted" type="b" access="readwrite"></property> 
     <property name="Blocked" type="b" access="readwrite"></property> 
     <property name="LegacyPairing" type="b" access="read"></property> 
     <property name="RSSI" type="n" access="read"></property> 
     <property name="Connected" type="b" access="read"></property> 
     <property name="UUIDs" type="as" access="read"></property> 
     <property name="Modalias" type="s" access="read"></property> 
     <property name="Adapter" type="o" access="read"></property> 
    </interface> 
    <interface name="org.freedesktop.DBus.Properties"> 
     <method name="Get"> 
      <arg name="interface" type="s" direction="in"/> 
      <arg name="name" type="s" direction="in"/> 
      <arg name="value" type="v" direction="out"/> 
     </method> 
     <method name="Set"> 
      <arg name="interface" type="s" direction="in"/> 
      <arg name="name" type="s" direction="in"/> 
      <arg name="value" type="v" direction="in"/> 
     </method> 
     <method name="GetAll"> 
      <arg name="interface" type="s" direction="in"/> 
      <arg name="properties" type="a{sv}" direction="out"/> 
     </method> 
     <signal name="PropertiesChanged"> 
      <arg name="interface" type="s"/> 
      <arg name="changed_properties" type="a{sv}"/> 
      <arg name="invalidated_properties" type="as"/> 
     </signal> 
    </interface> 
<node name="player0"/></node>" 
#endif 
#define BT_BLUEZ_NAME "org.bluez" 
#define BT_MANAGER_PATH "/" 
#define BT_ADAPTER_INTERFACE "org.bluez.Adapter1" 
#define BT_DEVICE_IFACE  "org.bluez.Device1" 
#define BT_MANAGER_INTERFACE "org.freedesktop.DBus.ObjectManager" 
#define BT_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties" 

int main(void) 
{ 
    char *known_address = "2C:F0:A2:26:D7:F5"; /*This is your address to search */ 

     conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); 
     proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, NULL, BT_BLUEZ_NAME, BT_MANAGER_PATH, BT_MANAGER_INTERFACE, NULL, &err); 
     result = g_dbus_proxy_call_sync(proxy, "GetManagedObjects", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err); 

    g_variant_get(result, "(a{oa{sa{sv}}})", &iter); 

     char *device_path = NULL; 
     char device_address[18] = { 0 }; 
     /* Parse the signature: oa{sa{sv}}} */ 
     while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &device_path, NULL)) { 
     { 
      char address[BT_ADDRESS_STRING_SIZE] = { 0 }; 
      char *dev_addr; 

      dev_addr = strstr(device_path, "dev_"); 
      if (dev_addr != NULL) { 
       char *pos = NULL; 
       dev_addr += 4; 
       g_strlcpy(address, dev_addr, sizeof(address)); 

       while ((pos = strchr(address, '_')) != NULL) { 
        *pos = ':'; 
       } 

       g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE); 
      } 

     } 

     if (g_strcmp0(known_address, device_address) == 0) { 
      /* Find the name of the device */ 
      device_property_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, NULL, BT_BLUEZ_NAME, &device_path, BT_PROPERTIES_INTERFACE, NULL, NULL); 
      result = g_dbus_proxy_call_sync(proxy, "Get", g_variant_new("(ss)", BT_DEVICE_IFACE, "Alias"), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); 

      const char *local = NULL; 
      g_variant_get(result, "(v)", &temp); 
      local = g_variant_get_string(temp, NULL); 
      printf("Your desired name : %s\n", local); 
     } 
     } 
} 
+0

감사합니다. 그러나이 코드는 이전에 bt 장치를 '찾았습니다'라고 가정합니다. – rsht

+0

@rsht 네, 귀하의 질문에 나는 당신이 이미 사용할 수있는 장치의 MAC 주소를 알고 있습니다. 그렇지 않은 경우 "StartDiscovery"와 같은 일부 어댑터 API에 관심이있을 수 있습니다. –

+0

예, 가지고 있습니다. 하지만 BlueZ의 스캔을 통해서가 아니라 iOS 특수 프로토콜 (USB를 통한 iAP2)을 통해 OOB 페어링 프로세스에서 가져 왔습니다. OOB 페어링의 요점은 정기적 인 블루투스 스캔/페어 절차없이 두 개의 장치를 페어링하는 것입니다. 어쨌든, 당신의 노력에 감사 드리며 다른 아이디어가 있다면 친절하게 알려주십시오. – rsht