How can I correctly to use HCI device with Bluez in linux - linux

We are working to use external Bluetooth device though BlueZ 5.49 and HCI-UART.
After using btattach or hciattach , we use the hciconfig to check the device status but the device always show DOWN.
The log as bellowing:
# hciattach -b /dev/ttyS1 any 1000000
Device setup complete
#
# hciconfig hci0 up
Can't init device hci0: Cannot assign requested address (99)
#
#hci0: Type: Primary Bus: UART
BD Address: 00:00:00:00:00:00 ACL MTU: 27:7 SCO MTU: 0:0
DOWN
RX bytes:231 acl:0 sco:0 events:16 errors:0
TX bytes:88 acl:0 sco:0 commands:16 errors:0
We use btmon to check hci command and the command was workable but the last HCI command was Set_Event_Mask_Page2. I checked several times and the result was always the same.
Does some one have such kind of experience ? or can suggest me which part I can look into??
Linux Kenerl :4.9.89-1.6,
BlueZ: 5.49

Related

ASUS N751JK, bluetooth doesn't work, ubuntu 17.10

Can't make my bluetooth work on Ubuntu.
The toggle is active.But it means nothing. Since system is not reacting on it
rfkill list output is normal:
0: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no
1: hci0: Bluetooth
Soft blocked: no
Hard blocked: no
hciconfig -a gives the following info
hci0: Type: Primary Bus: USB
BD Address: 9C:D2:1E:0D:2F:B2 ACL MTU: 1021:4 SCO MTU: 128:2
DOWN
RX bytes:1160 acl:0 sco:0 events:62 errors:0
TX bytes:742 acl:0 sco:0 commands:62 errors:0
Features: 0xff 0xff 0x8f 0xfe 0xdb 0xff 0x5b 0x87
Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
Link policy: RSWITCH HOLD SNIFF PARK
Link mode: SLAVE ACCEPT
Typping of sudo hciconfig hci0 up results in:
Can't init device hci0: Invalid request code (56)
The output of hcitool dev command is empty:
Devices:
This is hcitool scan output
Device is not available: No such device
I seached for similar problems, checked their solutions if it works in my case, but failed - nothing has helped me. So I've decided to ask it here
There may be another usefull information which is not given here for now. But I don't know yet what should be provided in order to make the problem be solved easier.
Download the driver for the Mediatek MT7630E wireless card here
https://community.linuxmint.com/tutorial/view/1796
and execute the commands to compile and install the driver.
If wifi works after installing the driver but bluetooth doesn't, you should check "Readme.md" in driver file directory (/Downloads/MT7630E-release).
It worked for me in Linux Mint 18.3. Both wifi and bluetooth. But wifi only at 2.4GHz

Why is LE device connected automatically on HCI up?

Whenever I hit hciconfig hci0 up noscan, followed by hcitool con after a few seconds, my LE device is always connected automatically.
> hciconfig hci0
hci0: Type: BR/EDR Bus: USB
BD Address: YY:YY:YY:YY:YY:YY ACL MTU: 310:10 SCO MTU: 64:8
DOWN
RX bytes:494922 acl:12605 sco:0 events:8429 errors:0
TX bytes:63543 acl:917 sco:0 commands:3692 errors:0
> hciconfig hci0 up
... (wait a few seconds)
> hcitool con
Connections:
< LE XX:XX:XX:XX:XX:XX handle 70 state 1 lm MASTER AUTH ENCRYPT
I initially thought that only devices in White List will be connected automatically, but only then to figure out that the device is not in white list after I typed in the following command.
> hcitool lewlrm XX:XX:XX:XX:XX:XX
Can't remove from white list: Input/output error(5)
Question:
Why will my device connected automatically when it is not in the White List?
Environment:
Kernel Version: Linux 4.11.1
Bluez Version: 5.37
My solution is to use bluetoothctl to disconnect and remove it.
> bluetoothctl
[bluetooth] > disconnect XX:XX:XX:XX:XX:XX
[bluetooth] > remove XX:XX:XX:XX:XX:XX

Bluetooth over uart using hciattach?

