更新时间:2022-12-30 23:12:53
通过一个应用程序我对汽车的要求管理与键盘功能的设备配对的过程作为一个服务检查特定种类设备的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.
我希望这会有所帮助。