示例#1
0
def _client_irq(event, data):
    if event == _IRQ_GATTC_SERVICE_RESULT:
        conn_handle, start_handle, end_handle, uuid = data
        ClientDiscover._discover_result(conn_handle, start_handle, end_handle, bluetooth.UUID(uuid))
    elif event == _IRQ_GATTC_SERVICE_DONE:
        conn_handle, status = data
        ClientDiscover._discover_done(conn_handle, status)
    elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT:
        conn_handle, def_handle, value_handle, properties, uuid = data
        ClientDiscover._discover_result(conn_handle, def_handle, value_handle, properties, bluetooth.UUID(uuid))
    elif event == _IRQ_GATTC_CHARACTERISTIC_DONE:
        conn_handle, status = data
        ClientDiscover._discover_done(conn_handle, status)
    elif event == _IRQ_GATTC_DESCRIPTOR_RESULT:
        conn_handle, dsc_handle, uuid = data
        ClientDiscover._discover_result(conn_handle, dsc_handle, bluetooth.UUID(uuid))
    elif event == _IRQ_GATTC_DESCRIPTOR_DONE:
        conn_handle, status = data
        ClientDiscover._discover_done(conn_handle, status)
    elif event == _IRQ_GATTC_READ_RESULT:
        conn_handle, value_handle, char_data = data
        ClientCharacteristic._read_result(conn_handle, value_handle, bytes(char_data))
    elif event == _IRQ_GATTC_READ_DONE:
        conn_handle, value_handle, status = data
        ClientCharacteristic._read_done(conn_handle, value_handle, status)
    elif event == _IRQ_GATTC_WRITE_DONE:
        conn_handle, value_handle, status = data
        ClientCharacteristic._write_done(conn_handle, value_handle, status)
    elif event == _IRQ_GATTC_NOTIFY:
        conn_handle, value_handle, notify_data = data
        ClientCharacteristic._on_notify(conn_handle, value_handle, bytes(notify_data))
    elif event == _IRQ_GATTC_INDICATE:
        conn_handle, value_handle, indicate_data = data
        ClientCharacteristic._on_indicate(conn_handle, value_handle, bytes(indicate_data))
示例#2
0
def decode_services(payload):
    services = []
    for u in decode_field(payload, ADType.ADV_TYPE_UUID16_COMPLETE):
        services.append(bluetooth.UUID(struct.unpack('<h', u)[0]))
    for u in decode_field(payload, ADType.ADV_TYPE_UUID128_COMPLETE):
        services.append(bluetooth.UUID(u))
    return services
示例#3
0
 def constructUUIDList(self):
     return ((bluetooth.UUID(self._config["wattmeter"]["service"]), ((
         bluetooth.UUID(self._config["wattmeter"]["amps"]),
         bluetooth.FLAG_NOTIFY,
     ), (
         bluetooth.UUID(self._config["wattmeter"]["mah"]),
         bluetooth.FLAG_NOTIFY,
     ))), )
示例#4
0
def demo():
    payload = advertising_payload(
        name="micropython",
        services=[bluetooth.UUID(0x181A), bluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")],
    )
    print(payload)
    print(decode_name(payload))
    print(decode_services(payload))
示例#5
0
 def decode_services(payload):
     services = []
     for u in BleHandler._decode_field(payload, _ADV_TYPE_UUID16_COMPLETE):
         services.append(bluetooth.UUID(struct.unpack("<h", u)[0]))
     for u in BleHandler._decode_field(payload, _ADV_TYPE_UUID32_COMPLETE):
         services.append(bluetooth.UUID(struct.unpack("<d", u)[0]))
     for u in BleHandler._decode_field(payload, _ADV_TYPE_UUID128_COMPLETE):
         services.append(bluetooth.UUID(u))
     return services
def decode_adv_services(data):
    services = []
    for payload in _find_adv_data(data, _ADV_TYPE_UUID16_COMPLETE):
        services.append(bluetooth.UUID(struct.unpack("<h", payload)[0]))
    for payload in _find_adv_data(data, _ADV_TYPE_UUID32_COMPLETE):
        services.append(bluetooth.UUID(struct.unpack("<d", payload)[0]))
    for payload in _find_adv_data(data, _ADV_TYPE_UUID128_COMPLETE):
        services.append(bluetooth.UUID(payload))
    return services
示例#7
0
 def services(self):
     for u in self._decode_field(_ADV_TYPE_UUID16_INCOMPLETE,
                                 _ADV_TYPE_UUID16_COMPLETE):
         yield bluetooth.UUID(struct.unpack("<H", u)[0])
     for u in self._decode_field(_ADV_TYPE_UUID32_INCOMPLETE,
                                 _ADV_TYPE_UUID32_COMPLETE):
         yield bluetooth.UUID(struct.unpack("<I", u)[0])
     for u in self._decode_field(_ADV_TYPE_UUID128_INCOMPLETE,
                                 _ADV_TYPE_UUID128_COMPLETE):
         yield bluetooth.UUID(u)
示例#8
0
class UARTService(blesync_client.Service):
    uuid = bluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")

    _rx = blesync_client.Characteristic(
        bluetooth.UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E"), )
    _tx = blesync_client.Characteristic(
        bluetooth.UUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E"), )

    on_message = _tx.on_notify

    def send(self, message, ack=False):
        self._rx.write(message, ack)
示例#9
0
 async def subscribe(self, notify=True, indicate=False):
     # Ensure that the generated notifications are dispatched in case the app
     # hasn't awaited on notified/indicated yet.
     self._register_with_connection()
     if cccd := await self.descriptor(bluetooth.UUID(_CCCD_UUID)):
         await cccd.write(
             struct.pack("<H",
                         _CCCD_NOTIFY * notify + _CCCD_INDICATE * indicate))
示例#10
0
class UARTService(blesync_server.Service):
    uuid = bluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")

    _tx = blesync_server.Characteristic(
        bluetooth.UUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E"),
        bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY,
    )

    _rx = blesync_server.Characteristic(
        bluetooth.UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E"),
        bluetooth.FLAG_WRITE,
        buffer_size=100,
        buffer_append=True)

    characteristics = (_tx, _rx)

    on_message = _rx.on_write

    def send(self, conn_handle, data):
        self._tx.notify(conn_handle, data)
示例#11
0
def Main(aConf) -> tuple:
    import bluetooth
    from micropython import const

    from IncP.BLE import GetAdvPayload
    from .Main import TBleEx
    from App import ConfApp

    # org.bluetooth.service.environmental_sensing
    _ENV_SENSE_UUID = bluetooth.UUID(0x181A)
    # org.bluetooth.characteristic.gap.appearance.xml
    _ADV_APPEARANCE_GENERIC_THERMOMETER = const(0x300)

    Payload = GetAdvPayload(aName=ConfApp.get('BLE_Name', 'MyBLE'),
                            aServices=[_ENV_SENSE_UUID],
                            aAppearance=_ADV_APPEARANCE_GENERIC_THERMOMETER)
    Obj = TBleEx(Payload)
    return (Obj, Obj.Run())
示例#12
0
class BLECharacteristic:
    def __init__(self, gattc, def_handle, value_handle, properties):
        self._gattc = gattc  # parent BLEGATTC
        self._def_handle = def_handle  # ?
        self._value_handle = value_handle  # handle used to read/write value
        self._end_handle = None  # last handle for characteristic (crazy stuff!)
        # public attributes
        self.properties = properties  # ?
        self.descriptors = {}  # BLEDescriptors indexed by UUID

    # Read the value
    async def read(self):
        gattc = self._gattc
        e = gattc._add_pending(self._value_handle)
        gattc._ble.gattc_read(gattc._conn_handle, self._value_handle)
        return await e.get()

    # Write the value
    def write(self, v, response=False):
        if not self.is_connected():
            return
        gattc = self._gattc
        gattc._ble.gattc_write(gattc._conn_handle, self._value_handle, v,
                               1 if response else 0)

    CCC_UUID = bluetooth.UUID(
        0x2902)  # client characteristic configuration descriptor

    # Subscribe to notifications
    def subscribe(self, qlen=2):
        if self.CCC_UUID in self.descriptors:
            gattc = self._gattc
            e = gattc._add_pending(self._value_handle)
            vh = self.descriptors[self.CCC_UUID]._value_handle
            gattc._ble.gattc_write(gattc._conn_handle, vh, b"\x01\x00", 0)
            return e.get  # returning the method itself
        else:
            log.warning("No CCC descriptor? %s", self.descriptors)
            raise OSError(errno.ENOENT)
示例#13
0
def demo():
    import time

    ble = bluetooth.BLE()
    uart = BLEUART(ble,
                   name='WifiKit32',
                   services=[bluetooth.UUID(b'\x7E\xE7\x55\xCA')])

    def on_rx():
        print('rx: ', uart.read().decode().strip())

    uart.irq(handler=on_rx)
    nums = [4, 8, 15, 16, 23, 42]
    i = 0

    try:
        while True:
            uart.write(str(nums[i]) + '\n')
            i = (i + 1) % len(nums)
            time.sleep_ms(1000)
    except KeyboardInterrupt:
        pass

    uart.close()
示例#14
0
#
# The sensor's local value updates every second, and it will notify
# any connected central every 10 seconds.

import bluetooth
import random
import struct
import time
from ble_advertising import advertising_payload

from micropython import const
_IRQ_CENTRAL_CONNECT                 = const(1 << 0)
_IRQ_CENTRAL_DISCONNECT              = const(1 << 1)

# org.bluetooth.service.environmental_sensing
_ENV_SENSE_UUID = bluetooth.UUID(0x181A)
# org.bluetooth.characteristic.temperature
_TEMP_CHAR = (bluetooth.UUID(0x2A6E), bluetooth.FLAG_READ|bluetooth.FLAG_NOTIFY,)
_ENV_SENSE_SERVICE = (_ENV_SENSE_UUID, (_TEMP_CHAR,),)

# org.bluetooth.characteristic.gap.appearance.xml
_ADV_APPEARANCE_GENERIC_THERMOMETER = const(768)

class BLETemperature:
    def __init__(self, ble, name='mpy-temp'):
        self._ble = ble
        self._ble.active(True)
        self._ble.irq(handler=self._irq)
        ((self._handle,),) = self._ble.gatts_register_services((_ENV_SENSE_SERVICE,))
        self._connections = set()
        self._payload = advertising_payload(name=name, services=[_ENV_SENSE_UUID], appearance=_ADV_APPEARANCE_GENERIC_THERMOMETER)
示例#15
0
_IRQ_PERIPHERAL_CONNECT = const(1 << 6)
_IRQ_PERIPHERAL_DISCONNECT = const(1 << 7)
_IRQ_GATTC_SERVICE_RESULT = const(1 << 8)
_IRQ_GATTC_CHARACTERISTIC_RESULT = const(1 << 9)
_IRQ_GATTC_DESCRIPTOR_RESULT = const(1 << 10)
_IRQ_GATTC_READ_RESULT = const(1 << 11)
_IRQ_GATTC_WRITE_STATUS = const(1 << 12)
_IRQ_GATTC_NOTIFY = const(1 << 13)
_IRQ_GATTC_INDICATE = const(1 << 14)
_IRQ_ALL = const(0xffff)

# org.bluetooth.characteristic.gap.appearance.xml
_ADV_APPEARANCE_GENERIC_COMPUTER = const(128)

##
_UART_UUID = bluetooth.UUID('6E400001-B5A3-F393-E0A9-E50E24DCCA9E')
_UART_TX = (
    bluetooth.UUID('6E400003-B5A3-F393-E0A9-E50E24DCCA9E'),
    bluetooth.FLAG_NOTIFY,
)
_UART_RX = (
    bluetooth.UUID('6E400002-B5A3-F393-E0A9-E50E24DCCA9E'),
    bluetooth.FLAG_WRITE,
)
_UART_SERVICE = (
    _UART_UUID,
    (
        _UART_TX,
        _UART_RX,
    ),
)
示例#16
0
# robby.py - fischertechnik ROBBY implementation
#
# (c) 2022 by Till Harbaum <*****@*****.**>
#
# This code implements the communication protocol of the fischertechnik early
# coding controller ("robby") in Micropython. This allows to e.g. control
# the ESP32 using the fischertechnik first coding app.
#

import bluetooth, struct, time

NAME = "Robby32"
DEV_INFO = True # False    # enable device info characteristics

# the custom ft Robby service. 
_ROBBY_UUID = bluetooth.UUID("7b130100-ce8d-45bb-9158-631b769139e9")
_ROBBY_SERVICE = (
  _ROBBY_UUID, (
    ( bluetooth.UUID("7b130101-ce8d-45bb-9158-631b769139e9"), 0x0a ),  # R/W
    ( bluetooth.UUID("7b130102-ce8d-45bb-9158-631b769139e9"), 0x0a ),  # R/W
    ( bluetooth.UUID("7b130103-ce8d-45bb-9158-631b769139e9"), 0x0a ),  # R/W
    ( bluetooth.UUID("7b130104-ce8d-45bb-9158-631b769139e9"), 0x12 ),  # R/N
    ( bluetooth.UUID("7b130105-ce8d-45bb-9158-631b769139e9"), 0x12 ),  # R/N
    ( bluetooth.UUID("7b130106-ce8d-45bb-9158-631b769139e9"), 0x12 ),  # R/N
    ( bluetooth.UUID("7b130107-ce8d-45bb-9158-631b769139e9"), 0x12 )   # R/N
  ),
)

_BATT_SERVICE = (
  bluetooth.UUID("0000180f-0000-1000-8000-00805f9b34fb"), (
    ( bluetooth.UUID("00002a19-0000-1000-8000-00805f9b34fb"), 0x12 ),  # R/N
示例#17
0
_IRQ_GATTC_READ_RESULT = const(1 << 11)
_IRQ_GATTC_WRITE_STATUS = const(1 << 12)
_IRQ_GATTC_NOTIFY = const(1 << 13)
_IRQ_GATTC_INDICATE = const(1 << 14)

_IRQ_GATTC_SERVICE_DONE = const(10)

_ADV_IND = const(0x00)
_ADV_DIRECT_IND = const(0x01)
_ADV_SCAN_IND = const(0x02)
_ADV_NONCONN_IND = const(0x03)

_NOTIFY_ENABLE = const(1)
_INDICATE_ENABLE = const(2)

_ACC_SERVICE_UUID = bluetooth.UUID("E95D0753-251D-470A-A062-FA1922DFA9A8")
_ACC_DATA_UUID = bluetooth.UUID("E95DCA4B-251D-470A-A062-FA1922DFA9A8")
_ACC_DESCR_UUID = bluetooth.UUID(0x2902)

_BUTTON_SERVICE_UUID = bluetooth.UUID("E95D9882-251D-470A-A062-FA1922DFA9A8")

_BUTTON_A_STATE_UUID = bluetooth.UUID("E95DDA90-251D-470A-A062-FA1922DFA9A8")
_BUTTON_B_STATE_UUID = bluetooth.UUID("E95DDA91-251D-470A-A062-FA1922DFA9A8")
"""
public static String ACCELEROMETERSERVICE_SERVICE_UUID = "E95D0753251D470AA062FA1922DFA9A8";
public static String ACCELEROMETERDATA_CHARACTERISTIC_UUID = "E95DCA4B251D470AA062FA1922DFA9A8";
public static String ACCELEROMETERPERIOD_CHARACTERISTIC_UUID = "E95DFB24251D470AA062FA1922DFA9A8";
"""
MAC_MICRO = b'\xFA\x35\x2F\x6C\x13\xf8'

# Advertising payloads are repeated packets of the following form:
示例#18
0
import bluetooth
import struct
import time
from ble_advertising import advertising_payload
from machine import Timer
from micropython import const
from upynotify import NOTIFYER
import os
import sys

_IRQ_CENTRAL_CONNECT = const(1 << 0)
_IRQ_CENTRAL_DISCONNECT = const(1 << 1)
_IRQ_GATTS_WRITE = const(1 << 2)

# org.bluetooth.service.immediate_alert_service
_IMMEDIATE_ALERT_SERV_UUID = bluetooth.UUID(0x1802)
# org.bluetooth.characteristic.alert_level
_ALERT_LEVEL_CHAR = (
    bluetooth.UUID(0x2A06),
    bluetooth.FLAG_WRITE,
)

_IMMEDIATE_ALERT_SERVICE = (
    _IMMEDIATE_ALERT_SERV_UUID,
    (_ALERT_LEVEL_CHAR, ),
)

# org.bluetooth.service.device_information
_DEV_INF_SERV_UUID = bluetooth.UUID(0x180A)
# org.bluetooth.characteristic.appearance
_APPEAR_CHAR = (bluetooth.UUID(0x2A01), bluetooth.FLAG_READ)
示例#19
0
_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11)
_IRQ_GATTC_CHARACTERISTIC_DONE = const(12)
_IRQ_GATTC_DESCRIPTOR_RESULT = const(13)
_IRQ_GATTC_DESCRIPTOR_DONE = const(14)
_IRQ_GATTC_READ_RESULT = const(15)
_IRQ_GATTC_READ_DONE = const(16)
_IRQ_GATTC_WRITE_DONE = const(17)
_IRQ_GATTC_NOTIFY = const(18)
_IRQ_GATTC_INDICATE = const(19)

_ADV_IND = const(0x00)
_ADV_DIRECT_IND = const(0x01)
_ADV_SCAN_IND = const(0x02)
_ADV_NONCONN_IND = const(0x03)

_UART_SERVICE_UUID = bluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
_UART_RX_CHAR_UUID = bluetooth.UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")
_UART_TX_CHAR_UUID = bluetooth.UUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")


class BLESimpleCentral:
    def __init__(self, ble):
        self._ble = ble
        self._ble.active(True)
        self._ble.irq(self._irq)

        self._reset()

    def _reset(self):
        # Cached name and address from a successful scan.
        self._name = None
示例#20
0
_IRQ_GATTC_NOTIFY = const(1 << 13)
_IRQ_GATTC_INDICATE = const(1 << 14)

# these values com from the generic micropython implementation and might not work on SPIKE prime
_IRQ_GATTC_SERVICE_DONE = const(10)
_IRQ_GATTC_WRITE_DONE = const(17)

_ADV_IND = const(0x00)
_ADV_DIRECT_IND = const(0x01)
_ADV_SCAN_IND = const(0x02)
_ADV_NONCONN_IND = const(0x03)

_NOTIFY_ENABLE = const(1)
_INDICATE_ENABLE = const(2)

_UART_UUID = bluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
_UART_TX = bluetooth.UUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")
_UART_RX = bluetooth.UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")
"""
public static String ACCELEROMETERSERVICE_SERVICE_UUID = "E95D0753251D470AA062FA1922DFA9A8";
public static String ACCELEROMETERDATA_CHARACTERISTIC_UUID = "E95DCA4B251D470AA062FA1922DFA9A8";
public static String ACCELEROMETERPERIOD_CHARACTERISTIC_UUID = "E95DFB24251D470AA062FA1922DFA9A8";
"""
MAC_MICRO = b'\xFA\x35\x2F\x6C\x13\xf8'
#MAC_ESP=b'\x50\x02\x91\x8d\x17\x26'
MAC_ESP = b'\xd8\xa0\x1d\x40\x71\x3e'

# Advertising payloads are repeated packets of the following form:
#1 byte data length (N + 1)
#1 byte type (see constants below)
#N bytes type-specific data
示例#21
0
_FLAG_READ = const(0x0002)
_FLAG_WRITE_NO_RESPONSE = const(0x0004)
_FLAG_WRITE = const(0x0008)
_FLAG_NOTIFY = const(0x0010)

_ADV_TYPE_FLAGS = const(0x01)
_ADV_TYPE_NAME = const(0x09)
_ADV_TYPE_UUID16_COMPLETE = const(0x3)
_ADV_TYPE_UUID32_COMPLETE = const(0x5)
_ADV_TYPE_UUID128_COMPLETE = const(0x7)
_ADV_TYPE_UUID16_MORE = const(0x2)
_ADV_TYPE_UUID32_MORE = const(0x4)
_ADV_TYPE_UUID128_MORE = const(0x6)
_ADV_TYPE_APPEARANCE = const(0x19)

_UART_SERVICE_UUID = bluetooth.UUID('6ee6d166-6084-11eb-ae93-0242ac130002')
_UART_RX_CHAR_UUID = bluetooth.UUID('6ee6d3e6-6084-11eb-ae93-0242ac130002')
_UART_TX_CHAR_UUID = bluetooth.UUID('6ee6d4cc-6084-11eb-ae93-0242ac130002')

_UART_UUID = _UART_SERVICE_UUID
_UART_TX = (
    _UART_TX_CHAR_UUID,
    _FLAG_READ | _FLAG_NOTIFY,
)
_UART_RX = (
    _UART_RX_CHAR_UUID,
    _FLAG_WRITE | _FLAG_WRITE_NO_RESPONSE,
)
_UART_SERVICE = (
    _UART_UUID,
    (_UART_TX, _UART_RX),
示例#22
0
https://mpython.readthedocs.io/en/master/library/micropython/ubluetooth.html
'''

import bluetooth
from micropython import const
from ubinascii import hexlify
import struct
#
from Inc.Log import Log

_IRQ_CENTRAL_CONNECT = const(1)
_IRQ_CENTRAL_DISCONNECT = const(2)
_IRQ_GATTS_WRITE = const(3)

# support 'Heart Rate'
_HR_UUID = bluetooth.UUID(0x180D)
_HR_CHAR = (
    bluetooth.UUID(0x2A37),
    bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY,
)
_HR_SERVICE = (
    _HR_UUID,
    (_HR_CHAR, ),
)

# support 'Nordic UART'
_UART_UUID = bluetooth.UUID('6E400001-B5A3-F393-E0A9-E50E24DCCA9E')
_UART_TX = (
    bluetooth.UUID('6E400003-B5A3-F393-E0A9-E50E24DCCA9E'),
    bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY,
)
示例#23
0
文件: micro.py 项目: ste7anste7an/ble
_IRQ_GATTC_DESCRIPTOR_DONE = const(14)
_IRQ_GATTC_READ_RESULT = const(15)
_IRQ_GATTC_READ_DONE = const(16)
_IRQ_GATTC_WRITE_DONE = const(17)
_IRQ_GATTC_NOTIFY = const(18)
_IRQ_GATTC_INDICATE = const(19)

_ADV_IND = const(0x00)
_ADV_DIRECT_IND = const(0x01)
_ADV_SCAN_IND = const(0x02)
_ADV_NONCONN_IND = const(0x03)

_NOTIFY_ENABLE = const(1)
_INDICATE_ENABLE = const(2)

_ACC_SERVICE_UUID = bluetooth.UUID("E95D0753-251D-470A-A062-FA1922DFA9A8")
_ACC_DATA_UUID = bluetooth.UUID("E95DCA4B-251D-470A-A062-FA1922DFA9A8")
_ACC_DESCR_UUID = bluetooth.UUID(0x2902)
"""
public static String ACCELEROMETERSERVICE_SERVICE_UUID = "E95D0753251D470AA062FA1922DFA9A8";
public static String ACCELEROMETERDATA_CHARACTERISTIC_UUID = "E95DCA4B251D470AA062FA1922DFA9A8";
public static String ACCELEROMETERPERIOD_CHARACTERISTIC_UUID = "E95DFB24251D470AA062FA1922DFA9A8";
"""
MAC_MICRO = b'\xFA\x35\x2F\x6C\x13\xf8'


class BLESimpleCentral:
    def __init__(self, ble):
        self._ble = ble
        self._ble.active(True)
        self._ble.irq(self._irq)
示例#24
0
# This example demonstrates a simple temperature sensor peripheral.
#
# The sensor's local value updates every second, and it will notify
# any connected central every 10 seconds.

import bluetooth
import random
import struct
import time
from micropython import const
from bleperipheral import BLEPeripheral

# org.bluetooth.service.environmental_sensing
_ENV_SENSE_UUID = bluetooth.UUID(0x181A)
# org.bluetooth.characteristic.temperature
_TEMP_CHAR = (
    bluetooth.UUID(0x2A6E),
    bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY,
)
_ENV_SENSE_SERVICE = (
    _ENV_SENSE_UUID,
    (_TEMP_CHAR, ),
)

# org.bluetooth.characteristic.gap.appearance.xml
_ADV_APPEARANCE_GENERIC_THERMOMETER = const(768)


class BLETemperature:
    def __init__(self, connected, disconnected, multi_connections):
        self._bleperipheral = BLEPeripheral(
_IRQ_GATTC_READ_RESULT = const(1 << 11)
_IRQ_GATTC_WRITE_STATUS = const(1 << 12)
_IRQ_GATTC_NOTIFY = const(1 << 13)
_IRQ_GATTC_INDICATE = const(1 << 14)

_IRQ_GATTC_SERVICE_DONE = const(10)

_ADV_IND = const(0x00)
_ADV_DIRECT_IND = const(0x01)
_ADV_SCAN_IND = const(0x02)
_ADV_NONCONN_IND = const(0x03)

_NOTIFY_ENABLE = const(1)
_INDICATE_ENABLE = const(2)

_ACC_SERVICE_UUID = bluetooth.UUID("E95D0753-251D-470A-A062-FA1922DFA9A8")
_ACC_DATA_UUID = bluetooth.UUID("E95DCA4B-251D-470A-A062-FA1922DFA9A8")
_ACC_DESCR_UUID = bluetooth.UUID(0x2902)

_BUTTON_SERVICE_UUID = bluetooth.UUID("E95D9882-251D-470A-A062-FA1922DFA9A8")

_BUTTON_A_STATE_UUID = bluetooth.UUID("E95DDA90-251D-470A-A062-FA1922DFA9A8")
_BUTTON_B_STATE_UUID = bluetooth.UUID("E95DDA91-251D-470A-A062-FA1922DFA9A8")

_LED_SERVICE_UUID = bluetooth.UUID("E95DD91D-251D-470A-A062-FA1922DFA9A8")
_LED_MATRIX_UUID = bluetooth.UUID("E95D7B77-251D-470A-A062-FA1922DFA9A8")
_LED_TEXT_UUID = bluetooth.UUID("E95D93EE-251D-470A-A062-FA1922DFA9A8")
_LED_DELAY_UUID = bluetooth.UUID("E95D0D2D-251D-470A-A062-FA1922DFA9A8")
"""
public static String ACCELEROMETERSERVICE_SERVICE_UUID = "E95D0753251D470AA062FA1922DFA9A8";
public static String ACCELEROMETERDATA_CHARACTERISTIC_UUID = "E95DCA4B251D470AA062FA1922DFA9A8";
示例#26
0
_IRQ_GATTC_CHARACTERISTIC_DONE = const(12)
_IRQ_GATTC_DESCRIPTOR_RESULT = const(13)
_IRQ_GATTC_DESCRIPTOR_DONE = const(14)
_IRQ_GATTC_READ_RESULT = const(15)
_IRQ_GATTC_READ_DONE = const(16)
_IRQ_GATTC_WRITE_DONE = const(17)
_IRQ_GATTC_NOTIFY = const(18)
_IRQ_GATTC_INDICATE = const(19)

_ADV_IND = const(0x00)
_ADV_DIRECT_IND = const(0x01)
_ADV_SCAN_IND = const(0x02)
_ADV_NONCONN_IND = const(0x03)

# org.bluetooth.service.environmental_sensing
_ENV_SENSE_UUID = bluetooth.UUID(0x181A)
# org.bluetooth.characteristic.temperature
_TEMP_UUID = bluetooth.UUID(0x2A6E)
_TEMP_CHAR = (
    _TEMP_UUID,
    bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY,
)
_ENV_SENSE_SERVICE = (
    _ENV_SENSE_UUID,
    (_TEMP_CHAR, ),
)

# org.bluetooth.characteristic.gap.appearance.xml
_ADV_APPEARANCE_GENERIC_THERMOMETER = const(768)

示例#27
0
    _IRQ_GATTC_NOTIFY = 1 << 13
    _IRQ_GATTC_CHARACTERISTIC_DONE = 1 << 12

    
_IRQ_GATTC_SERVICE_DONE = const(10)

_ADV_IND = const(0x00)
_ADV_DIRECT_IND = const(0x01)
_ADV_SCAN_IND = const(0x02)
_ADV_NONCONN_IND = const(0x03)

_NOTIFY_ENABLE = const(1)
_INDICATE_ENABLE = const(2)


_ACC_SERVICE_UUID =    bluetooth.UUID("E95D0753-251D-470A-A062-FA1922DFA9A8")
_ACC_DATA_UUID =       bluetooth.UUID("E95DCA4B-251D-470A-A062-FA1922DFA9A8")
_ACC_DESCR_UUID=       bluetooth.UUID(0x2902)

_BUTTON_SERVICE_UUID = bluetooth.UUID("E95D9882-251D-470A-A062-FA1922DFA9A8")

_BUTTON_A_STATE_UUID = bluetooth.UUID("E95DDA90-251D-470A-A062-FA1922DFA9A8")
_BUTTON_B_STATE_UUID = bluetooth.UUID("E95DDA91-251D-470A-A062-FA1922DFA9A8")

_LED_SERVICE_UUID =    bluetooth.UUID("E95DD91D-251D-470A-A062-FA1922DFA9A8")
_LED_MATRIX_UUID =     bluetooth.UUID("E95D7B77-251D-470A-A062-FA1922DFA9A8")
_LED_TEXT_UUID =       bluetooth.UUID("E95D93EE-251D-470A-A062-FA1922DFA9A8")
_LED_DELAY_UUID =      bluetooth.UUID("E95D0D2D-251D-470A-A062-FA1922DFA9A8")

_UART_UUID = bluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
_UART_RX = bluetooth.UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")
示例#28
0
# Test notification-specific behavior.

import sys

sys.path.append("")

from micropython import const
import time, machine

import uasyncio as asyncio
import aioble
import bluetooth

TIMEOUT_MS = 5000

SERVICE_UUID = bluetooth.UUID("A5A5A5A5-FFFF-9999-1111-5A5A5A5A5A5A")
CHAR_UUID = bluetooth.UUID("00000000-1111-2222-3333-444444444444")


# Acting in peripheral role.
async def instance0_task():
    service = aioble.Service(SERVICE_UUID)
    characteristic = aioble.Characteristic(service,
                                           CHAR_UUID,
                                           read=True,
                                           notify=True)
    aioble.register_services(service)

    multitest.globals(BDADDR=aioble.config("mac"))
    multitest.next()
示例#29
0
_IRQ_CENTRAL_CONNECT = const(1)
_IRQ_CENTRAL_DISCONNECT = const(2)
_IRQ_GATTS_WRITE = const(3)
_IRQ_PERIPHERAL_CONNECT = const(7)
_IRQ_PERIPHERAL_DISCONNECT = const(8)
_IRQ_GATTC_SERVICE_RESULT = const(9)
_IRQ_GATTC_SERVICE_DONE = const(10)
_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11)
_IRQ_GATTC_CHARACTERISTIC_DONE = const(12)
_IRQ_GATTC_READ_RESULT = const(15)
_IRQ_GATTC_READ_DONE = const(16)
_IRQ_GATTC_WRITE_DONE = const(17)
_IRQ_GATTC_NOTIFY = const(18)

SERVICE_UUID = bluetooth.UUID("00000001-1111-2222-3333-444444444444")
CHAR_CTRL_UUID = bluetooth.UUID("00000002-1111-2222-3333-444444444444")
CHAR_RX_UUID = bluetooth.UUID("00000003-1111-2222-3333-444444444444")
CHAR_TX_UUID = bluetooth.UUID("00000004-1111-2222-3333-444444444444")
CHAR_CTRL = (CHAR_CTRL_UUID, bluetooth.FLAG_WRITE | bluetooth.FLAG_NOTIFY)
CHAR_RX = (CHAR_RX_UUID,
           bluetooth.FLAG_WRITE | bluetooth.FLAG_WRITE_NO_RESPONSE)
CHAR_TX = (CHAR_TX_UUID, bluetooth.FLAG_NOTIFY)
SERVICE = (SERVICE_UUID, (CHAR_CTRL, CHAR_RX, CHAR_TX))
SERVICES = (SERVICE, )

waiting_event = None
waiting_data = None
ctrl_value_handle = 0
rx_value_handle = 0
tx_value_handle = 0
示例#30
0
BUTTON_PIN_MAP = {
    "button_rig_up": create_pin(32),
    "button_rig_down": create_pin(33),
    "button_scene1": create_pin(25),
    "button_scene2": create_pin(26),
    "button_scene3": create_pin(27),
    "button_scene4": create_pin(14),
    "button_tap_tempo": create_pin(12),
}

_FLAG_READ = const(0x0002)
_FLAG_WRITE_NO_RESPONSE = const(0x0004)
_FLAG_WRITE = const(0x0008)
_FLAG_NOTIFY = const(0x0010)

_UART_UUID = bluetooth.UUID("5c47e665-6580-4a15-91ea-02caea0ae4a7")
_UART_TX = (
    bluetooth.UUID("9138f9c0-9427-4299-85f3-7aa97b0d0c46"),
    _FLAG_READ | _FLAG_NOTIFY,
)
_UART_RX = (
    bluetooth.UUID("7a47b14d-04c5-440c-b701-c5ed67789dff"),
    _FLAG_WRITE | _FLAG_WRITE_NO_RESPONSE,
)
_UART_SERVICE = (
    _UART_UUID,
    (_UART_TX, _UART_RX),
)

BLE = bluetooth.BLE()
BLE.active(True)