Advertisement
SnoopSheep2001

ble service

Mar 22nd, 2023
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. import simplepyble
  2. from kivy.app import App
  3. from kivy.uix.button import Button
  4. from kivy.uix.gridlayout import GridLayout
  5.  
  6.  
  7. class MyGridLayout(GridLayout):
  8. def __init__(self, **kwargs):
  9. super(MyGridLayout, self).__init__(cols=3, **kwargs)
  10.  
  11. # create the UI components
  12. self.btn_scan = Button(text="Scan" )
  13. self.btn_scan.bind(on_press=self.scan_for_peripherals)
  14.  
  15. self.btn_connect = Button(text="Connect")
  16. self.btn_connect.bind(on_press=self.connect_to_peripheral)
  17. self.btn_connect.disabled = True
  18.  
  19. self.btn_disconnect = Button(text="Disconnect")
  20. self.btn_disconnect.bind(on_press=self.disconnect_from_peripheral)
  21. self.btn_disconnect.disabled = True
  22.  
  23. # add the components to the layout
  24. self.add_widget(self.btn_scan)
  25. self.add_widget(self.btn_connect)
  26. self.add_widget(self.btn_disconnect)
  27.  
  28. # create a variable to store the selected adapter and peripheral
  29. self.adapter = None
  30. self.peripheral = None
  31.  
  32.  
  33. def scan_for_peripherals(self, instance):
  34. # get a list of available BLE adapters
  35. self.adapters = simplepyble.Adapter.get_adapters()
  36.  
  37. if len(self.adapters) == 0:
  38. print("No adapters found")
  39. return
  40.  
  41. # Query the user to pick an adapter
  42. self.adapter = self.adapters[0]
  43.  
  44.  
  45.  
  46.  
  47. print(f"Selected adapter: {self.adapter.identifier()} [{self.adapter.address()}]")
  48.  
  49. self.adapter.set_callback_on_scan_start(lambda: print("Scan started."))
  50. self.adapter.set_callback_on_scan_stop(lambda: print("Scan complete."))
  51. self.adapter.set_callback_on_scan_found(lambda peripheral: print(f"Found {peripheral.identifier()} [{peripheral.address()}]"))
  52.  
  53. # Scan for 5 seconds
  54. self.adapter.scan_for(5000)
  55. self.peripherals = self.adapter.scan_get_results()
  56.  
  57. # Query the user to pick a peripheral
  58. print("Please select a peripheral:")
  59. for i, peripheral in enumerate(self.peripherals):
  60. print(f"{i}: {peripheral.identifier()} [{peripheral.address()}]")
  61.  
  62. choice = int(input("Enter choice: "))
  63. self.peripheral = self.peripherals[choice]
  64.  
  65. self.btn_connect.disabled = False
  66.  
  67. def connect_to_peripheral(self, instance):
  68. if self.peripheral is None:
  69. print("No peripheral selected")
  70. return
  71.  
  72. print(f"Connecting to: {self.peripheral.identifier()} [{self.peripheral.address()}]")
  73. self.peripheral.connect()
  74.  
  75. print("Successfully connected, listing services...")
  76. services = self.peripheral.services()
  77. for service in services:
  78. print(f"Service: {service.uuid()}")
  79. for characteristic in service.characteristics():
  80. print(f" Characteristic: {characteristic.uuid()}")
  81.  
  82. self.btn_disconnect.disabled = False
  83.  
  84. def disconnect_from_peripheral(self, instance):
  85. if self.peripheral is None:
  86. print("No peripheral selected")
  87. return
  88.  
  89. self.peripheral.disconnect()
  90. self.peripheral = None
  91. self.adapter = None
  92.  
  93. self.btn_connect.disabled = True
  94. self.btn_disconnect.disabled = True
  95.  
  96.  
  97. class MyApp(App):
  98. def build(self):
  99. return MyGridLayout()
  100.  
  101.  
  102. if __name__ == '__main__':
  103. MyApp().run()
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement