Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot receive correct data using simplepyble #328

Open
ranmukexin opened this issue Jul 11, 2024 · 1 comment
Open

cannot receive correct data using simplepyble #328

ranmukexin opened this issue Jul 11, 2024 · 1 comment

Comments

@ranmukexin
Copy link

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
Copy link
Author

picc
This is how my module is wired

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant