【发布时间】:2018-05-24 07:41:46
【问题描述】:
遇到了问题。有一个低功耗的蓝牙设备。低功耗。目标是向设备发送命令并取回数据。 例如:command - 0x** 0x** 0x**,其中第一个 0x** - 代码命令,第二个 0x - 数据长度。响应桅杆为 - 0x** 0x** 0x**。我无法向设备发送命令。该设备由 RFCOMM 工作。实际上是可用的代码,但它没有给出结果 - 它说设备已关闭。
from bluetooth import *
import socket
class Work:
def __init__(self):
self.rfcon = BluetoothSocket(RFCOMM)
# self.socket = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM)
self.server = '**:**:**:**:**:**'
self.port = 1
self.data = '0x01 0x00 0x00'
def scan_device(self):
connection = self.rfcon.connect((self.server, self.port))
print('con ===>>> ', connection)
senddata = self.rfcon.send(self.data)
print('senddata ====>>>> ', senddata)
data = self.rfcon.recv(256)
if not data:
print("no data!!!")
self.rfcon.close()
else:
print(data)
self.rfcon.close()
if __name__ == '__main__':
a = Work()
a.scan_device()
a.rfcon.close()
我通过另一个库 - 代码:
from bluepy.btle import *
class Work:
def __init__(self):
self.initialize = Peripheral()
def scan_devices(self):
scanner = Scanner()
devices = scanner.scan(10.0)
for dev in devices:
print("Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi))
for (adtype, desc, value) in dev.getScanData():
print(" %s = %s" % (desc, value))
def connect_to_device(self):
print('start con')
connection = self.initialize.connect('**:**:**:**:**:**', 'public')
print('initialize complite')
return connection
def check_device_status(self):
print('test ====>>> ', self.initialize.getCharacteristics())
cmd = '0x01 0x00 0x00 0x20 0x00'.encode('UTF-8')
print(cmd)
status_device = self.initialize.readCharacteristic(cmd)
print('Device status => ', status_device)
def diconnect(self):
self.initialize.disconnect()
if __name__ == '__main__':
a = Work()
a.connect_to_device()
a.check_device_status()
a.diconnect()
它提供连接但不发送命令并且不返回值,因为该库不知道 RFCOMM 是什么。也许有人在python中遇到过这个问题并且知道如何解决它?
【问题讨论】:
标签: python-3.x bluetooth bluetooth-lowenergy