从低功耗蓝牙设备射频通信python3读取数据 [英] Reading data from a Bluetooth device low energy rfcomm python3

查看:70
本文介绍了从低功耗蓝牙设备射频通信python3读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到了问题.有一种低功耗的蓝牙设备.BLE.目的是向设备发送命令并取回数据.例如:命令-0x ** 0x ** 0x **,其中第一个0x **-代码命令,第二个0x-数据长度.响应桅杆为-0x ** 0x ** 0x **.我无法将命令发送到设备.该设备由RFCOMM工作.实际上是可用的代码,但没有给出结果-它表示设备已关闭.

Faced a problem. There is a bluetooth device with low power consumption. BLE. The goal is to send a command to the device and get the data back. For example: command - 0x** 0x** 0x**, where first 0x** - code command, second 0x - data lenght. Response mast be - 0x** 0x** 0x**. I can not send a command to the device. The device works by RFCOMM. Actually the code that is available, but it does not give a result - it says that the device is off.

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()

我通过另一个库-代码完成了

I made it through another library - the code:

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中遇到了这个问题,并且知道如何解决?

It gives a connection but does not send a command and does not return a value since this library does not know what RFCOMM is. Perhaps someone faced this problem in the python and knows how to solve it?

推荐答案

RFCOMM是Bluetooth Classic的协议,BLE不支持它.无法使用RFCOMM与BLE设备进行通信.

RFCOMM is a protocol of Bluetooth Classic, BLE does not support it. It is impossible to use RFCOMM for communicating with a BLE device.

您应该阅读BLE的介绍,将使您对BLE有基本了解.还会有更多猜测,这取决于BLE设备的配置方式.

You should read an introduction to BLE, it will give you a basic understanding of BLE. Anything further will be guessing, it depends on how the BLE device is configured.

如果使用自己可以配置的设备,则一种可能性是创建一个支持写入和指示的特征.您可以指示(在特征值更改时通知您,并且新值是什么)并编写命令.该响应将通过指示接收.

If you are using your own device that you can configure, one possibility is to create a characteristic that supports Write and Indicate. You can indicate (to be notified when the characteristic value changes and what is the new value) and write the command. The response will be received via an indication.

这篇关于从低功耗蓝牙设备射频通信python3读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