如何以编程方式在 Android 上配对蓝牙设备 [英] How to programmatically pair a bluetooth device on Android

查看:28
本文介绍了如何以编程方式在 Android 上配对蓝牙设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的应用程序,我正在尝试以编程方式配对蓝牙设备.我可以显示我想要配对的设备的配对对话框,并且我可以输入密码.当我按下配对"时,对话框被删除,什么也没有发生.

我只需要支持 Android 2.0 及更新版本的设备.

目前我正在使用以下代码开始配对进度:

<代码>公共无效对设备(蓝牙设备设备){String ACTION_PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST";意图意图=新意图(ACTION_PAIRING_REQUEST);String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";intent.putExtra(EXTRA_DEVICE,设备);String EXTRA_PAIRING_VARIANT = "android.bluetooth.device.extra.PAIRING_VARIANT";int PAIRING_VARIANT_PIN = 0;intent.putExtra(EXTRA_PAIRING_VARIANT,PAIRING_VARIANT_PIN);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(意图);}

在开始配对请求之前,我停止扫描新设备.

我的应用程序具有以下蓝牙权限:

  • android.permission.BLUETOOTH_ADMIN
  • android.permission.BLUETOOTH

解决方案

我设法通过作为服务检查特定类型设备的存在和修改版本的应用程序自动请求与键盘特色设备的配对过程设置应用.

我不得不说,我在一个运行 Android 4.0.3 的自定义设备上工作,没有外部控件(没有返回/主页/确认按钮):在启动时配对控制器完成而无需任何交互,直到 PIN 请求是强制性的.

首先,我创建了一个在启动时启动 Activity 的服务(使用 android.intent.action.BOOT_COMPLETEDandroid.permission.RECEIVE_BOOT_COMPLETED),它会定期检查是否存在onReceive 回调上的 1344 类设备(一个键盘,唯一的方法是根据请求输入数据):

public void onReceive(上下文上下文,意图意图)...BluetoothDevice dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);...if(dev.getBluetoothClass().getDeviceClass() == 1344){...}

过滤后,我选择第一个可用的键盘,然后将 BT 地址传递给设置"应用:

Intent btSettingsIntent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);btSettingsIntent.putExtra("btcontroller", dev.getAddress());startActivityForResult(btSettingsIntent, 1);

棘手的部分是寻找调用配对过程的最佳位置.仅使用

intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);

引导我进入配对对话框,该对话框关闭后让我的设备已配对,但无法使用.

深入研究 com.Android.settings.Bluetooth 的类我找到了通过

createDevicePreference(CachedBluetoothDevice cachedDevice)

在 DeviceListPreferenceFragment 中.

从那里我确实将我之前选择的 BT 地址与可用的地址进行了比较,一旦成功匹配,我就打电话

cachedDevice.startPairing();

我知道,这很棘手,需要访问 Android 源代码,但在自定义环境中它可以工作.

我希望这会有所帮助.

For my application I'm trying to programmatically pair a bluetooth device. I'm able to show the pairing dialog for the device I want to pair and I can enter a pincode. When I press "Pair" the dialog is removed and nothing happens.

I only need to support devices with Android 2.0 and newer.

Currently I am using the following code to start the pairing progress:


public void pairDevice(BluetoothDevice device) {
        String ACTION_PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST";
        Intent intent = new Intent(ACTION_PAIRING_REQUEST);
        String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
        intent.putExtra(EXTRA_DEVICE, device);
        String EXTRA_PAIRING_VARIANT = "android.bluetooth.device.extra.PAIRING_VARIANT";
        int PAIRING_VARIANT_PIN = 0;
        intent.putExtra(EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

Before starting a pairing request I stop scanning for new devices.

My application has the following bluetooth permissions:

  • android.permission.BLUETOOTH_ADMIN
  • android.permission.BLUETOOTH

解决方案

I managed to auto request a pairing procedure with keyboard featured devices through an app working as a service checking the presence of a specific kind of device and a modified version of the Settings app.

I have to say that I was working on a custom device running Android 4.0.3 without external controls (no back/Home/confirm buttons): pairing a controller on boot complete without any interaction until PIN request was mandatory.

First I created a service starting an activity on boot (with android.intent.action.BOOT_COMPLETED and android.permission.RECEIVE_BOOT_COMPLETED) that checks periodically the presence of a 1344 class device (a keyboard, the only way to input data on request) on the onReceive callback:

public void onReceive(Context context, Intent intent) 
...
    BluetoothDevice dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
...
if(dev.getBluetoothClass().getDeviceClass() == 1344){...}

Once filtered I choose the first keyboard available and then I pass the BT address to the Settings app:

Intent btSettingsIntent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
btSettingsIntent.putExtra("btcontroller", dev.getAddress());
startActivityForResult(btSettingsIntent, 1);

The tricky part was looking for the best position to call the pairing process. Using only the

intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);

led me to a paring dialog that once closed left me with the device paired, but unusable.

Digging into the classes of com.Android.settings.Bluetooth I found my way through the

createDevicePreference(CachedBluetoothDevice cachedDevice) 

in the DeviceListPreferenceFragment.

From there I did compare my previously selected BT address with those available coming up and once successfully matched I call

cachedDevice.startPairing();

I know, it's tricky and requires access to the Android source code, but in a custom environment it works.

I hope this could be helpful.

这篇关于如何以编程方式在 Android 上配对蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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