以编程方式启用蓝牙网络共享 android [英] Enable bluetooth tethering android programmatically

查看:31
本文介绍了以编程方式启用蓝牙网络共享 android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Play 商店中制作一个类似于蓝牙自动网络共享"的应用程序.我在论坛上读到 Android 非常注重安全,如果没有用户交互,将不会启用此设置.

I am trying to make an application like "Bluetooth auto tethering" on play store. I read on the forum that Android is very security-aware and will not enable this setting without user interaction.

我需要一些关于如何启用蓝牙网络共享的说明.

I need some explanations about how enable bluetooth tethering.

谢谢

推荐答案

我不知道这是否仍然是一个问题,但我发现在反射调用中使用了connect方法作品.处理 pmontLorelelore 的回答:

I don't know if this is still an issue or not, but I found that using the connect method in the reflection call works. Working off of the code that pmont used from the link in Lorelorelore's answer:

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 类.

Of course, this assumes that the bluetooth is enabled, and you only have one paired device. But enabling bluetooth is pretty straightforward using standard (not reflection) calls, and you can just check for the paired device that you want to connect to in the for loop. Also, don't forget the BTPanServiceListener class from the other answer as well.

希望这会有所帮助.

这篇关于以编程方式启用蓝牙网络共享 android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