Reading data from a Bluetooth device low energy rfcomm python3

我怕爱的太早我们不能终老 提交于 2020-12-15 01:44:26

问题


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

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?


回答1:


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

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.



来源:https://stackoverflow.com/questions/50503701/reading-data-from-a-bluetooth-device-low-energy-rfcomm-python3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!