1

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?

2 Answers2

3

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.

Icce
  • 41
  • 3
0

For most practical purposes the answer that RFCOMM is only available in Bluetooth "Classic" is true: most Bluetooth LE devices don't support RFCOMM. However, in principle the Bluetooth Core Spec describes how a channel for RFCOMM can be opened between two LE devices using LE credit-based control flow with an L2CAP_LE_CREDIT_BASED_CONNECTION_REQ command (introduced in Core Spec v4.1 Vol 3 Part A Chapter 4.22 and the Assigned Numbers document).

user3482779
  • 15
  • 1
  • 6