问题描述
有关我的应用程序,我想以编程方式配对蓝牙设备。我能够以显示我要配对的设备配对对话框,我可以输入引脚code。当我preSS配对的对话框中被删除,没有任何反应。
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.
我只需要支持的设备与Android 2.0及更高版本。
I only need to support devices with Android 2.0 and newer.
目前我使用下面的code开始配对的进展:
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
推荐答案
通过一个应用程序我对汽车的要求管理与键盘功能的设备配对的过程作为一个服务检查特定种类设备的presence和设置应用程序的修改版本。
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.
我不得不说,我正在一个自定义的设备上,无需外部控制运行Android 4.0.3(没有返回/主页/确认键):配对开机完整的控制器没有任何交互,直至PIN码的要求是强制性的
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.
首先,我创建开始就启动一项活动服务(与 android.intent.action.BOOT_COMPLETED
和 android.permission.RECEIVE_BOOT_COMPLETED)
定期检查一个1344类设备的presence(键盘,根据要求输入数据的唯一途径)在的onReceive回调:
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){...}
过滤后我选择的第一个键盘可用,然后我通过BT地址来设置应用程序:
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.
挖掘com.Android.settings.Bluetooth的类通过我发现我的方式
Digging into the classes of com.Android.settings.Bluetooth I found my way through the
createDevicePreference(CachedBluetoothDevice cachedDevice)
在DEVICELIST preferenceFragment。
in the DeviceListPreferenceFragment.
从那里pviously选择BT地址与可来了,我也比较我的$ P $一次成功匹配我称之为
From there I did compare my previously selected BT address with those available coming up and once successfully matched I call
cachedDevice.startPairing();
我知道,这是棘手,需要访问Android源$ C $ C,但在自定义的环境中工作。
I know, it's tricky and requires access to the Android source code, but in a custom environment it works.
我希望这会有所帮助。
这篇关于如何以编程方式配对在Android蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!