Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import simplepyble
- from kivy.app import App
- from kivy.uix.button import Button
- from kivy.uix.gridlayout import GridLayout
- class MyGridLayout(GridLayout):
- def __init__(self, **kwargs):
- super(MyGridLayout, self).__init__(cols=3, **kwargs)
- # create the UI components
- self.btn_scan = Button(text="Scan" )
- self.btn_scan.bind(on_press=self.scan_for_peripherals)
- self.btn_connect = Button(text="Connect")
- self.btn_connect.bind(on_press=self.connect_to_peripheral)
- self.btn_connect.disabled = True
- self.btn_disconnect = Button(text="Disconnect")
- self.btn_disconnect.bind(on_press=self.disconnect_from_peripheral)
- self.btn_disconnect.disabled = True
- # add the components to the layout
- self.add_widget(self.btn_scan)
- self.add_widget(self.btn_connect)
- self.add_widget(self.btn_disconnect)
- # create a variable to store the selected adapter and peripheral
- self.adapter = None
- self.peripheral = None
- def scan_for_peripherals(self, instance):
- # get a list of available BLE adapters
- self.adapters = simplepyble.Adapter.get_adapters()
- if len(self.adapters) == 0:
- print("No adapters found")
- return
- # Query the user to pick an adapter
- self.adapter = self.adapters[0]
- print(f"Selected adapter: {self.adapter.identifier()} [{self.adapter.address()}]")
- self.adapter.set_callback_on_scan_start(lambda: print("Scan started."))
- self.adapter.set_callback_on_scan_stop(lambda: print("Scan complete."))
- self.adapter.set_callback_on_scan_found(lambda peripheral: print(f"Found {peripheral.identifier()} [{peripheral.address()}]"))
- # Scan for 5 seconds
- self.adapter.scan_for(5000)
- self.peripherals = self.adapter.scan_get_results()
- # Query the user to pick a peripheral
- print("Please select a peripheral:")
- for i, peripheral in enumerate(self.peripherals):
- print(f"{i}: {peripheral.identifier()} [{peripheral.address()}]")
- choice = int(input("Enter choice: "))
- self.peripheral = self.peripherals[choice]
- self.btn_connect.disabled = False
- def connect_to_peripheral(self, instance):
- if self.peripheral is None:
- print("No peripheral selected")
- return
- print(f"Connecting to: {self.peripheral.identifier()} [{self.peripheral.address()}]")
- self.peripheral.connect()
- print("Successfully connected, listing services...")
- services = self.peripheral.services()
- for service in services:
- print(f"Service: {service.uuid()}")
- for characteristic in service.characteristics():
- print(f" Characteristic: {characteristic.uuid()}")
- self.btn_disconnect.disabled = False
- def disconnect_from_peripheral(self, instance):
- if self.peripheral is None:
- print("No peripheral selected")
- return
- self.peripheral.disconnect()
- self.peripheral = None
- self.adapter = None
- self.btn_connect.disabled = True
- self.btn_disconnect.disabled = True
- class MyApp(App):
- def build(self):
- return MyGridLayout()
- if __name__ == '__main__':
- MyApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement