ブルーツースのペアリング
PI4 Python3.11でPIの操作を一切せずにパソコンWIN11からの操作でペアリングを完了させたい。
パソコンにペアリング登録されていないPIをペアリングするため、PIに下記のスクリプトを走らせて
パソコンの設定→Bluetoothとデバイス→デバイスの追加→Bluetooth→rasberrypi(デバイス)の表示がでるので
クリックするとPIに正常に接続されました。OKボタンが表示されます。このためOKをクリックしないと次に進めません。
PIにはディスプレイもマウスも接続しない予定な表示を出さないでかつボタンをクリックしないでペアリング完了させその後の
接続→ソケット通信に進んでいきたい。
下記がスクリプトです。
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
import sys
class BluetoothAgent(dbus.service.Object):
def __init__(self, bus):
self.path = "/test/agent"
super().__init__(bus, self.path)
@dbus.service.method("org.bluez.Agent1", in_signature="", out_signature="")
def Release(self):
print("Agent released")
@dbus.service.method("org.bluez.Agent1", in_signature="o", out_signature="")
def RequestAuthorization(self, device):
return
@dbus.service.method("org.bluez.Agent1", in_signature="os", out_signature="")
def RequestConfirmation(self, device, passkey):
return # 何も返さず自動的に承認
@dbus.service.method("org.bluez.Agent1", in_signature="o", out_signature="u")
def RequestPasskey(self, device):
print(f"Passkey requested for device {device}")
return dbus.UInt32(123456) # 固定のパスキーを返す
@dbus.service.method("org.bluez.Agent1", in_signature="osu", out_signature="")
def DisplayPasskey(self, device, passkey, entered):
print(f"Displaying passkey {passkey} for device {device} (entered: {entered})")
@dbus.service.method("org.bluez.Agent1", in_signature="o", out_signature="")
def Cancel(self, device):
print(f"Pairing canceled for device {device}")
def register_agent(bus, agent_path):
obj = bus.get_object("org.bluez", "/org/bluez")
agent_manager = dbus.Interface(obj, "org.bluez.AgentManager1")
agent_manager.RegisterAgent(agent_path, "NoInputNoOutput")
agent_manager.RequestDefaultAgent(agent_path)
print("Bluetooth agent registered and set as default.")
def main():
DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
# Bluetoothエージェントの登録
agent = BluetoothAgent(bus)
register_agent(bus, agent.path)
# Bluetoothアダプタの初期化
adapter_path = "/org/bluez/hci0"
adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path), "org.freedesktop.DBus.Properties")
adapter.Set("org.bluez.Adapter1", "Pairable", dbus.Boolean(1))
adapter.Set("org.bluez.Adapter1", "Discoverable", dbus.Boolean(1))
print("Bluetooth adapter set to pairable and discoverable.")
# プロセスを維持
print("Bluetooth pairing agent is running. Waiting for requests...")
try:
from gi.repository import GLib
loop = GLib.MainLoop()
loop.run()
except KeyboardInterrupt:
print("Exiting...")
sys.exit(0)
if __name__ == "__main__":
main()
パソコンにペアリング登録されていないPIをペアリングするため、PIに下記のスクリプトを走らせて
パソコンの設定→Bluetoothとデバイス→デバイスの追加→Bluetooth→rasberrypi(デバイス)の表示がでるので
クリックするとPIに正常に接続されました。OKボタンが表示されます。このためOKをクリックしないと次に進めません。
PIにはディスプレイもマウスも接続しない予定な表示を出さないでかつボタンをクリックしないでペアリング完了させその後の
接続→ソケット通信に進んでいきたい。
下記がスクリプトです。
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
import sys
class BluetoothAgent(dbus.service.Object):
def __init__(self, bus):
self.path = "/test/agent"
super().__init__(bus, self.path)
@dbus.service.method("org.bluez.Agent1", in_signature="", out_signature="")
def Release(self):
print("Agent released")
@dbus.service.method("org.bluez.Agent1", in_signature="o", out_signature="")
def RequestAuthorization(self, device):
return
@dbus.service.method("org.bluez.Agent1", in_signature="os", out_signature="")
def RequestConfirmation(self, device, passkey):
return # 何も返さず自動的に承認
@dbus.service.method("org.bluez.Agent1", in_signature="o", out_signature="u")
def RequestPasskey(self, device):
print(f"Passkey requested for device {device}")
return dbus.UInt32(123456) # 固定のパスキーを返す
@dbus.service.method("org.bluez.Agent1", in_signature="osu", out_signature="")
def DisplayPasskey(self, device, passkey, entered):
print(f"Displaying passkey {passkey} for device {device} (entered: {entered})")
@dbus.service.method("org.bluez.Agent1", in_signature="o", out_signature="")
def Cancel(self, device):
print(f"Pairing canceled for device {device}")
def register_agent(bus, agent_path):
obj = bus.get_object("org.bluez", "/org/bluez")
agent_manager = dbus.Interface(obj, "org.bluez.AgentManager1")
agent_manager.RegisterAgent(agent_path, "NoInputNoOutput")
agent_manager.RequestDefaultAgent(agent_path)
print("Bluetooth agent registered and set as default.")
def main():
DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
# Bluetoothエージェントの登録
agent = BluetoothAgent(bus)
register_agent(bus, agent.path)
# Bluetoothアダプタの初期化
adapter_path = "/org/bluez/hci0"
adapter = dbus.Interface(bus.get_object("org.bluez", adapter_path), "org.freedesktop.DBus.Properties")
adapter.Set("org.bluez.Adapter1", "Pairable", dbus.Boolean(1))
adapter.Set("org.bluez.Adapter1", "Discoverable", dbus.Boolean(1))
print("Bluetooth adapter set to pairable and discoverable.")
# プロセスを維持
print("Bluetooth pairing agent is running. Waiting for requests...")
try:
from gi.repository import GLib
loop = GLib.MainLoop()
loop.run()
except KeyboardInterrupt:
print("Exiting...")
sys.exit(0)
if __name__ == "__main__":
main()
Jump to
- Community
- General discussion
- Announcements
- Other languages
- Deutsch
- Español
- Français
- Italiano
- Nederlands
- 日本語
- Polski
- Português
- Русский
- Türkçe
- User groups and events
- Raspberry Pi Official Magazine
- Using the Raspberry Pi
- Beginners
- Troubleshooting
- Advanced users
- Assistive technology and accessibility
- Education
- Picademy
- Teaching and learning resources
- Staffroom, classroom and projects
- Astro Pi
- Mathematica
- High Altitude Balloon
- Weather station
- Programming
- C/C++
- Java
- Python
- Scratch
- Other programming languages
- Windows 10 for IoT
- Wolfram Language
- Bare metal, Assembly language
- Graphics programming
- OpenGLES
- OpenVG
- OpenMAX
- General programming discussion
- Projects
- Networking and servers
- Automation, sensing and robotics
- Graphics, sound and multimedia
- Other projects
- Gaming
- Media centres
- AIY Projects
- Hardware and peripherals
- Camera board
- Compute Module
- Official Display
- HATs and other add-ons
- Device Tree
- Interfacing (DSI, CSI, I2C, etc.)
- Keyboard computers (400, 500, 500+)
- Raspberry Pi Pico
- General
- SDK
- MicroPython
- Other RP2040 boards
- Zephyr
- Rust
- AI Accelerator
- AI Camera - IMX500
- Hailo
- Software
- Raspberry Pi OS
- Raspberry Pi Connect
- Raspberry Pi Desktop for PC and Mac
- Beta testing
- Other
- Android
- Debian
- FreeBSD
- Gentoo
- Linux Kernel
- NetBSD
- openSUSE
- Plan 9
- Puppy
- Arch
- Pidora / Fedora
- RISCOS
- Ubuntu
- Ye Olde Pi Shoppe
- For sale
- Wanted
- Off topic
- Off topic discussion