OpenBluetoothToolbox / SimpleBLE

SimpleBLE - the all-in-one Bluetooth library for MacOS, iOS, Windows, Linux and Android.
https://www.simpleble.org
Other
657 stars 110 forks source link

cannot receive correct data using simplepyble #328

Open ranmukexin opened 1 month ago

ranmukexin commented 1 month ago

I use Bluetooth low power module to connect the computer through usb to ttl, and the computer always uses the corresponding COM port to send data to the module. Using the mobile app test, I can normally connect and receive data, but when using this code modified from the read case program, I cannot parse and receive the correct data. I do not know where the problem is. The following is my code, in which connect_peripheral, service_uuid and characteristic_uuid are all right. I have checked my module that can be connected, but I can't receive data. The code is as follows:

import simplepyble
import time
connect_peripheral="12:29:4b:0f:27:34"
service_uuid= "00001800-0000-1000-8000-00805f9b34fb"
characteristic_uuid = "00002a00-0000-1000-8000-00805f9b34fb"
scan_flag = 0
myperipheral = None
if __name__ == "__main__":
    adapters = simplepyble.Adapter.get_adapters()

    if len(adapters) == 0:
        print("No adapters found")

    adapter = adapters[0]

    print(f"Selected adapter: {adapter.identifier()} [{adapter.address()}]")

    while(True):
        adapter.set_callback_on_scan_start(lambda: print("Scan started."))
        adapter.set_callback_on_scan_stop(lambda: print("Scan complete."))
        adapter.set_callback_on_scan_found(lambda peripheral: print(f"Found {peripheral.identifier()} [{peripheral.address()}]"))

        # Scan for 5 seconds
        adapter.scan_for(5000)
        peripherals = adapter.scan_get_results()
        print("Please select a peripheral:")
        for i, peripheral in enumerate(peripherals):
            print(f"{i}: {peripheral.identifier()} [{peripheral.address()}]")
            if(connect_peripheral == peripheral.address()):
                myperipheral = peripheral
                scan_flag = 1
        if(scan_flag==1):
            break

    print(f"Connecting to: {myperipheral.identifier()} [{myperipheral.address()}]")

    myperipheral.connect()
    print("Reading characteristic...")
    while True:

        contents = myperipheral.read(service_uuid, characteristic_uuid)
        # if(contents!=b'JDY-31-SPP'):
        print(f"Contents: {contents}")
        #延迟1s
        time.sleep(1)

myperipheral.disconnect()    

The following output is displayed:

Contents: b'JDY-31-SPP'
Contents: b'JDY-31-SPP'
Contents: b'JDY-31-SPP'
ranmukexin commented 1 month ago

picc This is how my module is wired