Skip to content

Commit 0445d12

Browse files
committed
Initial BLE HID; works everywhere but iOS
1 parent eb5ae68 commit 0445d12

File tree

4 files changed

+443
-272
lines changed

4 files changed

+443
-272
lines changed

adafruit_ble/advertising.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,9 @@ class Advertisement:
179179
consisting of an advertising data packet and an optional scan response packet.
180180
181181
:param int flags: advertising flags. Default is general discovery, and BLE only (not classic)
182+
:param int appearance: If not None, add appearance value to advertisement.
182183
"""
183-
def __init__(self, flags=None, tx_power=None):
184+
def __init__(self, flags=None, tx_power=None, appearance=None):
184185
self._packet = AdvertisingPacket()
185186
self._scan_response_packet = None
186187
if flags:
@@ -190,6 +191,8 @@ def __init__(self, flags=None, tx_power=None):
190191

191192
if tx_power is not None:
192193
self._packet.add_tx_power(tx_power)
194+
if appearance is not None:
195+
self._packet.add_appearance(appearance)
193196

194197
def add_name(self, name):
195198
"""Add name to advertisement. If it doesn't fit, add truncated name to packet,
@@ -246,10 +249,11 @@ class ServerAdvertisement(Advertisement):
246249
247250
:param Peripheral peripheral: the Peripheral to advertise. Use its services and name.
248251
:param int tx_power: transmit power in dBm at 0 meters (8 bit signed value). Default 0 dBm
252+
:param int appearance: If not None, add appearance value to advertisement.
249253
"""
250254

251-
def __init__(self, peripheral, *, tx_power=0):
252-
super().__init__()
255+
def __init__(self, peripheral, *, tx_power=0, appearance=None):
256+
super().__init__(tx_power=tx_power, appearance=appearance)
253257
uuids = [service.uuid for service in peripheral.services if not service.secondary]
254258
self.add_uuids(uuids,
255259
AdvertisingPacket.ALL_16_BIT_SERVICE_UUIDS,
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2019 Dan Halbert for Adafruit Industries
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
`adafruit_ble.device_information`
24+
====================================================
25+
26+
Device Information Service (DIS)
27+
28+
* Author(s): Dan Halbert for Adafruit Industries
29+
30+
"""
31+
from bleio import Attribute, Characteristic, Service, UUID
32+
33+
# pylint: disable=invalid-name
34+
def DeviceInformationService(*, model_number=None, serial_number=None, firmware_revision=None,
35+
hardware_revision='', software_revision='', manufacturer=''):
36+
"""
37+
Set up a Service with fixed Device Information Service charcteristics.
38+
All values are optional.
39+
40+
Note that this is a pseudo class: It returns a Service, but is not a subclass of Service,
41+
because Service is a native class and doesn't handle subclassing.
42+
43+
:param str model_number: Device model number. If None use `sys.platform`.
44+
:param str serial_number: Device serial number. If None use a hex representation of
45+
``microcontroller.cpu.id``.
46+
:param str firmware_revision: Device firmware revision. If None use ``os.uname().version``.
47+
:param str hardware_revision: Device hardware revision.
48+
:param str software_revision: Device software revision.
49+
:param str manufacturer: Device manufacturer name
50+
51+
Example::
52+
53+
dis = DeviceInformationService(software_revision="1.2.4", manufacturer="Acme Corp")
54+
"""
55+
56+
# Avoid creating constants with names if not necessary. Just takes up space.
57+
# Device Information Service UUID = 0x180A
58+
# Module Number UUID = 0x2A24
59+
# Serial Number UUID = 0x2A25
60+
# Firmware Revision UUID = 0x2A26
61+
# Hardware Revision UUID = 0x2A27
62+
# Software Revision UUID = 0x2A28
63+
# Manufacturer Name UUID = 0x2A29
64+
65+
characteristics = []
66+
# Values must correspond to UUID numbers.
67+
68+
if model_number is None:
69+
import sys
70+
model_number = sys.platform
71+
if serial_number is None:
72+
import microcontroller
73+
import binascii
74+
serial_number = binascii.hexlify(microcontroller.cpu.uid).decode('utf-8') # pylint: disable=no-member
75+
76+
if firmware_revision is None:
77+
import os
78+
firmware_revision = os.uname().version
79+
80+
for uuid_num, string in zip(
81+
range(0x2A24, 0x2A29+1),
82+
(model_number, serial_number,
83+
firmware_revision, hardware_revision, software_revision,
84+
manufacturer)):
85+
86+
char = Characteristic(UUID(uuid_num), properties=Characteristic.READ,
87+
read_perm=Attribute.OPEN, write_perm=Attribute.NO_ACCESS,
88+
fixed_length=True, max_length=len(string))
89+
char.value = string
90+
characteristics.append(char)
91+
92+
return Service(UUID(0x180A), characteristics)

adafruit_ble/hid.py

Lines changed: 0 additions & 269 deletions
This file was deleted.

0 commit comments

Comments
 (0)