LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-13-2009, 03:54 PM   #1
nishant.aem
LQ Newbie
 
Registered: May 2007
Posts: 2

Rep: Reputation: 0
Bluetooth RFCOM connection by two Dongle on same PC


hi,
I just started BT programming. I don't have BT mobile. So what i did, i purchased 2 BT dongle. what i think is that, i will made one Dongle as Master and other as slave on same system(PC).

Now i attach both dongle to same PC. Both are discoverable and by hciconfig scan command, device is comming also.

What i want to do now just to make RFCOMM connection between these two. So i made 2 program Client.c and Server.c

By client program i am trying to connect Server BT dongle address. Now what i am getting is...Connection Refused.

Please help me. i need to communicate these two dongle on same PC.

i am putting my program also...

*****************************************************************
Server.c
*****************************************************************



#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

int main(int argc, char **argv)
{
struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
char buf[1024] = { 0 };
int s, client, bytes_read;
socklen_t opt = sizeof(rem_addr);

// allocate socket
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

// bind socket to port 1 of the first available
// local bluetooth adapter
loc_addr.rc_family = AF_BLUETOOTH;
loc_addr.rc_bdaddr = *BDADDR_ANY;
loc_addr.rc_channel = (uint8_t) 1;
bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));

// put socket into listening mode
listen(s, 1);

// accept one connection
client = accept(s, (struct sockaddr *)&rem_addr, &opt);

ba2str( &rem_addr.rc_bdaddr, buf );
fprintf(stderr, "accepted connection from %s\n", buf);
memset(buf, 0, sizeof(buf));

// read data from the client
bytes_read = read(client, buf, sizeof(buf));
if( bytes_read > 0 ) {
printf("received from Client [%s]\n", buf);
}
// close connection
close(client);
close(s);
return 0;
}

**********************End of Sever.c*************************


/***********************************************************/
Client.c
**********************************************************
**
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

int main(int argc, char **argv)
{
struct sockaddr_rc addr = { 0 };
int s, status;
char dest[18] = "00:11:67:CB:3A:2B";

// allocate a socket
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
printf("step 1\n");
// set the connection parameters (who to connect to)
addr.rc_family = AF_BLUETOOTH;
addr.rc_channel = (uint8_t) 1;
str2ba( dest, &addr.rc_bdaddr );

printf("step 2\n");
// connect to server
status = connect(s, (struct sockaddr *)&addr, sizeof(addr));

printf("step 3\n");
// send a message
if( status == 0 ) {
status = write(s, "hello!", 6);
}

if( status < 0 ) perror("uh oh");

printf("step 4\n");
close(s);
return 0;
}

*************************** End of Client.c *************************
 
Old 03-22-2009, 10:31 AM   #2
nishant.aem
LQ Newbie
 
Registered: May 2007
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by nishant.aem View Post
hi,
I just started BT programming. I don't have BT mobile. So what i did, i purchased 2 BT dongle. what i think is that, i will made one Dongle as Master and other as slave on same system(PC).

Now i attach both dongle to same PC. Both are discoverable and by hciconfig scan command, device is comming also.

What i want to do now just to make RFCOMM connection between these two. So i made 2 program Client.c and Server.c

By client program i am trying to connect Server BT dongle address. Now what i am getting is...Connection Refused.

Please help me. i need to communicate these two dongle on same PC.

i am putting my program also...

*****************************************************************
Server.c
*****************************************************************



#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

int main(int argc, char **argv)
{
struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
char buf[1024] = { 0 };
int s, client, bytes_read;
socklen_t opt = sizeof(rem_addr);

// allocate socket
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

// bind socket to port 1 of the first available
// local bluetooth adapter
loc_addr.rc_family = AF_BLUETOOTH;
loc_addr.rc_bdaddr = *BDADDR_ANY;
loc_addr.rc_channel = (uint8_t) 1;
bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));

// put socket into listening mode
listen(s, 1);

// accept one connection
client = accept(s, (struct sockaddr *)&rem_addr, &opt);

ba2str( &rem_addr.rc_bdaddr, buf );
fprintf(stderr, "accepted connection from %s\n", buf);
memset(buf, 0, sizeof(buf));

// read data from the client
bytes_read = read(client, buf, sizeof(buf));
if( bytes_read > 0 ) {
printf("received from Client [%s]\n", buf);
}
// close connection
close(client);
close(s);
return 0;
}

**********************End of Sever.c*************************


/***********************************************************/
Client.c
**********************************************************
**
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

int main(int argc, char **argv)
{
struct sockaddr_rc addr = { 0 };
int s, status;
char dest[18] = "00:11:67:CB:3A:2B";

// allocate a socket
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
printf("step 1\n");
// set the connection parameters (who to connect to)
addr.rc_family = AF_BLUETOOTH;
addr.rc_channel = (uint8_t) 1;
str2ba( dest, &addr.rc_bdaddr );

printf("step 2\n");
// connect to server
status = connect(s, (struct sockaddr *)&addr, sizeof(addr));

printf("step 3\n");
// send a message
if( status == 0 ) {
status = write(s, "hello!", 6);
}

if( status < 0 ) perror("uh oh");

printf("step 4\n");
close(s);
return 0;
}

*************************** End of Client.c *************************
Nothing too big issue just a matter of permission. But its strange nobody just reply on this small issue. Client and Server is working very fine.
Thanks for not replying.... so that i tackle issue myself and sought out the issue. thanks... soon i will be back with new issue.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bluetooth USB dongle NDR008 Linux - Hardware 1 01-07-2007 05:52 PM
Installation of Bluetooth Dongle to enable GPRS internet connection leo_2005 Linux - Hardware 2 09-22-2006 07:34 AM
which bluetooth dongle linmith Linux - Networking 1 07-01-2005 04:09 PM
bluetooth dongle keiwu Linux - Hardware 0 02-13-2005 01:50 PM
Bluetooth USB Dongle vivekiyer Linux - Software 0 08-22-2003 05:36 AM


All times are GMT -5. The time now is 01:13 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration