# P2P Troubleshooting

P2P establishes a point-to-point channel between the phone and Glass3. It is suitable for files, images, audio/video streams, and remote control data.

## 1. When to use P2P

| Scenario | Use P2P? |
| --- | --- |
| Live video preview | Yes |
| Large file transfer | Yes |
| Image or audio data | Yes |
| Small control command | Bluetooth is enough |
| Connection negotiation | Requires Bluetooth first |

P2P does not replace Bluetooth as the first step. Bluetooth usually connects first, then triggers P2P negotiation.

## 2. Prerequisites

| Requirement | Notes |
| --- | --- |
| Bluetooth | Must be connected first |
| Wi-Fi | Phone Wi-Fi should be enabled |
| Phone hotspot | Recommended off during connection |
| Permissions | Wi-Fi, location, and network permissions |
| Versions | SDK and glasses system should match |

Common permissions:

```xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
```

Permission requirements vary by Android version.

## 3. Connection flow

```text
Phone permission check
  ↓
Bluetooth connected
  ↓
Send P2P request over Bluetooth
  ↓
Phone and glasses negotiate Wi-Fi P2P
  ↓
Establish TCP/UDP channel
  ↓
Transfer files, messages, or AV data
```

## 4. Cannot connect

### Bluetooth is not connected

P2P depends on Bluetooth negotiation. Fix Bluetooth first:

[Bluetooth troubleshooting](./蓝牙问题排查.md)

### Bluetooth connected but P2P fails

Check in order:

1. Phone Wi-Fi is enabled.
2. Phone hotspot is off.
3. App has location and Wi-Fi permissions.
4. P2P is not started too early.
5. SDK and glasses system versions match.
6. No permission dialog or system prompt is blocking the flow.

If it still fails, export phone logs:

```text
/sdcard/Android/data/<phone-app-package>/files/Documents/mobileLog/
```

## 5. P2P disconnects after glasses scan Wi-Fi QR code

This is usually related to Wi-Fi network switching. When glasses connect to a new Wi-Fi or hotspot, the existing P2P channel may be rebuilt or disconnected.

Recommendations:

1. Avoid switching Wi-Fi during P2P transfer.
2. Finish network setup first, then reconnect Bluetooth and P2P.
3. Provide the phone-side `mobileLog` directory if support is needed.

## 6. Keep P2P alive

If your business requires long-running P2P, enable keep-alive after connection succeeds.

```kotlin
GlobalData.p2pConnectState.collect(lifecycleScope) {
    connectStatus(GlobalData.btConnectState.value, it)
    binding.tvDeviceName.text = DeviceLinkerManager.getDeviceName()

    if (it && GlobalData.btConnectState.value) {
        PSecuritySDK.getWifiP2PClientService()
            ?.getIP2PConnectControl()
            ?.getKeepP2PConnectState({ keepAlive ->
                if (!keepAlive) {
                    PSecuritySDK.getWifiP2PClientService()
                        ?.getIP2PConnectControl()
                        ?.setKeepP2PConnect(true, { success ->
                            Log.d(TAG, "setKeepP2PConnect result=$success")
                        })
                }
            }, { errMsg: String, code: Int ->
                Log.e(TAG, "getKeepP2PConnectState failed: $code $errMsg")
            })
    }
}
```

## 7. Logs

Watch:

| Log | Meaning |
| --- | --- |
| Permission logs | Wi-Fi, location, and Bluetooth permissions |
| Bluetooth logs | Whether the P2P request was sent over Bluetooth |
| `Transport-sdk` | P2P connect, disconnect, and reconnect |
| Business logs | Whether transfer actually starts |

Provide:

- Phone model and Android version.
- Glasses system version.
- SDK version.
- Whether phone hotspot is enabled.
- Whether Wi-Fi was switched.
- `mobileLog` directory.