I am using QN9021 SoC working in controller mode (BLE Bluetooth core specification v4.0). It supports some standard HCI commands as well as some vendor specific commands. I am trying to attach it in my ubuntu laptop.
The command that i have used is hciattach.
hciattach -s 9600 /dev/ttyUSBx any 9600 noflow nosleep
The hcidump shows while executing sudo hciconfig hci1 up.
HCI sniffer - Bluetooth packet analyzer ver 5.37
device: hci1 snap_len: 1500 filter: 0xffffffffffffffff
> HCI Event: Command Complete (0x0e) plen 12
Read Local Supported Features (0x04|0x0003) ncmd 11
status 0x00
Features: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
> HCI Event: Command Complete (0x0e) plen 12
Read Local Version Information (0x04|0x0001) ncmd 11
status 0x00
HCI Version: 4.0 (0x6) HCI Revision: 0x400
LMP Version: 4.0 (0x6) LMP Subversion: 0x400
Manufacturer: Quintic Corp. (142)
> HCI Event: Command Complete (0x0e) plen 10
Read BD ADDR (0x04|0x0009) ncmd 11
status 0x00 bdaddr 08:7C:BE:3E:34:BB
> HCI Event: Command Complete (0x0e) plen 11
Read Buffer Size (0x04|0x0005) ncmd 11
status 0x00
ACL MTU 0:0 SCO MTU 0:0
> HCI Event: Command Complete (0x0e) plen 4
Read Class of Device (0x03|0x0023) ncmd 11
status 0x01 class 0x000000
Error: Unknown HCI Command
The hciconfig command shows:
hci1: Type: BR/EDR Bus: UART
BD Address: 08:7C:BE:3E:34:BB ACL MTU: 0:0 SCO MTU: 0:0
DOWN
RX bytes:192 acl:0 sco:0 events:15 errors:0
TX bytes:60 acl:0 sco:0 commands:15 errors:0
hci0: Type: BR/EDR Bus: USB
BD Address: C4:8E:8F:66:3B:0E ACL MTU: 820:8 SCO MTU: 255:16
UP RUNNING PSCAN
RX bytes:2457 acl:0 sco:0 events:196 errors:0
TX bytes:24646 acl:0 sco:0 commands:196 errors:0
I would like to know how to prevent the kernel or some bluetooth kernel module to send non-supported HCI commands. Do i need to patch linux kernel source code or write a module for my SoC.
Note:- Changing the SoC or writing firmware for it to support all necessary commands is not possible for this project.
EDIT:
I know the list of HCIcommand supported by my SoC. I am thinking of creating a module to tell the kernel and daemons running to send only supported commands to the SoC. I have a look at linux kernel source (specifically at this hci_core.c). I think modifying it may solve the problem after going through this link. In this link, some developer has provided a patch to support a bluetooth dongle. The patch prevents the hci_core.c file to send a specific HCI command to a dongle of a particular manufacturer.
All i want is suggestion to resolve this problem. Do i need to modify the linux kernel or write a module for my SoC?
NOTE:- The application will run on openwrt linux.
Okay, today we found out the solution:
# hciattach -r /dev/ttyS0 bcsp 115200
Maybe it helps somebody
Helpful Tips:May this link will be helps to you.
I will give two links for more details
and for bluetooth click here
and for kernel configuration click here
I have solved this problem few years back. I was using Linux version 4.4.14 and Bluez stack 5.38. Looks like QN9021 had some bugs. The problem wasn't with Read Class of Device but with the response given by QN9021 for the command: Read Local Supported Features. Since, it's the BLE controller chip, it shouldn't be sending Features: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 as the response to the above command.
The issue is kernel detects it as Classic Bluetooth controller chip and then sends non-supported HCI commands to the chip. The chip should have set the 5th and 6th bits (LMP_NO_BREDR and LMP_LE bits) of the 4th byte in the Features to the command Read Local Supported Features and then kernel detects it as a BLE controller mode chip and won't send any unsupported commands to a ble controller mode chip.
Since, I can't change the firmware of the chip, I had to patch the kernel.
Here are my patches:
*** hci_event.c 2017-02-10 00:05:13.149974000 +0530
--- bluetooth/hci_event.c 2016-06-24 22:48:38.000000000 +0530
***************
*** 588,597 ****
if (rp->status)
return;
-
memcpy(hdev->features, rp->features, 8);
- hdev->features[0][4] |= LMP_NO_BREDR;
- hdev->features[0][4] |= LMP_LE;
/* Adjust default settings according to features
* supported by device. */
--- 588,594 ----
I have also tired the following command but it didn't helped:
hciattach -r /dev/ttyS0 bcsp 115200

