-
-
Save wolfteeth/9baf0133c582fc08e518 to your computer and use it in GitHub Desktop.
Test scripts to detect Sphero BB-8 over BLE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import unicode_literals | |
import Adafruit_BluefruitLE.services.servicebase as servicebase | |
ROBOT_SERVICE_UUID = "22bb746f2bb075542d6f726568705327" | |
class RobotService(servicebase.ServiceBase): | |
ADVERTISED = [ROBOT_SERVICE_UUID] | |
SERVICES = [ROBOT_SERVICE_UUID] | |
CHARACTERISTICS = [ROBOT_SERVICE_UUID] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function, unicode_literals | |
import Adafruit_BluefruitLE as btle | |
import spheroservice | |
import atexit | |
import time | |
provider = btle.get_provider() | |
def main(): | |
# Clear any cached data because both bluez and CoreBluetooth have issues with | |
# caching data and it going stale. | |
provider.clear_cached_data() | |
# Get the first available BLE network adapter and make sure it's powered on. | |
adapter = provider.get_default_adapter() | |
adapter.power_on() | |
print('Using adapter: {0}'.format(adapter.name)) | |
# Start scanning with the bluetooth adapter. | |
adapter.start_scan() | |
# Use atexit.register to call the adapter stop_scan function before quiting. | |
# This is good practice for calling cleanup code in this main function as | |
# a try/finally block might not be called since this is a background thread. | |
atexit.register(adapter.stop_scan) | |
print('Searching for devices...') | |
# Enter a loop and print out whenever a new UART device is found. | |
known_devices = set() | |
bb8 = None | |
while True: | |
# Call UART.find_devices to get a list of any UART devices that | |
# have been found. This call will quickly return results and does | |
# not wait for devices to appear. | |
found = set(spheroservice.RobotService.find_devices()) | |
# Check for new devices that haven't been seen yet and print out | |
# their name and ID (MAC address on Linux, GUID on OSX). | |
new = found - known_devices | |
for device in new: | |
print('Found service: {0} [{1}]'.format(device.name, device.id)) | |
if device.name.startswith("BB-8"): | |
print("Found BB-8") | |
bb8 = device | |
adapter.stop_scan() | |
break | |
known_devices.update(new) | |
if bb8: | |
break | |
# Sleep for a second and see if new devices have appeared. | |
time.sleep(1.0) | |
print("Connecting to BB-8") | |
bb8.connect() | |
print("Connected:", bb8.is_connected) | |
print("Advertised services:", bb8.advertised) | |
print("Disconnecting...") | |
bb8.disconnect() | |
print("Done.") | |
provider.initialize() | |
provider.run_mainloop_with(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment