forked from vChavezB/bleak-bumble
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_change_connection_phy.py
More file actions
122 lines (106 loc) · 4.07 KB
/
Copy pathclient_change_connection_phy.py
File metadata and controls
122 lines (106 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
"""This example demonstrates how to get/set connection PHYs.
It was tested with a pair of RTL8761B usb dongles
and was confirmed to work by using nRF BLE sniffer.
For details, see `client_change_connection_phy.md`.
"""
import asyncio
from bumble import data_types
from bumble.core import AdvertisingData
from bumble.device import Device, AdvertisingParameters
from bumble.hci import Address, HCI_LE_1M_PHY, HCI_LE_2M_PHY, HCI_LE_CODED_PHY
from bleak import BleakClient, BleakScanner
from bleak_bumble import start_transport
from bleak_bumble.client import BleakClientBumble
from bleak_bumble.scanner import BleakScannerBumble
# True: LE extendend advertising ('1m', 'coded') / False: LE legacy advertising ('1m').
LE_EXTENDED_ADV = True
async def main():
transport = await start_transport("usb:0", True)
bumble_peripheral = Device.with_hci(
name="Bumble",
#address=Address.generate_static_address(),
address=Address('F1:F2:F3:F4:F5:F6'),
hci_source=transport.source,
hci_sink=transport.sink,
)
adv_data = [
data_types.Flags(
AdvertisingData.Flags.LE_GENERAL_DISCOVERABLE_MODE
| AdvertisingData.Flags.BR_EDR_NOT_SUPPORTED
),
data_types.CompleteLocalName(bumble_peripheral.name),
]
await bumble_peripheral.power_on()
if not LE_EXTENDED_ADV:
bumble_peripheral.advertising_interval_min = bumble_peripheral.advertising_interval_max = 200
bumble_peripheral.advertising_data = bytes(AdvertisingData(adv_data))
await bumble_peripheral.start_advertising()
else:
advertising_set = await bumble_peripheral.create_advertising_set(
advertising_parameters = AdvertisingParameters(
primary_advertising_interval_min = 200,
primary_advertising_interval_max = 200,
primary_advertising_phy = HCI_LE_1M_PHY,
secondary_advertising_phy = HCI_LE_CODED_PHY,
# The following options are ignored unless
# HCI_LE_SET_EXTENDED_ADVERTISING_PARAMETERS_V2
# is supported by both the controller and the host (Bumble).
#primary_advertising_phy_options = 0,
#secondary_advertising_phy_options = 0,
),
advertising_data = bytes(AdvertisingData(adv_data)),
)
#await asyncio.sleep(10)
device = await BleakScanner.find_device_by_name(
bumble_peripheral.name,
backend=BleakScannerBumble,
cfg="usb:1",
host_mode=True,
)
async with BleakClient(
device,
backend=BleakClientBumble,
cfg="usb:1",
host_mode=True,
phys="1m,2m,coded",
) as client:
if client.is_connected:
print('Connected.')
backend = client._backend
print(f'phys: {backend._phys}')
#print(f'preferences: {backend._connection_parameters_preferences}')
phy1 = await backend._connection.get_phy()
print(phy1)
await backend._connection.set_phy(
tx_phys=[HCI_LE_CODED_PHY],
rx_phys=[HCI_LE_CODED_PHY],
phy_options=0,
)
await asyncio.sleep(1)
phy2 = await backend._connection.get_phy()
print(phy2)
await backend._connection.set_phy(
tx_phys=[HCI_LE_2M_PHY],
rx_phys=[HCI_LE_2M_PHY],
)
await asyncio.sleep(1)
phy3 = await backend._connection.get_phy()
print(phy3)
await backend._connection.set_phy(
tx_phys=[HCI_LE_1M_PHY],
rx_phys=[HCI_LE_1M_PHY],
)
await asyncio.sleep(1)
phy4 = await backend._connection.get_phy()
print(phy4)
else:
print('Failed.')
print('Disconnecting...')
await bumble_peripheral.power_off()
await transport.close()
if __name__ == "__main__":
try:
asyncio.run(main())
finally:
asyncio.new_event_loop() # Clear retained state.