TRANSPORT_USB transport. Key properties:ConnectivityManager API — there is no Meta-specific SDK to integrate.NetworkRequest for the USB (NCM) network.| Permission | Required? | Why |
|---|---|---|
android.permission.CHANGE_NETWORK_STATE | Yes | Needed by ConnectivityManager.requestNetwork(). |
android.permission.ACCESS_NETWORK_STATE | Yes | Needed to observe network, capability, and link-property callbacks. |
android.permission.INTERNET | Yes (if you open sockets) | Required to create TCP/UDP sockets over the link. |
android.permission.CHANGE_WIFI_MULTICAST_STATE | If using mDNS | Needed for NsdManager discovery of peers on the link. |
NetworkRequest that asks for the TRANSPORT_USB transport and explicitly drops the INTERNET and TRUSTED capabilities (the USB link is local-only, so the default capabilities would never be satisfied). Then call ConnectivityManager.requestNetwork() with a NetworkCallback.static final NetworkRequest NCM_REQUEST =
new NetworkRequest.Builder()
.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED)
.addTransportType(NetworkCapabilities.TRANSPORT_USB)
.build();
ConnectivityManager cm = context.getSystemService(ConnectivityManager.class);
void requestNetwork() {
cm.requestNetwork(NCM_REQUEST, mCallback, new Handler(Looper.getMainLooper()));
}
private final ConnectivityManager.NetworkCallback mCallback =
new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
// The USB-NCM network is up and ready to use.
}
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities caps) {
// Reports link bandwidth — used to detect USB 2 vs USB 3.
}
@Override
public void onLinkPropertiesChanged(Network network, LinkProperties lp) {
// lp.getInterfaceName() is the NCM interface; lp.getLinkAddresses()
// contains the IPv6 link-local address to bind/connect with.
}
@Override
public void onLost(Network network) {
// The network went away (cable unplugged, USB mode changed, etc.).
}
@Override
public void onUnavailable() {
// The request could not be satisfied — most commonly the user
// denied the consent dialog.
}
};
Network (or use the IPv6 link-local source address from LinkProperties) rather than relying on the default route.cm.unregisterNetworkCallback(callback) when you no longer need the link (see Release the network). Holding the request preempts other USB networking.NetworkCallback:onAvailable(), followed by onCapabilitiesChanged() and onLinkPropertiesChanged().onUnavailable().Important: WhenonUnavailable()fires, your callback has already been unregistered and the network request released, as if you had calledunregisterNetworkCallback(). To let the user retry, reset your UI state.
@Override
public void onUnavailable() {
// The system reports onUnavailable when the request can't be satisfied,
// most commonly when the user denies the USB-NCM consent dialog. At this
// point the network request is already released. Update the UI to reflect that.
if (mRequested) {
mRequested = false;
}
mStatusText.setText(R.string.network_status_unavailable);
}
onUnavailable().requestNetwork() while your activity is visible. This is the simplest, most reliable path.NetworkCapabilities link-bandwidth fields on the USB network. The system reads the live USB gadget speed and stamps it onto the NCM network’s capabilities as both upstream and downstream link bandwidth (in Kbps). Your app reads it in onCapabilitiesChanged():@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities caps) {
int downKbps = caps.getLinkDownstreamBandwidthKbps();
int upKbps = caps.getLinkUpstreamBandwidthKbps();
// downKbps / upKbps reflect the negotiated USB cable/port speed.
}
| USB negotiated speed | Reported bandwidth | Cable class |
|---|---|---|
High-Speed (USB 2.0) | ~480,000 Kbps (480 Mbps) | USB 2 |
SuperSpeed / 5G (USB 3.x Gen 1) | ~5,120,000 Kbps (5 Gbps) | USB 3 |
private static final int USB3_MIN_KBPS = 1_000_000; // 1 Gbps threshold
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities caps) {
int downKbps = caps.getLinkDownstreamBandwidthKbps();
if (downKbps > 0 && downKbps < USB3_MIN_KBPS) {
// ~480 Mbps => USB 2 cable/port. Prompt the user to switch to a
// USB 3 cable for high-bandwidth transfers.
showUseUsb3CableHint(downKbps);
}
}
void releaseNetwork() {
cm.unregisterNetworkCallback(mCallback);
}
onDestroy()) so a backgrounded or finishing app does not keep holding the USB link.