google / bumble

Apache License 2.0
246 stars 70 forks source link

Android Studio Emulator Bluetooth not working #331

Open McMelonTV opened 10 months ago

McMelonTV commented 10 months ago

Hi, I tried to get bluetooth to work through bumble in my android emulator so I followed the instructions at https://google.github.io/bumble/platforms/android.html#connecting-a-custom-virtual-controller but I am having a problem where the bluetooth mac address just switches back and forth from 00:00:00:00:00:00 to unavailable and the bluetooth is just constantly turning on and off. I'm not sure whether I messed up or if it's an issue with my bluetooth dongle or if it's a bug. Any help would be appreciated

https://github.com/google/bumble/assets/62901799/5b747923-87e2-4044-b380-822f7ef26947

McMelonTV commented 10 months ago

Oh also, I'm using windows 10 if that matters

McMelonTV commented 10 months ago

I managed to get the mac address to work by following these instructions instead https://google.github.io/bumble/platforms/android.html#connecting-to-netsim but it still cant find any available devices

barbibulle commented 10 months ago

Hi. Which version of the emulator are you using? (you can see it from the 'SDK Tools' tab in the 'SDK Manager' window from Android Studio). One thing you may want to try also is to start the Bumble HCI bridge first, then boot/reboot the emulator. This will ensure that you start with a fresh Android Bluetooth stack, in case toggling BT on/off doesn't completely clean the state. Also, the second thing you've tried (connecting to netsim) is a different mode: that won't allow you to connect to a physical USB dongle. Netsim behaves as a virtual BT radio. You can connect multiple virtual devices to netsim, including the Android emulator as well as Bumble virtual devices/apps, but you can't bridge the netsim virtual radio and a USB physical radio. It's either or. In summary: either 1: Android <-> HCI-bridge <-> USB dongle <-> physical device. (that's the "controller" mode you tried first), or 2: Android <-> Netsim <-> Some other virtual device (that's the second case here).

McMelonTV commented 10 months ago

Ohhh, that makes sense. I use emulator version 34.1.10 and I have tried starting the HCI bridge before starting the emulator but that resulted in the same weird null mac.

McMelonTV commented 10 months ago

In the emulator I use this OS version image

barbibulle commented 10 months ago

I have tried to replicate this issue, but wasn't able to so far. I'm running version 34.1.11 of the emulator, but that shouldn't make a big difference. Running on a mac, the emulator does connect to my USB dongle via a Bumble HCI bridge, and I can scan/connect to local devices. Just to be sure we're looking at the same thing, could you try updating to version 34.1.11 of the emulator and trying again?

Shmuel930 commented 8 months ago

I have tried to replicate this issue but wasn't able to so far. I'm running version 34.1.11 of the emulator, but that shouldn't make a big difference. Running on a Mac, the emulator does connect to my USB dongle via a Bumble HCI bridge, and I can scan/connect to local devices. Just to be sure we're looking at the same thing, could you try updating to version 34.1.11 of the emulator and trying again?

Hey, joining to the question here. I'm also trying to get my Android emulator to work with Bluetooth. I have a physical Bluetooth dongle that is connected to my MacBook (via a small adapter from USB 2.0 -> Type C)

I can see the details of the dongle when running lsusb "Bus 002 Device 002: ID 0bda:8771 Realtek Semiconductor Corp. Bluetooth Radio Serial: 00E04C239987"

But I'm still getting the same issue that the guy who posted this question got. I'm running bumble-hci-bridge android-netsim:_:8877,mode=controller usb:0

Then run the emulator on another shell like that: emulator -packet-streamer-endpoint localhost:8877 -avd Pixel_6_API_33 -no-snapshot-load

It seems like when the phone boot, the hci-bridge console starts printing lots of messages (like in the video of the question) but I can't find any device when I try to discover it.

my emulator version is 33.1.23.0 and I can't see a way to update it (when I click "check for updates" Android Studio tells me there is no update pending)

Would you be able to regrade this?

EDIT: I've tried to manually download android emulator (with your version, 34.4.11, same issue)

barbibulle commented 8 months ago

The issue here may be that the USB dongle you’re using may need a special driver to load a binary firmware image/patch to it. Bumble does include such a driver for Realtek devices, but that works when you use the Bumble stack with the USB dongle. In your case, the Bumble host stack isn’t used, but the Android host stack is used instead, only using the HCI transport part of the Bumble library. The Android host stack doesn’t know how to initialize the dongle by loading FW into it. however, it should be possible to use Bumble’s bumble-rtk-util tool to load the FW, then start the HCI bridge. The FW should stay loaded until you unpower/unplug the dongle. See https://google.github.io/bumble/drivers/realtek.html for details on how to obtain the FW files and use the comma and line tool.

Shmuel930 commented 8 months ago

The issue here may be that the USB dongle you’re using may need a special driver to load a binary firmware image/patch to it. Bumble does include such a driver for Realtek devices, but that works when you use the Bumble stack with the USB dongle. In your case, the Bumble host stack isn’t used, but the Android host stack is used instead, only using the HCI transport part of the Bumble library. The Android host stack doesn’t know how to initialize the dongle by loading FW into it. however, it should be possible to use Bumble’s bumble-rtk-util tool to load the FW, then start the HCI bridge. The FW should stay loaded until you unpower/unplug the dongle. See https://google.github.io/bumble/drivers/realtek.html for details on how to obtain the FW files and use the comma and line tool.

Thank you so much, it worked :)

stalniy commented 5 months ago

I'm in the same situation. Bridge console shows found Bleutooth devices, but Android emulator doesn't see anything:

$ sudo  python tools/rtk_util.py  info usb:0
Driver:
  ROM:      8761
  Firmware: rtl8761bu_fw.bin
  Config:   rtl8761bu_config.bin

$ sudo  python tools/rtk_util.py  load usb:0
<no output>

$ sudo python apps/hci_bridge.py android-netsim:_:8877,mode=controller usb:0
>>> connecting to HCI...
>>> connected
>>> connecting to HCI...
>>> connected

Then I do

emulator -packet-streamer-endpoint localhost:8877 -avd Pixel_3a_API_34_extension_level_7_x86_64 -no-snapshot-load

Then I see discovered by Bluetooth devices in my computer terminal:

INFO:bumble.bridge:[CONTROLLER->HOST] HCI_LE_EXTENDED_ADVERTISING_REPORT_EVENT:
0:
  event_type:                    0x0010 [LEGACY_ADVERTISING_PDU_USED,COMPLETE](ADV_NONCONN_IND)
  address_type:                  PUBLIC_DEVICE_ADDRESS
  address:                       38:98:00:00:62:20/P
  primary_phy:                   LE 1M
  secondary_phy:                 0
  advertising_sid:               255
  tx_power:                      127
  rssi:                          -66
  periodic_advertising_interval: 0
  direct_address_type:           PUBLIC_DEVICE_ADDRESS
  direct_address:                00:00:00:00:00:00/P
  data:                          [Complete List of 16-bit Service Class UUIDs]: UUID-16:27A5, [Shortened Local Name]: "BR", [Manufacturer Specific Data]: company=0x1E28, data=1b015135e0

but it seems emulator doesn't see anything. however I can discover it from my iPhone.