如何在 Android 中以编程方式检查蓝牙网络共享状态 [英] How to check Bluetooth tethering status programmatically in Android

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

问题描述

有什么办法可以在 Android 2.3+(2.3+ 之后的任何版本)中以编程方式找出蓝牙网络共享的启用或禁用?

Is there any way to find out bluetooth tethering is enabled or disabled programmatically in Android 2.3+(any version after 2.3+)?

我不是要求启用/禁用它,只是想知道它当前是否启用.

I am not asking to enable/disable it, but only to know if it is enabled currently or not.

推荐答案

事实证明,BluetoothPan(个人区域网络)是网络共享的关键词.我仔细研究了 API 文档和 Android 源代码,但评论中的简短示例具有误导性.这张海报提供了一个示例,但最初我遇到了问题:
Android BluetoothPAN 创建 TCP/IP 网络Android设备和Windows7 PC之间

It turns out that BluetoothPan (Personal Area Networking) is the keyword for tethering. I pored over the API documentation and Android source code, but the brief examples in the comments were misleading. This poster provided an example but I had trouble with it initially:
Android BluetoothPAN to create TCP/IP network between Android device and Windows7 PC

我尝试了各种其他方法,包括检查 BT 设备的 IP 地址.但是不存在蓝牙网络设备,因此没有要检查的IP.
检测 android 上的 USB 网络共享

I tried various other methods including checking the IP address of the BT device. However no Bluetooth network device exists, so there's no IP to check.
Detect USB tethering on android

回到BluetoothPan 代码...第一个线程中的示例不完整(没有ServiceListener 实现).我尝试了一个标准的,但 isTetheringOn 代理调用失败.关键是 onServiceConnected() 回调至少需要一行代码,否则编译器会对其进行优化.它也不应该像大多数其他示例那样断开代理.这是工作代码:

Back to the BluetoothPan code... The example in the first thread was incomplete (no ServiceListener implementation). I tried a standard one but the isTetheringOn proxy call failed. The crucial piece is that the onServiceConnected() callback needs at least one line of code or the compiler optimizes it away. It also shouldn't disconnect the proxy like most other examples have. Here's the working code:

BluetoothAdapter mBluetoothAdapter = null;
Class<?> classBluetoothPan = null;
Constructor<?> BTPanCtor = null;
Object BTSrvInstance = null;
Class<?> noparams[] = {};
Method mIsBTTetheringOn;

@Override
public void onCreate() {
    Context MyContext = getApplicationContext();
    mBluetoothAdapter = getBTAdapter();
    try {
        classBluetoothPan = Class.forName("android.bluetooth.BluetoothPan");
        mIsBTTetheringOn = classBluetoothPan.getDeclaredMethod("isTetheringOn", noparams);
        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();
    }
}

private BluetoothAdapter getBTAdapter() {
    if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)
        return BluetoothAdapter.getDefaultAdapter();
    else {
        BluetoothManager bm = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        return bm.getAdapter();
    }
}

// Check whether Bluetooth tethering is enabled.
private boolean IsBluetoothTetherEnabled() {
    try {
        if(mBluetoothAdapter != null) {
            return (Boolean) mIsBTTetheringOn.invoke(BTSrvInstance, (Object []) noparams);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

public class BTPanServiceListener implements BluetoothProfile.ServiceListener {
    private final Context context;

    public BTPanServiceListener(final Context context) {
        this.context = context;
    }

    @Override
    public void onServiceConnected(final int profile,
                                   final BluetoothProfile proxy) {
        //Some code must be here or the compiler will optimize away this callback.
        Log.i("MyApp", "BTPan proxy connected");
    }

    @Override
    public void onServiceDisconnected(final int profile) {
    }
}

这篇关于如何在 Android 中以编程方式检查蓝牙网络共享状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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