首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >以编程方式启动蓝牙系绳

以编程方式启动蓝牙系绳
EN

Stack Overflow用户
提问于 2012-03-30 11:17:45
回答 1查看 2.3K关注 0票数 2

Android蓝牙类在启用、发现、列出配对设备和连接到蓝牙设备方面非常容易使用。

我的计划是发起到另一个蓝牙设备的连接,该设备通过蓝牙提供系留功能。

经过一些调查,这看起来并不可行-看起来我必须自己实现这个配置文件,并拥有root访问权限来进行联网,并在一个应用程序中做所有的事情。

似乎也没有什么意图可以通过设置触发来启动蓝牙连接,我能做的最多就是打开它。

我是不是遗漏了什么--如果系统没有公开启动系统级蓝牙连接的方法,我是不是不走运?

EN

Stack Overflow用户

发布于 2014-12-26 23:48:06

接口中已经存在私有配置文件:BluetoothPan

蓝牙PAN (Personal Area Network,个人区域网)是识别蓝牙上的系留的正确名称。

这个私有类允许您通过public boolean connect(BluetoothDevice device)public boolean disconnect(BluetoothDevice device)方法连接到暴露PAN蓝牙配置文件的设备和从该设备断开连接。

以下是连接到特定设备的示例代码片段:

代码语言:javascript
运行
复制
String sClassName = "android.bluetooth.BluetoothPan";

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) {
        Log.e("MyApp", "BTPan proxy connected");
        BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("AA:BB:CC:DD:EE:FF"); //e.g. this line gets the hardware address for the bluetooth device with MAC AA:BB:CC:DD:EE:FF. You can use any BluetoothDevice
        try {
            Method connectMethod = proxy.getClass().getDeclaredMethod("connect", BluetoothDevice.class);
            if(!((Boolean) connectMethod.invoke(proxy, device))){
                Log.e("MyApp", "Unable to start connection");
            }
        } catch (Exception e) {
            Log.e("MyApp", "Unable to reflect android.bluetooth.BluetoothPan", e);
        }
    }

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

try {

     Class<?> classBluetoothPan = Class.forName(sClassName);

     Constructor<?> ctor = classBluetoothPan.getDeclaredConstructor(Context.class, BluetoothProfile.ServiceListener.class);
     ctor.setAccessible(true);
     Object instance = ctor.newInstance(getApplicationContext(), new BTPanServiceListener(getApplicationContext()));
} catch (Exception e) {
     Log.e("MyApp", "Unable to reflect android.bluetooth.BluetoothPan", e);
}
票数 3
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9936551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档