您尚未登录。

楼主 #1 2021-03-03 19:22:05

TeveT
会员
注册时间: 2019-07-01
已发帖子: 148
积分: 91

玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

平台 V3S, TINA
基于C,玩弄bluez的时候总感觉要在代码里去使用bluez的agent极其蛋疼,又没怎么处理过dbus的东西,既然如此,拿现成工具搞起来? 好!
如果要直接新开进程bluetoothctl , 可以这样:
使用Local socket 去访问,用socketpair 到一个文件流节点,然后 vfork 出进程,用一个循环去读文件流节点,来取得bluetoothctl 的返回值,
代码如下:

void bluetooth_pthread(void)
{
	int i = 0, ret,haveconnect = 0,sizeread = 0;
	char * Connectedyes = NULL,* Pairedyes = NULL,* yesorno = NULL,*tt = NULL;
   	int fds[2];
	pid_t pid; 
	unsigned char *datareadp = buf;
	memset(buf,0,sizeof(buf));

	char startbluetooth[128];
	sprintf(startbluetooth, "/usr/bin/bluetoothctl\n");

	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0)
		return -1;

	switch(pid=vfork()) {
	case -1:    /* Error */
		close(fds[0]);
		close(fds[1]);
		return -1;
	case 0:     /* child */
		close(fds[0]);
		dup2(fds[1], 0);
		dup2(fds[1], 1);
		close(fds[1]);
		execl("/bin/sh", "sh", "-c", startbluetooth, NULL);
		//system(bluectl);
		_exit(127);
	}
	/* parent */
	close(fds[1]);
	bluectl =  fds[0];


	bluesafe_write(bluectl,"power on\n",sizeof("power on\n"));//Changing power on succeeded

	while(!blueQuit)
    {
		if((sizeread = read(bluectl,datareadp,4096))> 0)
		{
			printf("\nsizeread = %d\n", sizeread);
			if(LoopBluetoothStateAndHandle(buf) == 0 )//处理你收到的bluetoothctl的返回数据
			{
				
			}
			else
			{
				
			}
		}
	}
	
	blueThreadId = 0;

	close(bluectl);

}

啊这? 大写的蛋疼! 测试的时候还发现read 会断流,简直了还得处理断帧和粘帧? 不要啊!

好了,因为蛋疼所以倒腾。
直接抓了bluetoothctl 单独拿出来编译,这样你可以嵌入任何你自己的代码里,同时可以在代码里去控制bluetooth的任何状态。
这样可以更愉快地玩耍bluez 了,dbus还不用怎么操作, 如果大佬们有更好的办法,可以分享一下,那更感谢了。
当然,这个编译,只要你平台支持了bluez, 那换个平台也是OK的。

文件见附件:
mybluetoothctl.zip

离线

#2 2021-03-05 08:40:05

奔跑的孩子
会员
注册时间: 2021-01-28
已发帖子: 51
积分: 31.5

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

请问楼主 bluetoothctl 是bluez 的源码包里面的吗?

离线

楼主 #3 2021-03-05 08:42:19

TeveT
会员
注册时间: 2019-07-01
已发帖子: 148
积分: 91

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

对的,我直接拿这部分嵌入我的代码,做蓝牙播放器界面

奔跑的孩子 说:

请问楼主 bluetoothctl 是bluez 的源码包里面的吗?

离线

#4 2021-03-05 08:48:36

raspberryman
会员
注册时间: 2019-12-27
已发帖子: 503
积分: 465

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

感谢楼主分享,我以前参考晕哥那个 btstack帖子搞了一个demo,后来没有玩了

郁闷,没有搞定 Ubuntu下用btstack协议栈驱动 RTL8723BS
http://whycan.com/t_1133.html#p53670

咦,居然发现楼主也玩了 btstack...

TeveT 说:

对的,我直接拿这部分嵌入我的代码,做蓝牙播放器界面

奔跑的孩子 说:

请问楼主 bluetoothctl 是bluez 的源码包里面的吗?

最近编辑记录 raspberryman (2021-03-05 08:49:10)

离线

楼主 #5 2021-03-05 08:54:00

TeveT
会员
注册时间: 2019-07-01
已发帖子: 148
积分: 91

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

主要就是不想花大时间弄dbus,既然拿出来单独用就分享一下,都是bluez的代码哈哈哈哈,嵌入自己的代码只要回调注册搞一搞,界面交互效率高

raspberryman 说:

感谢楼主分享,我以前参考晕哥那个 btstack帖子搞了一个demo,后来没有玩了

郁闷,没有搞定 Ubuntu下用btstack协议栈驱动 RTL8723BS
http://whycan.com/t_1133.html#p53670

咦,居然发现楼主也玩了 btstack...

TeveT 说:

对的,我直接拿这部分嵌入我的代码,做蓝牙播放器界面

奔跑的孩子 说:

请问楼主 bluetoothctl 是bluez 的源码包里面的吗?

离线

楼主 #6 2021-03-05 08:58:13

TeveT
会员
注册时间: 2019-07-01
已发帖子: 148
积分: 91

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

没有玩btstack啦,那时候测试驱动包,
最后测试发现,蓝牙声音出问题是因为驱动打印log问题

raspberryman 说:

感谢楼主分享,我以前参考晕哥那个 btstack帖子搞了一个demo,后来没有玩了

郁闷,没有搞定 Ubuntu下用btstack协议栈驱动 RTL8723BS
http://whycan.com/t_1133.html#p53670

咦,居然发现楼主也玩了 btstack...

TeveT 说:

对的,我直接拿这部分嵌入我的代码,做蓝牙播放器界面

奔跑的孩子 说:

请问楼主 bluetoothctl 是bluez 的源码包里面的吗?

离线

#7 2021-03-05 09:11:19

raspberryman
会员
注册时间: 2019-12-27
已发帖子: 503
积分: 465

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

dbus不适合嵌入式环境,在桌面也有点奇葩,估计是为了进程互操作和解藕吧。

我也想试一试,请问楼主这个代码可以在Ubuntu玩吗?

TeveT 说:

主要就是不想花大时间弄dbus,既然拿出来单独用就分享一下,都是bluez的代码哈哈哈哈,嵌入自己的代码只要回调注册搞一搞,界面交互效率高

raspberryman 说:

感谢楼主分享,我以前参考晕哥那个 btstack帖子搞了一个demo,后来没有玩了

郁闷,没有搞定 Ubuntu下用btstack协议栈驱动 RTL8723BS
http://whycan.com/t_1133.html#p53670

咦,居然发现楼主也玩了 btstack...

TeveT 说:

对的,我直接拿这部分嵌入我的代码,做蓝牙播放器界面

离线

楼主 #8 2021-03-05 09:12:38

TeveT
会员
注册时间: 2019-07-01
已发帖子: 148
积分: 91

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

编译器和库的依赖路径要改一下,应该是没问题的。

raspberryman 说:

dbus不适合嵌入式环境,在桌面也有点奇葩,估计是为了进程互操作和解藕吧。

我也想试一试,请问楼主这个代码可以在Ubuntu玩吗?

TeveT 说:

主要就是不想花大时间弄dbus,既然拿出来单独用就分享一下,都是bluez的代码哈哈哈哈,嵌入自己的代码只要回调注册搞一搞,界面交互效率高

raspberryman 说:

感谢楼主分享,我以前参考晕哥那个 btstack帖子搞了一个demo,后来没有玩了

郁闷,没有搞定 Ubuntu下用btstack协议栈驱动 RTL8723BS
http://whycan.com/t_1133.html#p53670

咦,居然发现楼主也玩了 btstack...

离线

#9 2021-03-05 09:20:39

raspberryman
会员
注册时间: 2019-12-27
已发帖子: 503
积分: 465

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

那有空我试一试,再次感谢楼主分享!  有不明白的问题再跟帖请教。

离线

楼主 #10 2021-03-05 09:21:50

TeveT
会员
注册时间: 2019-07-01
已发帖子: 148
积分: 91

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

吾乃菜狗,请多指教

raspberryman 说:

那有空我试一试,再次感谢楼主分享!  有不明白的问题再跟帖请教。

离线

#11 2021-03-05 13:37:17

sy373466062
会员
注册时间: 2018-11-12
已发帖子: 130
积分: 116

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

还是DBUS功能全一些

离线

楼主 #12 2021-03-05 13:45:09

TeveT
会员
注册时间: 2019-07-01
已发帖子: 148
积分: 91

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

可以通过bluetoothctl的代码骚修改来打开dbus的大门,哈哈哈,

sy373466062 说:

还是DBUS功能全一些

离线

#13 2021-03-08 14:52:21

leonaTian
会员
注册时间: 2021-03-08
已发帖子: 1
积分: 1

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

楼主,你好,最近也在这个蓝牙,用你这个代码编译(交叉编译器:arm-himix200-linux-gcc)失败,如下:

/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: error: ./client/main.o uses VFP register arguments, mybluetoothctl does not
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: failed to merge target specific data of file ./client/main.o
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: error: gdbus/polkit.o uses VFP register arguments, mybluetoothctl does not
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: failed to merge target specific data of file gdbus/polkit.o
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: error: gdbus/watch.o uses VFP register arguments, mybluetoothctl does not
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: failed to merge target specific data of file gdbus/watch.o
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: error: gdbus/mainloop.o uses VFP register arguments, mybluetoothctl does not
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: failed to merge target specific data of file gdbus/mainloop.o
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: error: gdbus/object.o uses VFP register arguments, mybluetoothctl does not
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: failed to merge target specific data of file gdbus/object.o
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: error: gdbus/client.o uses VFP register arguments, mybluetoothctl does not
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: failed to merge target specific data of file gdbus/client.o
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: error: client/display.o uses VFP register arguments, mybluetoothctl does not
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: failed to merge target specific data of file client/display.o
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: error: client/agent.o uses VFP register arguments, mybluetoothctl does not
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: failed to merge target specific data of file client/agent.o
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: error: client/gatt.o uses VFP register arguments, mybluetoothctl does not
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: failed to merge target specific data of file client/gatt.o
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: error: monitor/uuid.o uses VFP register arguments, mybluetoothctl does not
/opt/hisi-linux/x86-arm/arm-himix200-linux/host_bin/../lib/gcc/arm-linux-gnueabi/6.3.0/../../../../arm-linux-gnueabi/bin/ld: failed to merge target specific data of file monitor/uuid.o

