AnneRanch
Posts: 91
Joined: Fri Oct 19, 2018 1:48 pm

Using bluetoothctl to initialize bluetooth

Tue May 14, 2019 6:30 pm

I was not sure where to post this , but bluetooth MAY be categorized as interface.

The basic questions are
what is bluetooth controller and
what is bluetooth device as far as RPi build-in bluetooth interface /hardware goes?

I am enclosing partial output from my attempt of using bluetoothctl to activate / verify bluetooth hardware.
Did not get very far.

Actually - is there " how to" navigate /use bluetoothctl to initialize /czech 3B RPi built-in bluetooith interface ?

Perhaps even a NAME for bluetoothctl would be nice .
Assuming it (what ? ) is based on "bluez" package.

Code: Select all

[email protected]:/ $ bluetoothctl
Agent registered                   [b] now what ?? [/b]

Available commands:
-------------------
advertise                                         Advertise Options Submenu
scan                                              Scan Options Submenu
gatt                                              Generic Attribute Submenu
list                                              List available controllers
show [ctrl]                                       Controller information
select <ctrl>                                     Select default controller
devices                                           List available devices

pair [dev]                                        Pair with device
trust [dev]                                       Trust device
untrust [dev]                                     Untrust device
block [dev]                                       Block device
unblock [dev]                                     Unblock device
remove <dev>                                      Remove device
connect <dev>                                     Connect device

[bluetooth]# show
[b]No default controller available[/b] hence no sense to look for "device " right ? 

[bluetooth]# show 0
Controller 0 not available
??? 

No controller means no device, right ?

User avatar
Douglas6
Posts: 4676
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Using bluetoothctl to initialize bluetooth

Tue May 14, 2019 7:06 pm

Bluetoothctl uses 'controller' to refer to the local Bluetooth device, commonly referred to as the 'adapter'. That is, the Bluetooth onboard chip or an USB device plugged into the Pi. 'Device' refers to remote devices.

Not sure what you mean by 'name' ? The name of the BlueZ CLI is 'bluetoothctl'. It is part of the BlueZ package.

Also not understanding what you mean by 'initializing' Bluetooth. If you haven't broken it, Bluetooth is initialized when you boot the Pi. Easiest way to tell is to run 'hciconfig -a' in a terminal. That should report that the adapter (hci0) is 'UP'. You may still have to pair (and trust) a remote device before you can connect with it; you can use bluetoothctl for that.

Bluetoothctl refers to adapters/devices by their Bluetooth device address. The results of 'show 0' are telling you there is no adapter with an address of '0'. A simple 'show' will report on the default adapter.

AnneRanch
Posts: 91
Joined: Fri Oct 19, 2018 1:48 pm

Re: Using bluetoothctl to initialize bluetooth

Tue May 14, 2019 8:04 pm

So I have controller , device , adapter and they ALL refer to same "thing".
I am not interested in USB bluetooth dongle stuff, just want to have RPI buidl-in bluetooth controller/ device / adapter working.

When i had blutoothctl working , now after reboot it does not, it reported "no controller".

Per you post it should be "active" after startup.


I just tried hciconfig -a and got NOTHINg back.
Just a terminal prompt.

I cannot provide more info since after reboot I lost all the "terminal " outputs.

Now the only way to get to blutoothctl is to provide limited options in initial command.
I suppose I should look for some decent documentation on bluetoothctl.

It apparently runs differently, or as of now not at all, when no options are provided. Odd.

Perhaps starting with
Register agent handler: <capability>


Code: Select all


[email protected]:~/Desktop $ bluetoothctl --help
bluetoothctl ver 5.50
Usage:
	bluetoothctl [options]
Options:
	--agent 	Register agent handler: <capability>
	--timeout 	Timeout in seconds for non-interactive mode
	--version 	Display version
	--help 		Display help
[email protected]:~/Desktop $ bluetoothctl --timeout
bluetoothctl: option '--timeout' requires an argument
bluetoothctl ver 5.50
Usage:
	bluetoothctl [options]
Options:
	--agent 	Register agent handler: <capability>
	--timeout 	Timeout in seconds for non-interactive mode
	--version 	Display version
	--help 		Display help
[email protected]:~/Desktop $ bluetoothctl --timeout 1  no rsponse (?) 



User avatar
Douglas6
Posts: 4676
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Using bluetoothctl to initialize bluetooth

Tue May 14, 2019 8:24 pm

Ok, the most common reason for there being no adapter is a faulty or missing hciuart service. Try this

Code: Select all

systemctl status hciuart
and post the results.

It might also be helpful to know what OS and version you're running, and any pertinent changes you've made, such as re-installing Bluetooth, changes to config.txt, etc.

AnneRanch
Posts: 91
Joined: Fri Oct 19, 2018 1:48 pm

Re: Using bluetoothctl to initialize bluetooth

Tue May 14, 2019 8:53 pm

Unit hciuart.service could not be found.
My RPi has been dormant for about two months.
Been busy trying to crosscompile libray for it and just finished.

Started checking my code and it did not detected any bluetooth devices so I went for bluetoothctl.
It initially worked just fine .
Now it is dead.
My code uses bluez library , bus so far I have used ONLY function detecting the "deviceID" .


That is partially why I am so paranoid abut naming stuff - the code uses "route" to detect "device ID".
Noting about ":controller " or even "agent".

I got an application which uses "agent " to pass executable to RPI.
That is why naming convention are important (to me).


Code: Select all

	// Get bluetooth device id
	int device_id = hci_get_route(NULL); // Passing NULL argument will retrieve the id of first avalaibe device
	if (device_id < 0) {
		printf("Error: Bluetooth device not found");
		perror("Error: Bluetooth device not found");
		exit(1);
	}




I have NOT gotten pass detection of device and have not written anything anywhere, not yet.




.

AnneRanch
Posts: 91
Joined: Fri Oct 19, 2018 1:48 pm

Re: Using bluetoothctl to initialize bluetooth

Tue May 14, 2019 9:08 pm

For correct name for bluetoothctl and documentation use

http://man.nicevoid.com/man/man1/bluetoothctl



Here is some more info.
Apparently this happen before "after" update .

I did try to install raspberrypi-sys-mods but it is current.

Code: Select all

[email protected]:/ $ sudo apt-get install raspberrypi-sys-mods
Reading package lists... Done
Building dependency tree       
Reading state information... Done
raspberrypi-sys-mods is already the newest version (20181127).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
[email protected]:/ $ bluetoothctl

[email protected]:/ $ bluetoothctl --help
bluetoothctl ver 5.50
Usage:
	bluetoothctl [options]
Options:
	--agent 	Register agent handler: <capability>
	--timeout 	Timeout in seconds for non-interactive mode
	--version 	Display version
	--help 		Display help
[email protected]:/ $ uname -a
Linux pi 4.19.40-v7+ #1217 SMP Tue May 7 17:22:08 BST 2019 armv7l GNU/Linux
[email protected]:/ $ 




PS I forgot how to check current / installed version of OS.
Could use some hints.
Last edited by AnneRanch on Tue May 14, 2019 9:13 pm, edited 1 time in total.

User avatar
Douglas6
Posts: 4676
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Using bluetoothctl to initialize bluetooth

Tue May 14, 2019 9:11 pm

I would try to (re)install pi-bluetooth.

Code: Select all

sudo apt-get install pi-bluetooth 
or

Code: Select all

sudo apt-get --reinstall install pi-bluetooth 
You might need to reboot. Then try 'hciconfig -a' again.

[EDIT: You might be running version 5.50 of bluetoothctl (and BlueZ). The version that ships with Raspbian Stretch is 5.43. You should think about starting over and installing the latest version of Raspbian.]

AnneRanch
Posts: 91
Joined: Fri Oct 19, 2018 1:48 pm

Re: Using bluetoothctl to initialize bluetooth

Tue May 14, 2019 9:27 pm

After sudo apt-get install pi-bluetooth I can access bluetoothctl and get ":list" controller.
Nice, but being paranoid I need to have some idea what whet wrong.
hciconfig -a also works .

All without reboot.

Last action I did was "exit" from bluetoothctl.
That should be legal.

Thanks for all the help. Appreciate it.

AnneRanch
Posts: 91
Joined: Fri Oct 19, 2018 1:48 pm

Re: Using bluetoothctl to initialize bluetooth

Tue May 14, 2019 9:58 pm

Well , I got my C++ code running and actually detecting device 0.
It looks as bluez library is working but there are some strange timing issues.
It would make some sense - the results from detecting bluetooth device have to go over WiFi /TCP back to my PC.
I do get "socket" connection , but then I loose the TCP - hence no more feedback from RPI.
That is going to be interesting to figure that out.
I cannot even go "ssh" to reboot RPI.

User avatar
Douglas6
Posts: 4676
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Using bluetoothctl to initialize bluetooth

Tue May 14, 2019 10:46 pm

Sorry, I could never get my head around C code. But if you post your code, somebody might be able to help. In general, Bluetooth operations should have no effect on TCP, and vice-versa. Personally, I would avoid socket programming, and set-up a virtual serial port on the Pi. Then, your C code only has to communicate with a serial device. But, I don't know what your goals are.

AnneRanch
Posts: 91
Joined: Fri Oct 19, 2018 1:48 pm

Re: Using bluetoothctl to initialize bluetooth

Wed May 15, 2019 1:22 am

My initial code is just "cut and paste " and it uses sockets.
I just need to prove that the library I have build works.
I have had some timing issues when I was just running plain local code, so I believe it has to do with timing on bluetooth.
Disabling TCP is different story and I may heed your r advise and abort sockets.
At this point communication speed is immaterial.

AnneRanch
Posts: 91
Joined: Fri Oct 19, 2018 1:48 pm

Re: Using bluetoothctl to initialize bluetooth

Wed May 15, 2019 3:48 pm

If there are no objections from management , I would like to continue this discussion.
After looking at the link posted I tried i to follow the example.
It looks looks like this:

Code: Select all

[email protected]:~ $ bluetoothctl
Agent registered
[bluetooth]# devices
[bluetooth]# 

I have not found anything ( definition ?) about "agent", so what is "agent registered" telling me"
Apparently I have no "devices".

The C code I am using "finds" first "device" - device with ID zero.
It does not tell anything about device with ID zero, then it scans for "adjacent devices" AND finds none.
I can fix that, but like to have an answers to original post - now adding
what is buletoothctl "agent "?
I am just asking for definition of these terms as used by bluetoothctl /RPi.
I do not need help with code.


Addendum
Here is what I have also tried , just FYI

Code: Select all

[email protected]:~ $ hciconfig
hci0:	Type: Primary  Bus: UART
	BD Address: B8:27:EB:11:3F:82  ACL MTU: 1021:8  SCO MTU: 64:1
	UP RUNNING 
	RX bytes:731 acl:0 sco:0 events:44 errors:0
	TX bytes:1755 acl:0 sco:0 commands:44 errors:0

[email protected]:~ $ 
[email protected]:~ $ hcitool scan
Scanning ...
[email protected]:~ $ 
[email protected]:~ $ sudo hciconfig hci0 piscan
[email protected]:~ $ hcitool scan
Scanning ...
[email protected]:~ $ hciconfig
hci0:	Type: Primary  Bus: UART
	BD Address: B8:27:EB:11:3F:82  ACL MTU: 1021:8  SCO MTU: 64:1
	UP RUNNING PSCAN ISCAN 
	RX bytes:767 acl:0 sco:0 events:50 errors:0
	TX bytes:1814 acl:0 sco:0 commands:48 errors:0

[email protected]:~ $ hcitool scan
Scanning ...
[email protected]:~ $ hcitool scan
Scanning ...
[email protected]:~ $ 


Final question
Could somebody confirm or deny that ALL the commands / application or whatever you want to call EVERYTHING but NOT C/C++ code are based on "bluez" (stack) ?

In an essence I need to find the underling hci_* functions to implement "commands " such as "hciconfig".
Sort of reverse engineering.
Last edited by AnneRanch on Wed May 15, 2019 4:33 pm, edited 1 time in total.

User avatar
Douglas6
Posts: 4676
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Using bluetoothctl to initialize bluetooth

Wed May 15, 2019 4:18 pm

An 'agent', or pairing agent, is a program that handles requests from another device to pair with the Pi. It may involve accepting a PIN code, and ultimately pairing the devices. It looks like your version of bluetoothctl starts an agent automatically; on mine I need to issue an 'agent on' command.

The 'devices' command lists discovered or paired remote devices, you have not scanned for or paired any yet.

The device that you're finding in your code (0), is the local adapter, not to be confused with a remote device. 'Adjacent devices' probably refers to remote devices.

AnneRanch
Posts: 91
Joined: Fri Oct 19, 2018 1:48 pm

Re: Using bluetoothctl to initialize bluetooth

Wed May 15, 2019 7:29 pm

Thanks.
So let me say this - I need to follow a sequence of commands to make for real outputs.
Not all the commands are intuitive.

It would be nice if such sequence was published somewhere.
Then I can "convert" such commands to C++ code where they actually came from anyway.

PS is "scan" seems to be dynamic event so it can be stopped as necessary. More documents to read.
Nice .

Code: Select all

[email protected]:~ $ bluetoothctl
Agent registered
[bluetooth]# scan
Missing on/off argument
[bluetooth]# scan -al
Invalid argument -al
[bluetooth]# scan on
Discovery started
[CHG] Controller B8:27:EB:11:3F:82 Discovering: yes
[NEW] Device 70:C0:4B:3E:2A:E7 70-C0-4B-3E-2A-E7
[NEW] Device 68:64:4B:16:CD:9E 68-64-4B-16-CD-9E
[CHG] Device 70:C0:4B:3E:2A:E7 RSSI: -72
[CHG] Device 68:64:4B:16:CD:9E RSSI: -73
[bluetooth]# 


User avatar
Douglas6
Posts: 4676
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Using bluetoothctl to initialize bluetooth

Wed May 15, 2019 7:52 pm

It's not possible to know what 'sequence' of 'commands' you are talking about, so I can't say if it has ever been published. If you want to see the source code for bluetoothctl, you can find it in the BlueZ github (link provided in another thread) in the /client/main.c file.

AnneRanch
Posts: 91
Joined: Fri Oct 19, 2018 1:48 pm

Re: Using bluetoothctl to initialize bluetooth

Wed May 15, 2019 8:28 pm

By sequence I meant - you cannot get "devices" until you do "scan on".
That may sound logical to gurus , but it is not.
I just learn that the sample code I started with inquires , that is the term "they" use, about REMOTE bluetooth "devices".
In my opinion that is illogical.
Why inquire about remote when we do not know squat about local device?

I ended up plugging bluetooth dongle to my PC and now my C++ program works as expected.
It looks as bluetoothctl scan gives out ton of info and so far no documentation has been found.

It also looks as "bluetooth" pairing is sort off automatic , but my goal was to verify the library and that has been achieved.
I appreciate all the help received.

I am not sure if it is customary on this forum to "close / mark solved " the thread.




-

User avatar
Douglas6
Posts: 4676
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Using bluetoothctl to initialize bluetooth

Wed May 15, 2019 11:14 pm

Ok, I hope I have been of some help.
By sequence I meant - you cannot get "devices" until you do "scan on".
That may sound logical to gurus , but it is not.
I disagree. To find out who is in the room, first you must open your eyes and look around. That seems very logical to me, and I'm no guru.

Good luck with your project. Things will get easier the more you learn. If you need more help, start a thread here.

PhatFil
Posts: 1246
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Using bluetoothctl to initialize bluetooth

Thu May 16, 2019 1:21 am

I found this useful when looking at Pi/B/toth and BLE
http://www.raspberry-pi-geek.com/Archiv ... (offset)/1

AnneRanch
Posts: 91
Joined: Fri Oct 19, 2018 1:48 pm

Re: Using bluetoothctl to initialize bluetooth

Thu May 16, 2019 2:17 am

Thanks for the link.
I am not into command line code.
I started this thread because one of the derivatives of bluez stack is bluetoothctl and I needed a easy way to activate bluetooth on RPi.
To be clear - all the "stuff" about bluetooth is derived from bluez.
Nothing magic or wrong about that.
Since bluez is a collection of hci functions it is only logical that an application can be build using theses functions,
Such application is bluetoothctl.
The other one is going to be my application.
My next step is to actually pass data via bluetooth , perhaps I will adopt using the term "hcI " instead.

Return to “Interfacing (DSI, CSI, I2C, etc.)”