bluetooth — Wireless Bluetooth Functions
This module provides BLE Bluetooth communication and HID device simulation functionality.
ble Class
The ble class supports BLE communication and HID functionality for both client and host roles. The HID feature follows the BLE HID specification and can simulate wireless Bluetooth HID devices (such as mice, keyboards, game controllers and other human interface devices).
- class bluetooth.ble
BLE Communication
- ble.init(name='mpython')
Initialize the BLE client device. When called without parameters, the default client device name is mpython.
name- Client device name, string type.
- ble.start_advertising()
Start BLE client device advertising. Once started, the device can be discovered by BLE host devices.
- ble.board_send(buf)
Send a byte buffer from the BLE client to the BLE host.
- ble.board_register_output_callback(f)
Register a callback function to receive data from the BLE host. f is the callback function, defined as
f(bytearray).
- ble.stop_advertising()
Stop BLE client device advertising
- ble.hidd_send_consumer(ble.HID_CONSUMER_xxx, True)
Simulate a HID consumer device controller, such as a Bluetooth remote control.
First parameter- Commonly used key IDs for the HID Consumer class. For example, to control TV power on, use ble.HID_CONSUMER_POWER. For specific key constants, see HID Consumer ID Table.Second parameter- Key state: True for pressed, False for released.
TV remote volume up/down example:
# Import Bluetooth ble class from bluetooth import ble import time # BLE initialization ble.init() # Wait for host device connection time.sleep(2) # Press volume up ble.hidd_send_consumer(ble.HID_CONSUMER_VOLUME_UP,True) # Hold pressed state time.sleep(1) # Release key ble.hidd_send_consumer(ble.HID_CONSUMER_VOLUME_UP,False) # Press volume down ble.hidd_send_consumer(ble.HID_CONSUMER_VOLUME_DOWN,True) # Hold pressed state time.sleep(1) # Release key ble.hidd_send_consumer(ble.HID_CONSUMER_VOLUME_DOWN,False)
- ble.hidd_send_keyboard(keys = [key1...], modifier = KEY_MASK_NONE)
Simulate a HID keyboard device.
keys- An array of one or more keys. Members in the array represent pressed keys; to release a key, set the corresponding position value to 0. For specific key constants, see HID Keyboard ID Table.modifier- Keyboard combination keys. Default is KEY_MASK_NONE for single key press. Can be one of the following values. For 2 combination keys, use the|OR operation, e.g., Ctrl + Alt combination is 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
Keyboard example:
# Ctrl + F combination ble.hidd_send_keyboard([ble.HID_KEY_F],ble.KEY_MASK_L_CTRL) # Release keys ble.hidd_send_keyboard([0]) # Ctrl + Alt + Del combination ble.hidd_send_keyboard([ble.HID_KEYPAD_DOT],ble.KEY_MASK_L_CTRL | ble.KEY_MASK_L_ALT ) # Release keys ble.hidd_send_keyboard([0]) # Single key A ble.hidd_send_keyboard([ble.HID_KEY_A]) # Release keys ble.hidd_send_keyboard([0])
- ble.hidd_send_mouse(button=0, x=0, y=0)
Simulate a HID mouse device.
button- Mouse button, can be one of the following values:0- Release buttonble.HID_MOUSE_LEFT- Left mouse buttonble.HID_MOUSE_MIDDLE- Middle mouse buttonble.HID_MOUSE_RIGHT- Right mouse button
x- Relative displacement on mouse x-axis, range -1023 ~ 1023y- Relative displacement on mouse y-axis, range -1023 ~ 1023
Mouse example:
# Press left mouse button ble.hidd_send_mouse(ble.HID_MOUSE_LEFT) # Release mouse button ble.hidd_send_mouse(0) # X-axis displacement ble.hidd_send_mouse(x = 50 )
Constants
HID Consumer
HID Consumer Usage IDs |
Value |
Description |
|---|---|---|
|
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 |
Value |
Description |
|---|---|---|
|
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 |
Value |
Description |
|---|---|---|
|
253 |
Mouse Left |
|
254 |
Mouse Middle |
|
255 |
Mouse Right |
1# MIT license; Copyright (c) 2019 Labplus & mPython Development
2# V1.0 Tangliufeng
3# mPython Board PPT page-turning remote control
4# Usage: short press A - previous page; short press B - next page; long press A - start slideshow; long press B - exit slideshow
5#
6from mpython import *
7from bluetooth import ble
8import time
9# Button status flags
10btn_a_status, btn_b_status = [0, 0]
11
12oled.DispChar('PPT Remote',20,20)
13oled.show()
14
15# BLE initialization
16ble.init('PPT_Remote')
17# Wait for the Bluetooth host
18sleep(2)
19
20while True:
21 # When button A is at low level and the flag is 0
22 if button_a.value() == 0 and btn_a_status == 0:
23 btn_a_time = time.ticks_ms()
24 # Button hold flag
25 a_hold_flag = False
26 # While the button is held down
27 while not button_a.value():
28 # When held for 2 seconds
29 if time.ticks_diff(time.ticks_ms(), btn_a_time) > 2000:
30 # Send F5 to start the slideshow
31 ble.hidd_send_keyboard([ble.HID_KEY_F5])
32 print("Pressed F5")
33 # Set the button hold flag to 1
34 a_hold_flag = True
35 break
36 # If the button was not held down
37 if a_hold_flag is False:
38 # Send LeftArrow for the previous slide
39 ble.hidd_send_keyboard([ble.HID_KEY_LEFT_ARROW])
40 print("Pressed LeftArrow")
41 # Set the button status to 1
42 btn_a_status = 1
43
44 # When button A is at high level and the flag is 1, release the button
45 elif button_a.value() == 1 and btn_a_status == 1:
46 print("Relesed Button A")
47 ble.hidd_send_keyboard([0])
48 btn_a_status = 0
49
50 if button_b.value() == 0 and btn_b_status == 0:
51 btn_b_time = time.ticks_ms()
52 b_hold_flag = False
53
54 while not button_b.value():
55 if time.ticks_diff(time.ticks_ms(), btn_b_time) > 2000:
56 # Send ESC to exit the slideshow
57 ble.hidd_send_keyboard([ble.HID_KEY_ESCAPE])
58 print("Pressed ESC")
59 b_hold_flag = True
60 break
61
62 if b_hold_flag is False:
63 # Send RightArrow for the next slide
64 ble.hidd_send_keyboard([ble.HID_KEY_RIGHT_ARROW])
65 print("Pressed RightArrow")
66
67 btn_b_status = 1
68
69 elif button_b.value() == 1 and btn_b_status == 1:
70 print("Relesed Button B")
71 ble.hidd_send_keyboard([0])
72 btn_b_status = 0