iOS: Connect via Bluetooth

Demo: Native iOS example (WKWebView + CoreBluetooth) โ†’ native-ios-example

This page shows how to integrate @onekeyfe/hd-common-connect-sdk in a native iOS host via a low-level adapter. The JavaScript bundle runs in WKWebView; transport calls are forwarded to native CoreBluetooth and bridged back to JS.

Key stack:

  • CoreBluetooth (system)

  • WKWebViewJavascriptBridge (CocoaPods) for JS โ†” Native messaging

Info.plist (permissions):

  • NSBluetoothAlwaysUsageDescription

  • (Older iOS) NSBluetoothPeripheralUsageDescription

OneKey BLE UUIDs:

  • Service: 00000001-0000-1000-8000-00805f9b34fb

  • Write: 00000002-0000-1000-8000-00805f9b34fb

  • Notify: 00000003-0000-1000-8000-00805f9b34fb

Step 1. Pod and web assets

Podfile (add the bridge dependency):

platform :ios, '13.0'
use_frameworks!

target 'YourAppTarget' do
  pod 'WKWebViewJavascriptBridge'
end

Build the web bundle (already implemented in the demo):

Copy web assets into your app target resources (two options):

  • Option A (keep folder; recommended for clarity):

    • Copy the entire web/web_dist/ folder into your Xcode project (e.g., a group named web/web_dist) and ensure it is included in "Copy Bundle Resources".

    • Load path: web/web_dist/index.html.

  • Option B (flatten):

    • Copy all files inside web/web_dist/ directly into your app bundle root (or a chosen assets folder).

    • Load the matching path (e.g., index.html).

Keep the HTML <script src="..."> paths consistent with your placement.

Step 2. State and handler names

Step 3. WKWebView + Bridge + load HTML (initialization order matters)

  • Create the WKWebView

  • Create the bridge

  • Register all native handlers (enumerate/connect/disconnect/send/monitorCharacteristic)

  • Load the HTML (from the bundled web/web_dist/)

Step 4. Bridge handlers (enumerate / connect / disconnect / send)

Step 5. CoreBluetooth scanning and notifications

Step 6. JavaScript bundle (low-level adapter)

The demoโ€™s web project (under native-ios-example/web) already builds a bundle that initializes the SDK with env: 'lowlevel' and wires the low-level adapter. You typically do NOT need to write extra JS โ€” just build and include the web/web_dist/ directory in your app resources so it can be loaded (e.g., web/web_dist/index.html).

If you customize the adapter, keep the same pattern: initialize with env: 'lowlevel' and forward enumerate/connect/disconnect/send/receive to the native bridge.

Step 7. (Optional) Native UI prompts bridging

If you want to present native PIN/confirmation UI instead of handling dialogs only in JS, expose extra handlers so JS can request native UI and receive results. The demo shows this pattern.

In JS, you would call these handlers from your adapter to mirror the demoโ€™s behavior. Otherwise, you can handle UI entirely in JS using UI_EVENT โ€” see Config Event for event wiring and responses (WebUSB guide includes minimal dialogs).

Step 8. Checklist

  • Register handlers before loading the HTML to avoid race conditions.

  • Scan with service UUID filter and stop within a reasonable time window.

  • Persist connectId (peripheral UUID) and fetch device_id with getFeatures(connectId) after the first connection.

  • Always subscribe to UI_EVENT in JS to avoid stalled requests (see Config Event).

  • Keep web/web_dist/ in your app bundle and adjust the load path accordingly.

References

Last updated

Was this helpful?