当前位置: 首页>>代码示例>>Python>>正文


Python Peripheral.discoverServices方法代码示例

本文整理汇总了Python中bluepy.btle.Peripheral.discoverServices方法的典型用法代码示例。如果您正苦于以下问题:Python Peripheral.discoverServices方法的具体用法?Python Peripheral.discoverServices怎么用?Python Peripheral.discoverServices使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在bluepy.btle.Peripheral的用法示例。


在下文中一共展示了Peripheral.discoverServices方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from bluepy.btle import Peripheral [as 别名]
# 或者: from bluepy.btle.Peripheral import discoverServices [as 别名]
class BeepMaker:
    ''' BeepMaker takes an address, queries to see if the device is beepable, and sets it beeping if it is... '''
    def __init__(self, addr):
        self.addr = addr
        self.thread = threading.Thread(target=self.run)
        self.terminate = False
        self.per = None
        self.lastBeep = 0
        self.state = 'Initialized'
        self.beep_characteristic = None

    def __str__(self):
        return 'BeepMaker[addr=%s] state=%s' % (self.addr, self.state)

    def run(self):
        try:
            self.connect()
            if not self.terminate:
                self.discover()
            if not self.terminate:
                self.verify()
            while not self.terminate:
                self.state = 'Lurking'
                if time.time() - self.lastBeep > 15:
                    self.beep()
                time.sleep(1)
        except Exception as e:
            log.exception('BeepMaker[addr=%s] %s: %s' % (self.addr, type(e), e))
            self.terminate = True

        log.debug('BeepMaker addr=%s %s' % (self.addr, 'end' if not self.terminate else 'terminated'))

    def connect(self):
        log.debug('BeepMaker[addr=%s], connecting...' % self.addr)
        self.state = 'Connecting'
        self.per = Peripheral(self.addr)

    def discover(self):
        log.debug('BeepMaker[addr=%s], service discovery...' % self.addr)
        self.state = 'Discovery'
        self.per.discoverServices()

    def verify(self):
        ''' Check this is in fact a beepable device '''
        log.debug('BeepMaker[addr=%s], analysing services...' % self.addr)
        self.state = 'Analysing'
        inf = dict()
        for uuid, service in self.per.services.items():
            if uuid == '180a':
                for c in service.getCharacteristics():
                    if c.supportsRead():
                        inf[c.uuid.getCommonName()] = c.read()
            if uuid == 'fff0':
                for c in service.getCharacteristics():
                    if c.uuid == 'fff2':
                        self.beep_characteristic = c
        if inf['Manufacturer Name String'] != b'SIGNAL' or inf['Model Number String'] != b'BT A8105':
            raise Exception('Manufacturer and/or model not a known beeper: %s' % inf)
        if self.beep_characteristic is None:
            raise Exception('No beep characteristic fff0/fff2')

    def beep(self):
        log.debug('BeepMaker[addr=%s], beeping...' % self.addr)
        self.state = 'Beeping'
        self.morse('hi mouse')
        self.lastBeep = time.time()

    def morse(self, message):
        log.debug('BeepMaker[addr=%s], morse(%s)' % (self.addr, message))
        morse = ''
        for letter in list(message):
            morse += internationalMorse[letter.upper()] + ' '
        log.debug('Morse is: %s' % morse)
        ml = list(morse)
        while len(ml) > 0:
            chunk = ml.pop(0)
            while len(ml) > 0 and ml[0] == chunk[-1]:
                chunk += ml.pop(0)
            if chunk[0] == '.':
                self.dit(len(chunk))
            elif chunk[0] == '-':
                self.dash(len(chunk))
            elif chunk[0] == ' ':
                time.sleep(0.200)
            elif chunk[0] == '/':
                time.sleep(0.400)

    def dit(self, n=1):
        self.multibeep(120, 120, n)

    def dash(self, n=1):
        self.multibeep(255, 120, n)

    def multibeep(self, ontime, offtime, n=1):
        log.debug('BeepMaker[addr=%s], multibeep(on=%s, off=%s, n=%s)' % (self.addr, ontime, offtime, n))
        sleepms = (ontime+offtime)*n+(offtime*2)
        self.beep_characteristic.write(struct.pack('BBBBB', 0xaa, 3, n, ontime, offtime))
        time.sleep(sleepms/1000)
        log.debug('BeepMaker[addr=%s], multibeep wait end' % self.addr)

#.........这里部分代码省略.........
开发者ID:matthewg42,项目名称:btlefun,代码行数:103,代码来源:beepmonster.py

示例2: print

# 需要导入模块: from bluepy.btle import Peripheral [as 别名]
# 或者: from bluepy.btle.Peripheral import discoverServices [as 别名]
    global de
    for u, s in de.services.items():
        if u == uuid:
            return s
    print('no such service uuid: %s' % uuid)
    return None

def lsc(serv):
    for c in serv.getCharacteristics():
        print('handle=%-3s valHandle=%-3s supportsRead=%-5s uuid=%s properties=%s' % (
                c.handle,
                c.valHandle,
                c.supportsRead(),
                str(c.uuid),
                c.propertiesToString()))

def handler(handle, data):
    print('handler(handle=%s, data=%s)' % (repr(handle), repr(data)))
    
print('connecting to %s...' % addr)
de = Peripheral(addr)
print('connected, discovering services...')
de.discoverServices()
print('commands:')
print('  lss()            list services')
print('  service(uuid)    get service object')
print('  lsc(service)     list characteristics for a service')
print('  char(serv, uuid) get a characteristic by uuid')


开发者ID:matthewg42,项目名称:fire_pong,代码行数:30,代码来源:dirt.py

示例3: service

# 需要导入模块: from bluepy.btle import Peripheral [as 别名]
# 或者: from bluepy.btle.Peripheral import discoverServices [as 别名]
ledev = None

def service(uuid):
    global ledev
    for u, s in ledev.services.items():
        print('%s : %s' % (str(u), str(s)))
        if u == uuid:
            print('Found %s' % uuid)
            return s
    print('%s not found' % uuid)
    return None

def handler(h, d):
    print('handler(h, d), h=%s, d=%s' % (repr(h), repr(d)))

ledev = Peripheral(addr)
ledev.discoverServices()
print('device:', ledev)
#for uuid in ['fff0', 'fff3']:
#    s = service(uuid)
#    for h in range(s.hndStart, s.hndEnd+1):
#        ledev.writeCharacteristic(h, struct.pack('<BB', 0, 1))

s = service('fff0')

print('listening for notifications...')
while True:
    print(ledev.waitForNotifications(1.))


开发者ID:matthewg42,项目名称:fire_pong,代码行数:30,代码来源:notify.py


注:本文中的bluepy.btle.Peripheral.discoverServices方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。