TL;DR:你不能 - 如果你不想做一些非常丑陋和潜在危险的黑客攻击。
没有简单的方法可以做到这一点。如果您查看 discoverServices() 在 Nougat 上的作用,您会看到它调用了 GattService:L1454。
void discoverServices(int clientIf, String address) {
enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
Integer connId = mClientMap.connIdByAddress(clientIf, address);
if (DBG) Log.d(TAG, "discoverServices() - address=" + address + ", connId=" + connId);
if (connId != null)
gattClientSearchServiceNative(connId, true, 0, 0);
else
Log.e(TAG, "discoverServices() - No connection for " + address + "...");
}
这里的关键是gattClientSearchServiceNative:
gattClientSearchServiceNative(int conn_id,
boolean search_all,
long service_uuid_lsb,
long service_uuid_msb)
如果search_all 为true,它将扫描所有内容。如果没有,它将仅搜索具有提供的 UUID 及其特征和描述符的服务。
您实际上可以尝试使用反射来调用它,直接传递 false search_all 参数和有效的 UUID,但这是一种非常糟糕的做法,并且会在新版本发布时崩溃,并且此类的内部实现发生变化。