Author Topic: Using Pi Zero W as Controller - Need Help Initializing Connection to Laptop  (Read 4124 times)

0 Members and 1 Guest are viewing this topic.

Offline xudongz

  • Thread Starter
  • Posts: 37
    • Personal Site
I am using a Raspberry Pi Zero W to emulate a keyboard over Bluetooth. Currently running Raspbian 8 with Linux 4.14.98+ #1200 along with BlueZ 5.43.2. I can connect to it from various devices by initializing the connection on those devices. However, I'm unable to initialize the connection from the Pi Zero. For example, my Bluetooth mouse is able to remain disconnected from my computer until I move it, at which point it establishes the Bluetooth connection and the mouse begins working.

How do I go about doing something like that with the Bluetooth keyboard that I'm emulating with the Pi Zero? Specifically I want to get it working with my MacBook Air. Obviously my laptop will frequently move out of range and will need to reestablish the connection once it's back in range. This would be useful so I can initialize the connection only when a key is pressed.

I am running code similar to https://github.com/mlabviet/BL_keyboard_RPI. Essentially there's a Python script serving the SDP record using BlueZ's API, a process listening on PSM 17, and another listening on PSM 19.

I've tried to initialize the connection using bluetoothctl and executing "connect 01:23:45:01:23:45" with the MAC address of the MacBook Air. When I do that, the Mac's Bluetooth page in System Preferences shows it as connected for a couple of seconds before going back to disconnected. Within the bluetoothd process, I get the following output upon the initial connection

        bluetoothd[537]: src/device.c:connect_profiles() /org/bluez/hci0/dev_01_23_45_01_23_45 (all), client :1.56
        bluetoothd[537]: src/device.c:connect_profiles() Resolving services for /org/bluez/hci0/dev_01_23_45_01_23_45
        bluetoothd[537]: src/adapter.c:connected_callback() hci0 device 01:23:45:01:23:45 connected eir_len 8

I then get the following response when the disconnect occurs

        bluetoothd[537]: src/device.c:search_cb() 01:23:45:01:23:45: No service update
        bluetoothd[537]: src/device.c:device_svc_resolved() /org/bluez/hci0/dev_01_23_45_01_23_45 err 0
        bluetoothd[537]: src/device.c:connect_profiles() /org/bluez/hci0/dev_01_23_45_01_23_45 (all), client :1.56
        bluetoothd[537]: src/adapter.c:dev_disconnected() Device 01:23:45:01:23:45 disconnected, reason 2
        bluetoothd[537]: src/adapter.c:adapter_remove_connection()
        bluetoothd[537]: src/adapter.c:bonding_attempt_complete() hci0 bdaddr 01:23:45:01:23:45 type 0 status 0xe
        bluetoothd[537]: src/device.c:device_bonding_complete() bonding (nil) status 0x0e
        bluetoothd[537]: src/device.c:device_bonding_failed() status 14
        bluetoothd[537]: src/adapter.c:resume_discovery()

For comparison when I initialize the connection from the MacBook itself, I get

        bluetoothd[537]: src/adapter.c:connected_callback() hci0 device 01:23:45:01:23:45 connected eir_len 5

When I disconnect manually, I get

        bluetoothd[537]: src/adapter.c:dev_disconnected() Device 01:23:45:01:23:45 disconnected, reason 3
        bluetoothd[537]: src/adapter.c:adapter_remove_connection()
        bluetoothd[537]: src/adapter.c:bonding_attempt_complete() hci0 bdaddr 01:23:45:01:23:45 type 0 status 0xe
        bluetoothd[537]: src/device.c:device_bonding_complete() bonding (nil) status 0x0e
        bluetoothd[537]: src/device.c:device_bonding_failed() status 14
        bluetoothd[537]: src/adapter.c:resume_discovery()

Any ideas on how I can initialize the connection from the Pi Zero? Is there something I need to change in the SDP record?

What is going on under the hood when I run "connect 01:23:45:01:23:45"? Is there some other way in which I should be initializing the connection from the Pi Zero? Do I need to establish some L2CAP connection manually?

Any help would be appreciated.
SliceMK | ErgoDox Wireless group buy round 2 running until November 16, 2021