Change bluetooth mac address in Raspbian

hi i need to change the bluetooth mac address from my device. i've download the bdaddr utility from here! and i compiled it.
Then...
wget -U "Mozilla" http://www.petrilopia.net/wordpress/wp-content/uploads/bdaddrtar.bz2
...
mv bdaddrtar.bz2 bdaddr.tar.bz2
tar xvjf bdaddr.tar.bz2
cd bdaddr
sudo apt-get install libbluetooth-dev
...
make
...
pi#raspberrypi ~/bdaddr $ hcitool dev
Devices:
hci0 00:1F:81:00:08:30
pi#raspberrypi ~/bdaddr $ hciconfig
hci0: Type: BR/EDR Bus: USB
BD Address: 00:1F:81:00:08:30 ACL MTU: 1021:4 SCO MTU: 180:1
UP RUNNING PSCAN ISCAN
RX bytes:2296 acl:0 sco:0 events:100 errors:0
TX bytes:1081 acl:0 sco:0 commands:98 errors:0
pi#raspberrypi ~/bdaddr $ sudo ./bdaddr -i hci0 00:1F:81:00:48:30
Manufacturer: Cambridge Silicon Radio (10)
Device address: 00:1F:81:00:08:30
New BD address: 00:1F:81:00:48:30
Can't write new address
Why can't i write the address? some idea?
thanks
The bdaddr utility is not part of the bluez package because it uses manufacturer specific code and as such does not work on all devices.
http://ubuntuforums.org/archive/index.php/t-925611.html

How to tell whether bluetooth adapter supports bluetooth LE (4.0)?

I have two Bluetooth adapters, an old internal adapter and a new Bluetooth 4.0 adapter.
I am writing an application that uses a socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP) to connect to a Bluetooth LE device on Linux. When I call connect() with a sockaddr_l2 {.l2_family = AF_BLUETOOTH, .l2_bdaddr = {...}, .l2_cid = L2CAP_CID_ATT, .l2_bdaddr_type = BRADDR_LE_PUBLIC}, connect fails with errno=0x38000000=939524096 Unknown error, because Linux arbitrarily chooses the old adapter that only supports Bluetooth 2.1, not Bluetooth 4.0. The solution is to bind the socket to the bd_addr of the adapter that supports Bluetooth 4.0.
Question: How to tell which adapter is the Bluetooth 4.0 adapter? hciconfig does not tell which one is the one to use; they both say BR/EDR and not LE.
$ hciconfig
hci1: Type: BR/EDR Bus: USB
BD Address: 00:25:00:F6:97:F0 ACL MTU: 1021:5 SCO MTU: 64:1
UP RUNNING PSCAN
RX bytes:1074 acl:0 sco:0 events:56 errors:0
TX bytes:1462 acl:0 sco:0 commands:56 errors:0
hci0: Type: BR/EDR Bus: USB
BD Address: 00:02:72:D6:A0:BF ACL MTU: 1021:8 SCO MTU: 64:1
UP RUNNING PSCAN
RX bytes:146505 acl:328 sco:0 events:4189 errors:0
TX bytes:6213 acl:215 sco:0 commands:83 errors:0
$ modinfo bluetooth | grep ^version:
version: 2.17
$ modinfo btusb | grep ^version:
version: 0.6
$ lsb_release --description
Description: Ubuntu 14.04.1 LTS
$ uname --kernel-release
3.13.0-40-generic
To answer your question there is btmgmt info which will list the HCI version (on the same line as addr), you will have to look into Host Controller Interface Assigned numbers for the meaning of the numbers, version 6 below mean Bluetooth 4.0.
# btmgmt info
hci0: Primary controller
addr 5C:F3:70:XX:XX:XX version 6 manufacturer 15 class 0x1c010c
supported settings: powered connectable fast-connectable discoverable bondable link-security ssp br/edr hs le advertising secure-conn debug-keys privacy configuration static-addr
current settings: powered bondable ssp br/edr le secure-conn
name BlueZ 5.47
short name
If you need to know if the adapter support LE, you will have to look for le in Supported settings: because LE is not mandatory in bluetooth 4.0/4.1.
Try: hciconfig hci[0|1] version

Resources