gpt4 book ai didi

android - 以编程方式启用蓝牙网络共享android

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:55:23 24 4
gpt4 key购买 nike

我正在尝试在 Play 商店中制作类似“蓝牙自动网络共享”的应用程序。我在论坛上看到,Android 非常注重安全,在没有用户交互的情况下不会启用此设置。

我需要一些关于如何启用蓝牙网络共享的解释。

谢谢

最佳答案

我不知道这是否仍然是个问题,但我发现在反射调用中使用 connect 方法是可行的。处理 pmont 的代码从 Lorelorelore 中的链接使用的回答:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Class<?> classBluetoothPan = null;
Constructor<?> BTPanCtor = null;
Object BTSrvInstance = null;
Method mBTPanConnect;

try {
classBluetoothPan = Class.forName("android.bluetooth.BluetoothPan");
mBTPanConnect = classBluetoothPan.getDeclaredMethod("connect", BluetoothDevice.class);
BTPanCtor = classBluetoothPan.getDeclaredConstructor(Context.class, BluetoothProfile.ServiceListener.class);
BTPanCtor.setAccessible(true);
BTSrvInstance = BTPanCtor.newInstance(myContext, new BTPanServiceListener(myContext));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
try{
mBTPanConnect.invoke(BTSrvInstance, device);
}
catch (Exception e) {
e.printStackTrace();
}
}
}

当然,这假设蓝牙已启用,并且您只有一个配对设备。但是使用标准(非反射)调用启用蓝牙非常简单,您只需在 for 循环中检查要连接的配对设备即可。另外,不要忘记其他答案中的 BTPanServiceListener 类。

希望这对您有所帮助。

关于android - 以编程方式启用蓝牙网络共享android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22864670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com