Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
Robobin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
plw1g21
Robobin
Commits
0510219d
Commit
0510219d
authored
5 months ago
by
Paul-Winpenny
Browse files
Options
Downloads
Patches
Plain Diff
Create RPiBluetooth.py
parent
a564f3f6
No related branches found
Branches containing commit
No related tags found
1 merge request
!1
App now has a basic structure and BLE support
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ButtonControls/RPiBluetooth.py
+64
-0
64 additions, 0 deletions
ButtonControls/RPiBluetooth.py
with
64 additions
and
0 deletions
ButtonControls/RPiBluetooth.py
0 → 100644
+
64
−
0
View file @
0510219d
import
dbus
from
dbus.mainloop.glib
import
DBusGMainLoop
from
gi.repository
import
GLib
ADVERTISING_UUID
=
'
00001801-0000-1000-8000-00805f9b34fb
'
def
register_ad_cb
():
print
(
"
Advertisement registered
"
)
def
register_ad_error_cb
(
error
):
print
(
f
"
Failed to register advertisement:
{
error
}
"
)
def
register_advertisement
(
adapter
,
advertisement
):
ad_manager
=
dbus
.
Interface
(
adapter
,
'
org.bluez.LEAdvertisingManager1
'
)
ad_manager
.
RegisterAdvertisement
(
advertisement
.
get_path
(),
{},
reply_handler
=
register_ad_cb
,
error_handler
=
register_ad_error_cb
)
class
BLEAdvertisement
(
dbus
.
service
.
Object
):
def
__init__
(
self
,
bus
,
index
):
self
.
path
=
f
"
/org/bluez/example/advertisement
{
index
}
"
self
.
props
=
{
'
Type
'
:
dbus
.
String
(
'
peripheral
'
),
'
ServiceUUIDs
'
:
dbus
.
Array
([
dbus
.
String
(
ADVERTISING_UUID
)]),
'
LocalName
'
:
dbus
.
String
(
'
MyRaspberryPi
'
)
}
super
().
__init__
(
bus
,
self
.
path
)
def
get_path
(
self
):
return
dbus
.
ObjectPath
(
self
.
path
)
@dbus.service.method
(
'
org.bluez.LEAdvertisement1
'
,
in_signature
=
''
,
out_signature
=
'
a{sv}
'
)
def
GetProperties
(
self
):
return
self
.
props
@dbus.service.method
(
'
org.bluez.LEAdvertisement1
'
,
in_signature
=
''
,
out_signature
=
''
)
def
Release
(
self
):
print
(
f
"
{
self
.
path
}
: Released
"
)
def
main
():
DBusGMainLoop
(
set_as_default
=
True
)
bus
=
dbus
.
SystemBus
()
# Access the Bluetooth adapter
adapter
=
dbus
.
Interface
(
bus
.
get_object
(
'
org.bluez
'
,
'
/org/bluez/hci0
'
),
'
org.bluez.Adapter1
'
)
# Make sure the adapter is powered on
adapter_props
=
dbus
.
Interface
(
bus
.
get_object
(
'
org.bluez
'
,
'
/org/bluez/hci0
'
),
'
org.freedesktop.DBus.Properties
'
)
adapter_props
.
Set
(
'
org.bluez.Adapter1
'
,
'
Powered
'
,
dbus
.
Boolean
(
1
))
# Create the BLE advertisement and register it
advertisement
=
BLEAdvertisement
(
bus
,
0
)
ad_manager
=
dbus
.
Interface
(
bus
.
get_object
(
'
org.bluez
'
,
'
/org/bluez/hci0
'
),
'
org.bluez.LEAdvertisingManager1
'
)
ad_manager
.
RegisterAdvertisement
(
advertisement
.
get_path
(),
{},
reply_handler
=
register_ad_cb
,
error_handler
=
register_ad_error_cb
)
print
(
"
BLE Advertising started. Press Ctrl+C to stop.
"
)
mainloop
=
GLib
.
MainLoop
()
mainloop
.
run
()
if
__name__
==
'
__main__
'
:
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment