<等待翻译>Android Wear 进阶 2.6 Debugging over Bluetooth 通过蓝牙调试

本文介绍如何通过蓝牙连接手持设备与穿戴设备进行调试。首先在手持设备上启用USB调试,在穿戴设备上启用蓝牙调试。然后通过Android Wear应用设置蓝牙调试,并使用ADB命令建立连接。最后介绍如何运行ADB命令来调试应用。

Debugging over Bluetooth

You can debug your wearable over Bluetooth by routing its debug output to the handheld device that's connected to your development machine.

Setup Devices for Debugging



  1. Enable USB debugging on the handheld:
    • Open the Settings app and scroll to the bottom.
    • If it doesn't have a Developer Options setting, tap About Phone (or About Tablet), scroll to the bottom, and tap the build number 7 times.
    • Go back and tap Developer Options.
    • Enable USB debugging.
  2. Enable Bluetooth debugging on the wearable:
    1. Tap the home screen twice to bring up the Wear menu.
    2. Scroll to the bottom and tap Settings.
    3. Scroll to the bottom. If there's no Developer Options item, tap About, and then tap the build number 7 times.
    4. Tap the Developer Options item.
    5. Enable Debug over Bluetooth.

Set Up a Debugging Session



  1. On the handheld, open the Android Wear companion app.
  2. Tap the menu on the top right and select Settings.
  3. Enable Debugging over Bluetooth. You should see a tiny status summary appear under the option:
    Host: disconnected
    Target: connected
  4. Connect the handheld to your machine over USB and run:
    adb forward tcp:4444 localabstract:/adb-hub
    adb connect localhost:4444

    Note: You can use any available port that you have access to.

In the Android Wear companion app, you should see the status change to:

Host: connected
Target: connected

Debug Your App



Your wearable should show up as  localhost:4444 when running  adb devices. To run any  adb command, use this format:
adb -s localhost:4444 <command> 

If there are no other devices connected over TCP/IP (namely emulators), you can shorten the command to:

adb -e <command>

For example:

adb -e logcat
adb -e shell
adb -e bugreport
<think>好的,我现在需要帮助用户解决如何使用cdt-java-client 4.0.0连接到已运行的浏览器实例的问题。首先,我要回忆一下cdt-java-client的相关知识。cdt-java-client是Chrome DevTools协议的Java客户端库,用于与Chrome或Chromium浏览器进行交互。用户的问题是关于连接到已经运行的浏览器实例,而不是启动新的实例。 首先,我需要确定连接到已运行实例的关键步骤。通常,这需要知道浏览器的调试端口,并且确保浏览器是以远程调试模式启动的。比如,用户可能需要通过命令行启动浏览器时指定--remote-debugging-port参数,例如chrome --remote-debugging-port=9222。这样,浏览器会监听该端口,允许外部客户端连接。 接下来,用户需要使用cdt-java-client库中的相关类来建立连接。可能涉及到的类包括BrowserFactory、ChromeClient等。我需要查找cdt-java-client的文档或示例,确认如何正确实例化这些类并连接到指定的端口。 然后,思考可能的代码结构。用户需要获取已打开的浏览器的WebSocket URL,通常可以通过访问http://localhost:9222/json/list来获取活动页面的信息。之后,使用该URL连接到特定的页面或浏览器实例。 还需要考虑版本兼容性,用户提到的是4.0.0版本,所以需要确保示例代码适用于该版本,避免使用过时的方法或类。可能需要查阅4.0.0版本的文档或GitHub仓库中的示例。 另外,用户提供的引用中提到了官网的sh例子用于启动Jenkins,这可能与启动浏览器实例无关,但需要注意是否有其他相关的引用或配置需要提及。 最后,生成相关问题时要围绕连接已运行实例的常见疑问,比如如何获取调试端口、如何处理多标签页、如何复用现有实例等。 总结步骤: 1. 确保浏览器以调试模式运行。 2. 获取浏览器的WebSocket调试URL。 3. 使用cdt-java-client的API建立连接。 4. 提供示例代码,并解释关键部分。 5. 验证代码的正确性和版本适用性。 6. 生成相关问题,帮助用户深入理解。</think>### 如何使用 `cdt-java-client 4.0.0` 连接到已运行的浏览器实例? #### 步骤说明 1. **启动浏览器时启用调试模式** 浏览器需通过命令行参数启用远程调试端口(例如 `9222`): ```bash chrome --remote-debugging-port=9222 --no-first-run --no-default-browser-check ``` 若已运行实例未启用调试模式,需重启浏览器[^1]。 2. **获取浏览器调试信息** 访问 `http://localhost:9222/json/list` 获取已打开的页面或浏览器实例的 WebSocket URL,例如: ```json [{ "description": "Chrome DevTools", "devtoolsFrontendUrl": "...", "id": "F8B4E4E1...", "webSocketDebuggerUrl": "ws://localhost:9222/devtools/browser/..." }] ``` 3. **使用 `cdt-java-client` 连接** 通过 `BrowserFactory` 和 `WebSocketService` 连接到指定 WebSocket URL: ```java import com.github.kklisura.cdt.services.ChromeService; import com.github.kklisura.cdt.services.types.ChromeTab; public class ConnectToRunningInstance { public static void main(String[] args) { // 指定调试端口 int port = 9222; // 创建服务并连接到已运行的浏览器 ChromeService service = ChromeService.create(port); List<ChromeTab> tabs = service.getTabs(); // 连接到第一个标签页 ChromeTab tab = tabs.get(0); tab.connect(); // 执行操作(例如获取页面标题) String title = tab.getPage().getTitle(); System.out.println("Title: " + title); // 关闭连接 service.close(); } } ``` #### 关键点说明 - **`ChromeService.create(port)`**:直接通过端口连接到浏览器实例,无需手动解析 WebSocket URL。 - **复用现有实例**:若浏览器已运行且启用了调试端口,`cdt-java-client` 可自动发现并连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值