-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbtpc.c
91 lines (81 loc) · 2.06 KB
/
btpc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* =====================================================================================
*
* Filename: btpc.c
*
* Description:
*
* Version: 1.0
* Created: 09/15/11 04:14:10
* Revision: none
* Compiler: gcc
*
* Author: Tom Xue (), [email protected]
* Company: Nokia
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.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,*buf; //="00:11:67:32:61:62";
void baswap(bdaddr_t *dst, const bdaddr_t *src)
{
register unsigned char *d = (unsigned char *) dst;
register const unsigned char *s = (const unsigned char *) src;
register int i;
for (i = 0; i < 6; i++)
d[i] = s[5-i];
}
int str2ba(const char *str, bdaddr_t *ba)
{
uint8_t b[6];
const char *ptr = str;
int i;
for (i = 0; i < 6; i++) {
b[i] = (uint8_t) strtol(ptr, NULL, 16);
if (i != 5 && !(ptr = strchr(ptr, ':')))
ptr = ":00:00:00:00:00";
ptr++;
}
baswap(ba, (bdaddr_t *) b);
return 0;
}
if(argc==2)
{
dest=argv[1];
}
else
{
printf("prarm error\n");
exit(1);
}
// allocatea socket
s=socket(AF_BLUETOOTH,SOCK_STREAM,BTPROTO_RFCOMM);
if(s<0)
{
perror("create socket error");
exit(1);
}
// set the connection parameters (who to connect to )
addr.rc_family=AF_BLUETOOTH;
addr.rc_channel=(uint8_t)1;
str2ba(dest,&addr.rc_bdaddr);
// connect to server
printf("connectting...\n");
status=connect(s,(struct sockaddr *)&addr,sizeof(addr));
// send a message
if(status==0){
printf("scuess!\n");
status=write(s,"hello!",6);
close(s);
return 0;
}
}