马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
学校寒假有个步调操持角逐,我也不停想要去写一个安卓模仿的蓝牙键盘,如许无论到那里,好比班班通和没有键盘的电脑装备,有手机就可以操纵它,也比USB方便一些。忙活了一个寒假,也走了不少歪路,终于整成了,下面分享一些履历。
(学校的软件操持角逐已经交了终稿了,我的堆栈开源在Gitee和GitHub,求求star:
Gitee:https://gitee.com/FengyunTHU/keyboard
GitHub:https://github.com/FengyunTHU/keyboardOFbluetooth)
本身在写代码的过程中也参考了许多CSDN博客,枚举如下:
蓝牙HID——将android装备酿成蓝牙键盘(BluetoothHidDevice)
仅通过蓝牙HID将安卓手机模仿成鼠标和键盘
利用旧手检做成蓝牙键盘
CSDN上大佬真的许多!
代码思绪
①第一步是蓝牙HID的初始化
在安卓API28后开放了BluetoothHidDevice类,重要就是用它来完成。起首是注册HID服务:
- mBtAdapter.getProfileProxy(context, new BluetoothProfile.ServiceListener() {
-
-
- @Override
- public void onServiceConnected(int profile, BluetoothProfile proxy) {
-
-
- Log.d(TAG, "onServiceConnected: " + profile);
- Toast.makeText(context, "Okk_connected_service", Toast.LENGTH_SHORT).show();
- if (profile == BluetoothProfile.HID_DEVICE) {
-
-
- Log.d(TAG, "Proxy received but it isn't hid_OUT");
- if (!(proxy instanceof BluetoothHidDevice)) {
-
-
- Log.e(TAG, "Proxy received but it isn't hid");
- return;
- }
- Log.d(TAG,"Connecting HID…");
- mHidDevice = (BluetoothHidDevice) proxy;
- Log.d(TAG, "proxyOK");
- BluetoothHidDeviceAppSdpSettings Sdpsettings = new BluetoothHidDeviceAppSdpSettings(
- HidConfig.KEYBOARD_NAME,
- HidConfig.DESCRIPTION,
- HidConfig.PROVIDER,
- BluetoothHidDevice.SUBCLASS1_KEYBOARD,
- HidConfig.KEYBOARD_COMBO
- );
- if (mHidDevice != null) {
-
-
- Toast.makeText(context, "OK for HID profile", Toast.LENGTH_SHORT).show();
- Log.d(TAG, "HID_OK");
- Log.d(TAG, "Get in register");
- //getPermission();
- // 创建一个BluetoothHidDeviceAppSdpSettings对象
- if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
-
-
- // TODO: Consider calling
- Log.d(TAG,"return before register");
- String[] list = new String[] {
-
-
- Manifest.permission.BLUETOOTH_SCAN,
- Manifest.permission.BLUETOOTH_CONNECT
- };
- requestPermissions(activity,list,1);
- return;
- }
- BluetoothHidDeviceAppQosSettings inQos = new BluetoothHidDeviceAppQosSettings(
- BluetoothHidDeviceAppQosSettings.SERVICE_GUARANTEED, 200, 2, 200,
- 10000 /* 10 ms */, 10000 /* 10 ms */);
- BluetoothHidDeviceAppQosSettings outQos = new BluetoothHidDeviceAppQosSettings(
- BluetoothHidDeviceAppQosSettings.SERVICE_GUARANTEED, 900, 9, 900,
- 10000 /* 10 ms */, 10000 /* 10 ms */);
- mHidDevice.registerApp(Sdpsettings, null, null, Executors.newCachedThreadPool(), mCallback);
- // registerApp();// 注册
- } else {
-
-
- Toast.makeText(context, "Disable for HID profile", Toast.LENGTH_SHORT).show();
- }
- // 启用设备发现
- // requestLauncher.launch(new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE));
- Log.d(TAG, "Discover");
- }
- }
- @SuppressLint("MissingPermission")
- @Override
- public void onServiceDisconnected(int profile) {
-
- // 断开连接
- if (profile == BluetoothProfile.HID_DEVICE) {
-
-
- Log.d(TAG, "Unexpected Disconnected: " + profile);
- mHidDevice = null;
- mHidDevice.unregisterApp();
- }
- }
- }, BluetoothProfile.HID_DEVICE);
- }
- public final BluetoothHidDevice.Callback mCallback = new BluetoothHidDevice.Callback() {
-
-
- private final int[] mMatchingStates = new int[]{
-
-
- BluetoothProfile.STATE_DISCONNECTED,
- BluetoothProfile.STATE_CONNECTING,
- BluetoothProfile.STATE_CONNECTED
- };
- @Override
- public void onAppStatusChanged(BluetoothDevice pluggedDevice, boolean registered) {
-
-
- Log.d(TAG, "ccccc_str");
- if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
-
-
- // TODO: Consider calling
- return;
- }
- Log.d(TAG, "onAppStatusChanged: " + (pluggedDevice != null ? pluggedDevice.getName() : "null") + "registered:" + registered
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金 |