Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions adafruit_ble/advertising.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ class Advertisement:
consisting of an advertising data packet and an optional scan response packet.

:param int flags: advertising flags. Default is general discovery, and BLE only (not classic)
:param int appearance: If not None, add appearance value to advertisement.
"""
def __init__(self, flags=None, tx_power=None):
def __init__(self, flags=None, tx_power=None, appearance=None):
self._packet = AdvertisingPacket()
self._scan_response_packet = None
if flags:
Expand All @@ -190,6 +191,8 @@ def __init__(self, flags=None, tx_power=None):

if tx_power is not None:
self._packet.add_tx_power(tx_power)
if appearance is not None:
self._packet.add_appearance(appearance)

def add_name(self, name):
"""Add name to advertisement. If it doesn't fit, add truncated name to packet,
Expand Down Expand Up @@ -246,10 +249,11 @@ class ServerAdvertisement(Advertisement):

:param Peripheral peripheral: the Peripheral to advertise. Use its services and name.
:param int tx_power: transmit power in dBm at 0 meters (8 bit signed value). Default 0 dBm
:param int appearance: If not None, add appearance value to advertisement.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is appearance?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""

def __init__(self, peripheral, *, tx_power=0):
super().__init__()
def __init__(self, peripheral, *, tx_power=0, appearance=None):
super().__init__(tx_power=tx_power, appearance=appearance)
uuids = [service.uuid for service in peripheral.services if not service.secondary]
self.add_uuids(uuids,
AdvertisingPacket.ALL_16_BIT_SERVICE_UUIDS,
Expand Down
2 changes: 1 addition & 1 deletion adafruit_ble/beacon.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, advertising_packet):

:param AdvertisingPacket advertising_packet
"""
self._broadcaster = bleio.Peripheral(name=None)
self._broadcaster = bleio.Peripheral()
self._advertising_packet = advertising_packet

def start(self, interval=1.0):
Expand Down
2 changes: 1 addition & 1 deletion adafruit_ble/current_time_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class CurrentTimeClient:
LOCAL_TIME_INFORMATION_UUID = UUID(0x2A0F)

def __init__(self, name=None, tx_power=0):
self._periph = Peripheral(name=name)
self._periph = Peripheral(name)
self._advertisement = SolicitationAdvertisement(self._periph.name,
(self.CTS_UUID,), tx_power=tx_power)
self._current_time_char = self._local_time_char = None
Expand Down
96 changes: 96 additions & 0 deletions adafruit_ble/device_information_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# The MIT License (MIT)
#
# Copyright (c) 2019 Dan Halbert for Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
`adafruit_ble.device_information`
====================================================

Device Information Service (DIS)

* Author(s): Dan Halbert for Adafruit Industries

"""
from bleio import Attribute, Characteristic, UUID

class DeviceInformationService:
"""This is a factory class only, and has no instances."""

@staticmethod
def add_to_peripheral(peripheral, *, model_number=None, serial_number=None,
firmware_revision=None, hardware_revision='',
software_revision='', manufacturer=''):
"""
Add a Service with fixed Device Information Service characteristics to the given Peripheral.
All values are optional.

:param str model_number: Device model number. If None use `sys.platform`.
:param str serial_number: Device serial number. If None use a hex representation of
``microcontroller.cpu.id``.
:param str firmware_revision: Device firmware revision.
If None use ``os.uname().version``.
:param str hardware_revision: Device hardware revision.
:param str software_revision: Device software revision.
:param str manufacturer: Device manufacturer name
:return: the created Service

Example::

peripheral = Peripheral()
dis = DeviceInformationService.add_to_peripheral(
peripheral, software_revision="1.2.4", manufacturer="Acme Corp")
"""

# Avoid creating constants with names if not necessary. Just takes up space.
# Device Information Service UUID = 0x180A
# Module Number UUID = 0x2A24
# Serial Number UUID = 0x2A25
# Firmware Revision UUID = 0x2A26
# Hardware Revision UUID = 0x2A27
# Software Revision UUID = 0x2A28
# Manufacturer Name UUID = 0x2A29

service = peripheral.add_service(UUID(0x180A))

if model_number is None:
import sys
model_number = sys.platform
if serial_number is None:
import microcontroller
import binascii
serial_number = binascii.hexlify(microcontroller.cpu.uid).decode('utf-8') # pylint: disable=no-member

if firmware_revision is None:
import os
firmware_revision = os.uname().version

# Values must correspond to UUID numbers.
for uuid_num, value in zip(
range(0x2A24, 0x2A29+1),
(model_number, serial_number,
firmware_revision, hardware_revision, software_revision,
manufacturer)):

service.add_characteristic(UUID(uuid_num), properties=Characteristic.READ,
read_perm=Attribute.OPEN, write_perm=Attribute.NO_ACCESS,
fixed_length=True, max_length=len(value),
initial_value=value)

return service
Loading