bluetooth
— 提供无线蓝牙功能
该模块提供ble蓝牙通讯、HID模拟设备功能。
ble类
ble类, 支持客户端与主机端的BLE通讯及HID功能。HID功能,遵循BLE HID规范,可实现模拟无线蓝牙HID设备(如鼠标,键盘,游戏手柄等人机交互设备)。
- class bluetooth.ble
BLE 通讯
- ble.init(name='mpython')
BLE客户端设备初始化。当不带参数时,默认客户端设备名为 mpython 。
name
- 客户端设备名称,字符串类型。
- ble.start_advertising()
BLE客户端设备开启广播。开启后,可被BLE主机设置搜索。
- ble.board_send(buf)
BLE客户端向BLE主机端发送字节缓存
- ble.board_register_output_callback(f)
接收BLE主机端的数据的回调函数, f 为回调函数,函数定义如下,
f(bytearray)
。
- ble.stop_advertising()
BLE客户端设备关闭广播
- ble.hidd_send_consumer(ble.HID_CONSUMER_xxx, True)
模拟HID消费类设备的控制器。如,蓝牙遥控器。
第一参数
- HID Consumer类的常用的按键ID。如你想控制电视机开机,则用 ble.HID_CONSUMER_POWER 。具体的按键常量详见 HID Consumer ID表 。第二参数
- 按键状态, True 为按下, False 为松开。
电视遥控音量加减的示例:
# 导入蓝牙ble类 from bluetooth import ble import time # ble初始化 ble.init() # 等待主机设备连接 time.sleep(2) # 按下音量+ ble.hidd_send_consumer(ble.HID_CONSUMER_VOLUME_UP,True) # 按下保持状态 time.sleep(1) # 释放按键 ble.hidd_send_consumer(ble.HID_CONSUMER_VOLUME_UP,False) # 按下音量- ble.hidd_send_consumer(ble.HID_CONSUMER_VOLUME_DOWN,True) # 按下保持状态 time.sleep(1) # 释放按键 ble.hidd_send_consumer(ble.HID_CONSUMER_VOLUME_DOWN,False)
- ble.hidd_send_keyboard(keys = [key1...], modifier = KEY_MASK_NONE)
模拟HID键盘设备。
keys
- 一个或多个按键的数组类型。当数组内成员表示为按下的按键,如要释放按键只需将数组内对应位置的值赋值为0。具体的按键常量详见 HID Keyboard ID表 。modifier
- 键盘组合按键,默认为 KEY_MASK_NONE 表示为单按键 ,可以为以下值。如要用到2个组合按键,可使用|
或逻辑运算,像 Ctrl + Alt 组合按键时为 KEY_MASK_L_CTRL | KEY_MASK_L_ALTKEY_MASK_NONE - 0
KEY_MASK_L_CTRL - 1
KEY_MASK_L_SHIFT - 2
KEY_MASK_L_ALT - 4
KEY_MASK_L_GUI - 8
KEY_MASK_R_CTRL - 16
KEY_MASK_R_SHIFT - 32
KEY_MASK_R_ALT - 64
KEY_MASK_R_GUI - 128
键盘示例:
# Ctrl + F组合按键 ble.hidd_send_keyboard([ble.HID_KEY_F],ble.KEY_MASK_L_CTRL) # 释放按键 ble.hidd_send_keyboard([0]) # Ctrl + Alt + Del 组合按键 ble.hidd_send_keyboard([ble.HID_KEYPAD_DOT],ble.KEY_MASK_L_CTRL | ble.KEY_MASK_L_ALT ) # 释放按键 ble.hidd_send_keyboard([0]) # 单按键 A ble.hidd_send_keyboard([ble.HID_KEY_A]) # 释放按键 ble.hidd_send_keyboard([0])
- ble.hidd_send_mouse(button=0, x=0, y=0)
模拟HID鼠标设备。
button
- 鼠标按键, 可以为以下值:0
- 释放按键ble.HID_MOUSE_LEFT
- 鼠标左键ble.HID_MOUSE_MIDDLE
- 鼠标中键ble.HID_MOUSE_RIGHT
- 鼠标右键
x
- 鼠标坐标x轴的相对位移,范围-1023 ~ 1023y
- 鼠标坐标y轴的相对位移,范围-1023 ~ 1023
鼠标示例:
# 按下鼠标左键 ble.hidd_send_mouse(ble.HID_MOUSE_LEFT) # 释放鼠标按键 ble.hidd_send_mouse(0) # 坐标x轴位移 ble.hidd_send_mouse(x = 50 )
常量
HID Consumer
HID Consumer Usage IDs |
数值 |
定义 |
---|---|---|
|
48 |
Power |
|
49 |
Reset |
|
50 |
Sleep |
|
64 |
Menu |
|
128 |
Selection |
|
129 |
Assign Selection |
|
130 |
Mode Step |
|
131 |
Recall Last |
|
148 |
Quit |
|
149 |
Help |
|
156 |
Channel Increment |
|
157 |
Channel Decrement |
|
176 |
Play |
|
177 |
Pause |
|
178 |
Record |
|
179 |
Fast Forward |
|
180 |
Rewind |
|
181 |
Scan Next Track |
|
182 |
Scan Previous Track |
|
183 |
Stop |
|
184 |
Eject |
|
185 |
Random Play |
|
186 |
Select Disk |
|
187 |
Enter Disc |
|
188 |
Repeat |
|
204 |
Stop/Eject |
|
205 |
Play/Pause |
|
206 |
Play/Skip |
|
224 |
Volume |
|
225 |
Balance |
|
226 |
Mute |
|
227 |
Bass |
|
233 |
Volume Increment |
|
234 |
Volume Decrement |
HID Keyboard
HID Keyboard Usage IDs |
数值 |
定义 |
---|---|---|
|
4 |
Keyboard A and a |
|
5 |
Keyboard B and b |
|
6 |
Keyboard C and c |
|
7 |
Keyboard D and d |
|
8 |
Keyboard E and e |
|
9 |
Keyboard F and f |
|
10 |
Keyboard G and g |
|
11 |
Keyboard H and h |
|
12 |
Keyboard I and i |
|
13 |
Keyboard J and j |
|
14 |
Keyboard K and k |
|
15 |
Keyboard L and l |
|
16 |
Keyboard M and m |
|
17 |
Keyboard N and n |
|
18 |
Keyboard O and o |
|
19 |
Keyboard P and p |
|
20 |
Keyboard Q and q |
|
21 |
Keyboard R and r |
|
22 |
Keyboard S and s |
|
23 |
Keyboard T and t |
|
24 |
Keyboard U and u |
|
25 |
Keyboard V and v |
|
26 |
Keyboard W and w |
|
27 |
Keyboard X and x |
|
28 |
Keyboard Y and y |
|
29 |
Keyboard Z and z |
|
30 |
Keyboard 1 |
|
31 |
Keyboard 2 |
|
32 |
Keyboard 3 |
|
33 |
Keyboard 4 |
|
34 |
Keyboard 5 |
|
35 |
Keyboard 6 |
|
36 |
Keyboard 7 |
|
37 |
Keyboard 8 |
|
38 |
Keyboard 9 |
|
39 |
Keyboard 0 |
|
40 |
Keyboard Return (ENTER) |
|
41 |
Keyboard ESCAPE |
|
42 |
Keyboard DELETE (Backspace) |
|
43 |
Keyboard Tab |
|
44 |
Keyboard Spacebar |
|
45 |
Keyboard - and (underscore) |
|
46 |
Keyboard = and + |
|
47 |
Keyboard [ and { |
|
48 |
Keyboard ] and } |
|
49 |
Keyboard and | |
|
51 |
Keyboard ; and : |
|
52 |
Keyboard ‘ and “ |
|
53 |
Keyboard Grave Accent and Tilde |
|
54 |
Keyboard , and < |
|
55 |
Keyboard . and > |
|
56 |
Keyboard / and ? |
|
57 |
Keyboard Caps Lock |
|
58 |
Keyboard F1 |
|
59 |
Keyboard F2 |
|
60 |
Keyboard F3 |
|
61 |
Keyboard F4 |
|
62 |
Keyboard F5 |
|
63 |
Keyboard F6 |
|
64 |
Keyboard F7 |
|
65 |
Keyboard F8 |
|
66 |
Keyboard F9 |
|
67 |
Keyboard F10 |
|
68 |
Keyboard F11 |
|
69 |
Keyboard F12 |
|
70 |
Keyboard Print Screen |
|
71 |
Keyboard Scroll Lock |
|
72 |
Keyboard Pause |
|
73 |
Keyboard Insert |
|
74 |
Keyboard Home |
|
75 |
Keyboard PageUp |
|
76 |
Keyboard Delete Forward |
|
77 |
Keyboard End |
|
78 |
Keyboard PageDown |
|
79 |
Keyboard RightArrow |
|
80 |
Keyboard LeftArrow |
|
81 |
Keyboard DownArrow |
|
82 |
Keyboard UpArrow |
|
83 |
Keypad Num Lock and Clear |
|
84 |
Keypad / |
|
85 |
Keypad * |
|
86 |
Keypad - |
|
87 |
Keypad +- |
|
88 |
Keypad ENTER |
|
89 |
Keypad 1 and End |
|
90 |
Keypad 2 and Down Arrow |
|
91 |
Keypad 3 and PageDn |
|
92 |
Keypad 4 and Lfet Arrow |
|
93 |
Keypad 5 |
|
94 |
Keypad 6 and Right Arrow |
|
95 |
Keypad 7 and Home |
|
96 |
Keypad 8 and Up Arrow |
|
97 |
Keypad 9 and PageUp |
|
98 |
Keypad 0 and Insert |
|
99 |
Keypad . and Delete |
|
127 |
Keyboard Mute |
|
128 |
Keyboard Volume up |
|
129 |
Keyboard Volume down |
HID Mouse
HID Mouse Usage IDs |
数值 |
定义 |
---|---|---|
|
253 |
Mouse Left |
|
254 |
Mouse Middle |
|
255 |
Mouse Right |