title: Raspberry Pi bluez C++, why does the bind() function return -1 (fail)?
body: <p>I have a Raspberry Pi Zero W that I'm trying to write code to connect to. The <code>bind()</code> command is failing with -1. I can't use <code>BDADDR_ANY</code> as I get a compile error of:</p>
  
  <blockquote>
    <p>taking address of temporary</p>
  </blockquote>
  
  <p>I'm using <code>my_bdaddr_any</code> instead but that is what gets the -1 return. If I use <code>my_bdaddr_all</code> or <code>my_bdaddr_local</code>, the bind works, but the <code>accept()</code> never works. Here is my code snippet:</p>
  
  <pre><code>char buf[1024] = {0};   
  int bluetoothSocket, client, bytes_read;         
  struct sockaddr_rc loc_addr = {0};
  struct sockaddr_rc client_addr = {0};
  
  socklen_t opt = sizeof(client_addr);    
  
  bdaddr_t my_bdaddr_any = {0, 0, 0, 0, 0, 0};
  bdaddr_t my_bdaddr_all = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  bdaddr_t my_bdaddr_local = {0, 0, 0, 0xff, 0xff, 0xff};     
  
  bluetoothSocket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);     
  
  loc_addr.rc_family = AF_BLUETOOTH;
  loc_addr.rc_bdaddr = (bdaddr_t &amp;) my_bdaddr_all;                
  loc_addr.rc_channel = (uint8_t) 1;
  
  int ret = -1;
  
  if(ret = bind(bluetoothSocket, (struct sockaddr *)&amp;loc_addr, sizeof(loc_addr)) == -1)    
  {
      printf("Bluetooth bind failed.\n");
      return 0;
  }
  
  listen(bluetoothSocket, 1);
  
  client = accept(bluetoothSocket, (struct sockaddr *)&amp;client_addr, &amp;opt);
  
  if (client == -1)
  {
      close(client);" 
  }
  
  ba2str(&amp;loc_addr.rc_bdaddr, buf);
  fprintf(stderr, "accepted connection from %s\n", buf);
  memset(buf, 0, sizeof(buf));
  
  bytes_read = read(client, buf, sizeof(buf));
  if (bytes_read &gt; 0) 
  {
      printf("Bluetooth bytes received [%s]\n", buf);
  }   
  close(client);
  close(bluetoothSocket);
  return; 
  </code></pre>