gpt4 book ai didi

python - 异常断言错误: AssertionError() in 'pyobjus.protocol_forwardInvocation' ignored

转载 作者:太空宇宙 更新时间:2023-11-03 16:43:32 28 4
gpt4 key购买 nike

我正在构建一个 Kivy 应用程序,它使用 pyobjus 连接到 TI SensorTag 。我已成功让应用程序在扫描期间找到传感器,但无法连接它。我找到了一些代码here ,但它适用于不同的传感器。我收到的错误是:Exception AssertionError: AssertionError() in 'pyobjus.protocol_forwardInvocation' ignored来自 pyobjus,但我不知道如何跟踪错误,因为它被忽略了。

  1. 如何找到导致错误的原因(例如防止 pyobjus 忽略错误)?
  2. 更好的是,有没有人有一个简单的工作示例来说明如何通过 BLE 将设备连接到 Mac OSX?

编辑

这是代码,似乎问题出在 connect() 函数

class Ble(object):
def check_le(self, central):
state = central.state
if state == CBCentralManagerStateUnknown:
print 'CentralManager: Unknown state'
elif state == CBCentralManagerStatePoweredOn:
print 'CentralManager: Ready to go!'
return True
elif state == CBCentralManagerStatePoweredOff:
print 'CentralManager: Bluetooth is powered off'
elif state == CBCentralManagerStateUnauthorized:
print 'CentralManager: The application is not authorized to use BLE' # noqa
elif state == CBCentralManagerStateUnsupported:
print 'CentralManager: This hardware doesnt support BLE'

def connect(self, peripheral):
print "connecting"
self.stop_scan()
self.central.cancelPeripheralConnection_(peripheral)
self.central.connectPeripheral_options_(peripheral, None, None)

def disconnecte(self, peripheral):
# XXX !
print "Disconnect Not Implemented!"

def create(self):
self.callback = None
self.peripherals = {}
load_framework(INCLUDE.IOBluetooth)
CBCentralManager = autoclass('CBCentralManager')
self.central = CBCentralManager.alloc().initWithDelegate_queue_(
self, None)

def start_scan(self):
print 'Scanning started on Mac OSX MJR'
self.asked_services = []
self.central.scanForPeripheralsWithServices_options_(None, None)
Clock.schedule_interval(self.pop_queue, 0.04)

def stop_scan(self):
print "stopping scan"
self.central.stopScan()

def pop_queue(self, dt):
if self.queue:
self.callback(*self.queue.pop(0))

@protocol('CBCentralManagerDelegate')
def centralManagerDidUpdateState_(self, central):
print 'central state', central.state
self.check_le(central)
self.start_scan()

@protocol('CBCentralManagerDelegate')
def centralManager_didDiscoverPeripheral_advertisementData_RSSI_(
self, central, peripheral, data, rssi):
# print('centralManager:didDiscoverPeripheral:advertisementData:RSSI:')
# uuid = peripheral.identifier.UUIDString().cString()
# if peripheral.name:
# print('-> name=', peripheral.name.cString())
# print 'uuid:', uuid

keys = data.allKeys()
count = keys.count()
if count < 2:
return
name = peripheral.name.cString()
# name = data.objectForKey_(keys.objectAtIndex_(count - 2)).cString()
values = data.objectForKey_(keys.objectAtIndex_(count - 1))

sensor = c.get_from_ptr(values.bytes().arg_ref, 'c', values.length())
uuid = peripheral.description().cString()
if self.callback:
self.callback(rssi.intValue(), name, sensor)
else:
print uuid, name, sensor, rssi
if uuid not in self.peripherals:
pass
#self.connect(peripheral)
self.peripherals[name] = (peripheral, rssi)


@protocol('CBCentralManagerDelegate')
def centralManager_didConnectPeripheral_(self, central, peripheral):
if not peripheral.name.UTF8String():
return
peripheral.delegate = self
CBUUID = autoclass('CBUUID')
service = CBUUID.UUIDWithString_('1901')
peripheral.discoverServices_([service])

@protocol('CBPeripheralDelegate')
def peripheral_didDiscoverServices_(self, peripheral, error):
for i in range(peripheral.services.count()):
service = peripheral.services.objectAtIndex_(i)
if service.UUID.UUIDString.cString() == '1901':
break
else:
assert(0)
peripheral.discoverCharacteristics_forService_([], service)

@protocol('CBPeripheralDelegate')
def peripheral_didDiscoverCharacteristicsForService_error_(self, peripheral, service, error):
print "discovered characteristic: ", service, service.characteristics
for i in range(service.characteristics.count()):
ch = service.characteristics.objectAtIndex_(i)
if ch.UUID.UUIDString.cString() == '2B01':
peripheral.setNotifyValue_forCharacteristic_(True, ch)
print "set notify for chr {}".format(i)

@protocol('CBPeripheralDelegate')
def peripheral_didUpdateNotificationStateForCharacteristic_error_(
self, peripheral, characteristic, error):
# probably needs to decode like for advertising
# sensor = c.get_from_ptr(values.bytes().arg_ref, 'c', values.length())
print "characteristic: {} notifying: {}".format(characteristic.UUID.UUIDString.cString(), characteristic.isNotifying)
pass

@protocol('CBPeripheralDelegate')
def peripheral_didUpdateValueForCharacteristic_error_(self, peripheral, characteristic, error):
# probably needs to decode like for advertising
# sensor = c.get_from_ptr(values.bytes().arg_ref, 'c', values.length())
data = c.get_from_ptr(characteristic.value.bytes().arg_ref, 'c', characteristic.value.length())
name = peripheral.name.cString()
peripheral.readRSSI()
rssi = peripheral.RSSI and peripheral.RSSI.intValue() or 0
if self.callback:
if self.queue is not None:
self.queue.append((rssi, name, data))

else:
self.callback(rssi, name, data)
else:
print name, rsii, data

最佳答案

这意味着您的协议(protocol)方法之一存在错误,例如 centralManager_didDiscoverPeripheral_advertisementData_RSSI_()。但您没有提供任何代码。

我现在也更新了我的 plyer BLE 代码 - 它支持连接和基本的特征读取/写入。这是一项正在进行中的工作,代码相当困惑,因为它是我的 BLE 测试和学习环境。您可以在这里查看:https://github.com/kivy/plyer/pull/185

如果您尝试为此编写自己的代码,请记住以下几点。首先,确保在连接时保留对 CBPeripheral 对象的引用 - 否则该对象将被释放,并且您的连接将默默失败。其次,确保通过 peripheral.setDelegate_(self) 设置 CBPeripheralDelegate 而不是 peripheral.delegate = self - 后者将不起作用,并且您的协议(protocol)方法将不会被调用。最后,pyobjus 中存在一个将字节传递给 void* 参数的错误 - 这将阻止您创建二进制 NSData 对象来读取/写入特征值。不过,我有一个针对该错误的 PR ( https://github.com/kivy/pyobjus/pull/26 )。

关于python - 异常断言错误: AssertionError() in 'pyobjus.protocol_forwardInvocation' ignored,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36551634/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com