【发布时间】:2015-06-01 07:17:06
【问题描述】:
我有一个蓝牙设备,我可以在 Linux 上使用 gatttool 来控制它。我想开发自己的可以向其发送命令的 c 程序。
我以前做过蓝牙编程,比较简单,类似于网络编程,但这次是蓝牙低功耗设备,遵循here的原则,当我可以清楚地看到主机已关闭消息时使用 gatttool 连接/断开连接。
如何创建这个程序?我知道我应该使用 bluez 库,但我不确定从哪里开始使用低能耗设备。
int main(int argc, char **argv)
{
struct sockaddr_rc addr = { 0 };
int s, status;
char dest[18] = "B4:99:4C:5C:EE:49";
char buf[2048];
pthread_t rthread;
setbuf(stdout, NULL);
// allocate a socket
s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
// 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
status = connect(s, (struct sockaddr *)&addr, sizeof(addr));
if( status < 0 ){
perror("Error connecting to host\n");
exit(1);
}
while(fgets(buf, sizeof(buf), stdin) != NULL){
status = send(s, buf, sizeof(buf), 0);
if(status < 0){
printf("Error sending.\n");
exit(1);
}
}
close(s);
return;
【问题讨论】:
标签: c bluetooth bluetooth-lowenergy bluez intel-edison