I'm attempting to connect a bluez peripheral to an iOS device; it's not working.
Please find below logs on peripheral side. I'm advertising using hciconfig hci0 leadv but I don't see any "Connect" option on Scanner applications on phone. Also bluez-4.101 doesn't have hciconfig hci0 leadv0 option to make it connectable.
With the same device acting as a Central I am able to make LE connections with other BLE devices, so that confirms kernel LE support and device is fine, only some bluez issues I assume are there.
> HCI Event: LE Meta Event (0x3e) plen 19
LE Connection Complete
status 0x00 handle 1025, role slave
bdaddr 67:5D:F6:87:3D:2C (Random)
> ACL data: handle 1025 flags 0x02 dlen 7
ATT: MTU req (0x02)
client rx mtu 158
> ACL data: handle 1025 flags 0x02 dlen 27
> ACL data: handle 1025 flags 0x01 dlen 27
> ACL data: handle 1025 flags 0x01 dlen 9
L2CAP(d): cid 0x003a len 59 [psm 0]
0000: .9......com.appl
0010: e.BT.TS."....com
0020: .apple.BTLEServe
0030: r.classic..
> ACL data: handle 1025 flags 0x02 dlen 11
L2CAP(d): cid 0x003a len 7 [psm 0]
0000: ......
> HCI Event: Disconn Complete (0x05) plen 4
status 0x00 handle 1025 reason 0x13
Reason: Remote User Terminated Connection
How can I get this peripheral to connect?
Upgrading to the latest BlueZ version will solve the GATT related issues.
Quote from the linked page:
The 2.x , 3.x and 4.x series of libraries and packages are deprecated and not supported anymore by BlueZ developers. If you are using them please update to the 5.x series. The download link is only provided for reference.
Related
I followed the tutorial from The Bluetooth Technology for Linux Developers Study Guide (I skipped the 5th guide because I'm doing a gatt server), and everything worked fine until I got to Handling Characteristic Writes.
(Also, I am using a Raspberry PI 4 Model B)
In nRF Connect I have the following in the logs:
Writing request to characteristic e95d93ee-251d-470a-a062-fa1922dfa9a8
Data written to e95d93ee-251d-470a-a062-fa1922dfa9a8, value: (0x)68-65-6C-6C-6F, "hello"
"hello" sent
so the data should have been sent.
sudo btmon gives the following:
> ACL Data RX: Handle 64 flags 0x02 dlen 14 #129 [hci0] 82.584227
ATT: Prepare Write Request (0x16) len 9
Handle: 0x0019
Offset: 0x0000
Data: 68656c6c6f
< ACL Data TX: Handle 64 flags 0x00 dlen 14 #130 [hci0] 82.585053
ATT: Prepare Write Response (0x17) len 9
Handle: 0x0019
Offset: 0x0000
Data: 68656c6c6f
> HCI Event: Number of Completed Packets (0x13) plen 5 #131 [hci0] 82.814773
Num handles: 1
Handle: 64
Count: 1
so the data was indeed received. Though I don't understand why it says Prepare Write Request instead of just Write Request.
bluetoothd with -nd flag and tail -f /var/log/syslog gives the following:
Jul 28 09:20:45 raspberrypi bluetoothd[820]: bluetoothd[820]: src/device.c:gatt_debug() (chan 0x1bb9dd0) ATT PDU received: 0x16
Jul 28 09:20:45 raspberrypi bluetoothd[820]: bluetoothd[820]: src/device.c:gatt_debug() Prep Write Req - handle: 0x0019
Jul 28 09:20:45 raspberrypi bluetoothd[820]: src/device.c:gatt_debug() (chan 0x1bb9dd0) ATT PDU received: 0x16
Jul 28 09:20:45 raspberrypi bluetoothd[820]: src/device.c:gatt_debug() Prep Write Req - handle: 0x0019
but there is no reaction using sudo dbus-monitor --system, and nothing either in the program output terminal.
Do you have any Idea where the problem might come from?
(I tried reinstalling nRF Connect twice already, so I think the problem is from somewhere in the Raspberry, but is it from bluez? from dbus? or somewhere else?)
After reinstalling bluez, and not solving anything, I tried with an old samsung tablet, and it works. So it appears my assumption was wrong and the problem could be from my phone.
I am broadcasting a BLE advertisement using the following command in linux :
sudo hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 63 6F 3F 8F 64 91 4B EE 95 F7 D8 CC 64 A8 63 B5 00 00 00 00 C8
The BLE devce is visible, but don't allow any connection (since it follows the iBeacon specs, I think).
So my question is, is there a way to create with BlueZ a BLE device that allows not just one, but several connections ?
I read somewhere that A BLE device should be able to allow up to 20 connections, against 7 for a classic bluetooth.
Thank you very much
This depends on your hardware and the Bluetooth version that it supports. With Bluetooth v4.1, all restrictions have been removed and your device can be a peripheral connected to multiple centrals. This is mentioned at this link:-
Beginning with version 4.1 of the specification, any restrictions on
role combinations have been removed, and the following are all
possible:
A device can act as a central and a peripheral at the same time.
A central can be connected to multiple peripherals.
A peripheral can be connected to multiple centrals.
Previous versions of the specification limited the peripheral to a
single central connection (although not conversely) and limited the
role combinations.
You can find out the Bluetooth version of your device via hciconfig -a:-
hci0: Type: Primary Bus: USB
BD Address: 00:11:22:33:44:55 ACL MTU: 310:10 SCO MTU: 64:8
UP RUNNING
RX bytes:736 acl:0 sco:0 events:57 errors:0
TX bytes:5366 acl:0 sco:0 commands:57 errors:0
Features: 0xff 0xff 0xcf 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
Name: 'uknown'
Class: 0x1c0000
Service Classes: Rendering, Capturing, Object Transfer
Device Class: Miscellaneous,
**HCI Version: 4.0 (0x6) Revision: 0x22bb**
LMP Version: 4.0 (0x6) Subversion: 0x22bb
Manufacturer: Cambridge Silicon Radio (10)
I would recommend using the bluetoothctl utility on Linux to advertise, instead of doing it through hcitool given that hcitool is deprecated and is also not very user friendly. More information on using bluetoothctl to advertise and accept incoming connections can be found here:-
BlueZ: How to set up a GATT server from the command line
Need to determine whether the central and peripheral devices (both running Bluetooth 5.0) are communicating via the faster LE 2M PHY bit rate.
The command I am sending via the hcitool:
sudo hcitool cmd 8 30 40 0
The response received:
01 30 20 01
The btmon output:
# RAW Open: hcitool (privileged) version 2.22 {0x0005} 3503.199467
# RAW Close: hcitool {0x0005} 3503.199506
# RAW Open: hcitool (privileged) version 2.22 {0x0005} [hci0] 3503.199594
< HCI Command: LE Read PHY (0x08|0x0030) plen 2 #11120 [hci0] 3503.200023
Handle: 64
> HCI Event: Command Complete (0x0e) plen 4 #11121 [hci0] 3503.200353
LE Read PHY (0x08|0x0030) ncmd 1
Status: Unknown HCI Command (0x01)
# RAW Close: hcitool
For some reason I am getting the Unknown HCI Command error when running this command on the RPi 4 which is set as the peripheral via bleno.
Am I doing something wrong? Are there preliminary steps I need to take before I can read and set the symbol rate?
More info:
< HCI Command: Read Local Ve.. (0x04|0x0001) plen 0 #3 [hci0] 42.465863
> HCI Event: Command Complete (0x0e) plen 12 #4 [hci0] 42.466325
Read Local Version Information (0x04|0x0001) ncmd 1
Status: Success (0x00)
HCI version: Bluetooth 5.0 (0x09) - Revision 315 (0x013b)
LMP version: Bluetooth 5.0 (0x09) - Subversion 24857 (0x6119)
Manufacturer: Cypress Semiconductor Corporation (305)
# RAW Close: hcitool {0x0003} [hci0] 42.466507
Currently using on OH1 Heart rate sensor (tiny.cc/mom03y)
I'm looking to subscribe to HR notification using bluepy. I have got the notifications working but the OH1 device disconnects after about 20-30secs in both bluepy and gatttool (remote user terminated) but not in bluetoothctl.
Looking for a reason why the connection stays alive in bluetoothctl and not in bluepy or gatttool, code and hcidump below, using bluez 5.50 and bluepy 1.30 on rasbian 4.14.
Bluepy
#packet count
packets = 0
class hrCallback(btle.DefaultDelegate):
def __init__(self):
btle.DefaultDelegate.__init__(self)
def handleNotification(self, cHandle, data):
global packets
packets += 1
print("packet: %s Handle: %s HR (bpm): %s " % (packets, cHandle, data[1]))
#connect to OH1
mac = "a0:9e:1a:4f:ef:8b"
oh1 = btle.Peripheral( mac )
oh1.setDelegate( hrCallback() )
#start hr notification
oh1.writeCharacteristic(38, b"\x01\x00", True)
#listen for notifications
while True:
try:
if oh1.waitForNotifications(1.0):
continue
except btle.BTLEDisconnectError:
pass
hcidump
> HCI Event: Command Complete (0x0e) plen 4
LE Set Scan Parameters (0x08|0x000b) ncmd 1
status 0x00
> HCI Event: Command Complete (0x0e) plen 4
LE Set Scan Enable (0x08|0x000c) ncmd 1
status 0x00
> HCI Event: Command Complete (0x0e) plen 4
LE Set Scan Enable (0x08|0x000c) ncmd 1
status 0x00
> HCI Event: Command Status (0x0f) plen 4
LE Create Connection (0x08|0x000d) status 0x00 ncmd 1
> HCI Event: Command Status (0x0f) plen 4
LE Read Remote Used Features (0x08|0x0016) status 0x00 ncmd 1
> HCI Event: Disconn Complete (0x05) plen 4
status 0x00 handle 64 reason 0x13
Reason: Remote User Terminated Connection
Which version of BlueZ are you using? several tools (e.g. gatttool, hcitool, hciconfig) was deprecated and replaced by bluetoothctl and btmgmt, and the recommendation from the BlueZ team is to use the new tools instead. Please have a look at the following link:-
Deprecated BlueZ Tools
The difference between the new tools and the old tools is that the old tools were able of interfacing directly with the kernel, whereas the new tools perform operations by interfacing with D-Bus instead.
Therefore, the recommendation is always to use bluetoothctl as old tools are not maintained and this is likely why you are seeing the issue.
This problem only occurs after I updated to Bluez-5.20+ (or maybe earlier)
I compiled Bluez with maintainer mode and experimental to get gatt-example included in the service list, and enabled the advertisement via hciconfig.
Every time I connect with iOS (with app like LightBlue, etc.), I got disconnected after this event :
> HCI Event: LE Meta Event (0x3e) plen 19 [hci0] 48449.282018
LE Connection Complete (0x01)
Status: Success (0x00)
Handle: 64
Role: Slave (0x01)
Peer address type: Random (0x01)
Peer address: 7A:A0:D6:50:69:CE (Resolvable)
Connection interval: 30.00 msec (0x0018)
Connection latency: 0.00 msec (0x0000)
Supervision timeout: 720 msec (0x0048)
Master clock accuracy: 0x05
# Device Connected: 7A:A0:D6:50:69:CE (2) flags 0x0000
> HCI Event: LE Meta Event (0x3e) plen 13 [hci0] 48449.356021
LE Long Term Key Request (0x05)
Handle: 64
Random number: be7ee4252ef5d67f
Encryption diversifier: 0x57f5
< HCI Command: LE Long Term Key Request Reply (0x08|0x001a) plen 18 [hci0] 48449.356058
Handle: 64
Long term key: 696469e1eaa559f7707643bf410ab39b
> HCI Event: Command Complete (0x0e) plen 6 [hci0] 48449.357019
LE Long Term Key Request Reply (0x08|0x001a) ncmd 1
Status: Success (0x00)
Handle: 64
> HCI Event: Encryption Change (0x08) plen 4 [hci0] 48449.506019
Status: Success (0x00)
Handle: 64
Encryption: Enabled with AES-CCM (0x01)
> ACL Data RX: Handle 64 flags 0x02 dlen 7 [hci0] 48449.594893
ATT: Exchange MTU Request (0x02) len 2
Client RX MTU: 158
< HCI Command: Disconnect (0x01|0x0006) plen 3 [hci0] 48453.289196
Handle: 64
Reason: Remote User Terminated Connection (0x13)
> HCI Event: Command Status (0x0f) plen 4 [hci0] 48453.289992
Disconnect (0x01|0x0006) ncmd 1
Status: Success (0x00)
> HCI Event: Disconnect Complete (0x05) plen 4 [hci0] 48453.345991
Status: Success (0x00)
Handle: 64
Reason: Connection Terminated By Local Host (0x16)
And bluetoothd debug log :
bluetoothd[3381]: src/adapter.c:resume_discovery()
bluetoothd[3381]: src/adapter.c:connected_callback() hci0 device 7A:A0:D6:50:69:CE connected eir_len 0
bluetoothd[3381]: src/device.c:device_create() dst 7A:A0:D6:50:69:CE
bluetoothd[3381]: src/device.c:device_new() address 7A:A0:D6:50:69:CE
bluetoothd[3381]: src/device.c:device_new() Creating device /org/bluez/hci0/dev_7A_A0_D6_50_69_CE
bluetoothd[3381]: src/attrib-server.c:connect_event()
bluetoothd[3381]: src/device.c:device_create() dst 7A:A0:D6:50:69:CE
bluetoothd[3381]: src/device.c:device_new() address 7A:A0:D6:50:69:CE
bluetoothd[3381]: src/device.c:device_new() Creating device /org/bluez/hci0/dev_7A_A0_D6_50_69_CE
bluetoothd[3381]: Unable to register device interface for 7A:A0:D6:50:69:CE
bluetoothd[3381]: src/device.c:device_free() 0x11dd220
bluetoothd[3381]: src/adapter.c:dev_disconnected() Device 7A:A0:D6:50:69:CE disconnected, reason 2
bluetoothd[3381]: src/adapter.c:adapter_remove_connection()
bluetoothd[3381]: src/adapter.c:adapter_remove_connection() Removing temporary device /org/bluez/hci0/dev_7A_A0_D6_50_69_CE
bluetoothd[3381]: src/device.c:device_remove() Removing device /org/bluez/hci0/dev_7A_A0_D6_50_69_CE
bluetoothd[3381]: src/device.c:btd_device_unref() Freeing device /org/bluez/hci0/dev_7A_A0_D6_50_69_CE
bluetoothd[3381]: src/device.c:device_free() 0x11e9720
bluetoothd[3381]: plugins/policy.c:disconnect_cb() reason 2
Any possible explanation to this?