这个是什么情况?

离线

楼主 #14 2021-03-08 15:28:16

TeveT
会员
注册时间: 2019-07-01
已发帖子: 148
积分: 91

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

依赖库对了吗,make clean 一下编。

leonaTian 说:

楼主,你好,最近也在这个蓝牙,用你这个代码编译(交叉编译器:arm-himix200-linux-gcc)失败,如下:
...
这个是什么情况?

离线

#15 2021-03-26 15:12:42

test0001
会员
注册时间: 2019-12-04
已发帖子: 38
积分: 33.5

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

大佬,bluez做蓝牙音箱要咋用,用pulseaudio,
搞晕了都

离线

楼主 #16 2021-03-26 18:13:25

TeveT
会员
注册时间: 2019-07-01
已发帖子: 148
积分: 91

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

参考我的这个: https://whycan.com/t_5305.html

test0001 说:

大佬,bluez做蓝牙音箱要咋用,用pulseaudio,
搞晕了都

离线

#17 2022-07-06 10:51:58

carinatus
会员
注册时间: 2022-06-20
已发帖子: 2
积分: 2

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

需要linux上开发bluez,代码协议都是海量内容要看

离线

楼主 #18 2022-07-06 12:02:57

TeveT
会员
注册时间: 2019-07-01
已发帖子: 148
积分: 91

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

carinatus 说:

需要linux上开发bluez,代码协议都是海量内容要看

确实啊大佬,DBUS 有得玩,有得头疼的。

离线

#19 2022-07-12 16:37:10

kk200
会员
注册时间: 2022-07-08
已发帖子: 9
积分: 4

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

请问bluetoothctl的源码是哪里找到的啊,bluez的源码里只找到了一个莫名其妙的文件叫做bluetoothctl,源码部分都找不到

离线

楼主 #20 2022-07-12 19:29:11

TeveT
会员
注册时间: 2019-07-01
已发帖子: 148
积分: 91

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

kk200 说:

请问bluetoothctl的源码是哪里找到的啊,bluez的源码里只找到了一个莫名其妙的文件叫做bluetoothctl,源码部分都找不到

就是bluez里边附带的 client 文件夹,就是这个源码。

离线

#21 2022-07-12 20:54:54

kk200
会员
注册时间: 2022-07-08
已发帖子: 9
积分: 4

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

嗯嗯,刚刚找到啦,之前是用notepad++在bluez整个文件里搜索bluetoothctl关键词,结果没有搜索到,只能搜索到bluez-5.64\completion\zsh路径下有一个bluetoothctl文件的,这个应该是编译好的文件,没有想到在client目录下,刚刚下载了您上传的附件,又去跟bluez的源码对比了一下才发现是在client目录下,谢谢楼主哈!

离线

#22 2022-12-28 15:31:15

josuke
会员
注册时间: 2022-12-28
已发帖子: 2
积分: 2

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

搞bluez开发dbus的学习是跳不过的:)

离线

#23 2023-04-12 16:48:02

DaneLam
会员
注册时间: 2023-04-12
已发帖子: 1
积分: 1

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

楼主你好,你拿出来单独编译的时候是只提取出了client这个目录吗?我这样编译不过啊

离线

楼主 #24 2023-07-07 17:26:49

TeveT
会员
注册时间: 2019-07-01
已发帖子: 148
积分: 91

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

DaneLam 说:

楼主你好,你拿出来单独编译的时候是只提取出了client这个目录吗?我这样编译不过啊

可以看下我的包的代码架构。

离线

#26 2023-11-13 15:56:26

哈利路亚
会员
注册时间: 2023-11-13
已发帖子: 7
积分: 2

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

正需要,直接调用bluetoothctl太耗性能,还是要api调着清爽一些,封装成socket更好了,起服务直接使用了

离线

楼主 #27 2023-11-28 11:51:24

TeveT
会员
注册时间: 2019-07-01
已发帖子: 148
积分: 91

Re: 玩弄bluetoothctl的时候直接拿过来单独编译搞事情(附源码)

哈利路亚 说:

正需要,直接调用bluetoothctl太耗性能,还是要api调着清爽一些,封装成socket更好了,起服务直接使用了

就是主打的学习和研究DBUS的套路出发, 单独集成自己APP 可以学习。
也可以自己起炉灶。反正都有个模仿到打通的过程。

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn