Android模仿蓝牙蓝牙键盘——适配Android和Windows

[复制链接]
发表于 2026-2-16 02:47:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
学校寒假有个步调操持角逐,我也不停想要去写一个安卓模仿的蓝牙键盘,如许无论到那里,好比班班通和没有键盘的电脑装备,有手机就可以操纵它,也比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服务:
  1. mBtAdapter.getProfileProxy(context, new BluetoothProfile.ServiceListener() {
  2.    
  3.    
  4.             @Override
  5.             public void onServiceConnected(int profile, BluetoothProfile proxy) {
  6.    
  7.    
  8.                 Log.d(TAG, "onServiceConnected: " + profile);
  9.                 Toast.makeText(context, "Okk_connected_service", Toast.LENGTH_SHORT).show();
  10.                 if (profile == BluetoothProfile.HID_DEVICE) {
  11.    
  12.    
  13.                     Log.d(TAG, "Proxy received but it isn't hid_OUT");
  14.                     if (!(proxy instanceof BluetoothHidDevice)) {
  15.    
  16.    
  17.                         Log.e(TAG, "Proxy received but it isn't hid");
  18.                         return;
  19.                     }
  20.                     Log.d(TAG,"Connecting HID…");
  21.                     mHidDevice = (BluetoothHidDevice) proxy;
  22.                     Log.d(TAG, "proxyOK");
  23.                     BluetoothHidDeviceAppSdpSettings Sdpsettings = new BluetoothHidDeviceAppSdpSettings(
  24.                             HidConfig.KEYBOARD_NAME,
  25.                             HidConfig.DESCRIPTION,
  26.                             HidConfig.PROVIDER,
  27.                             BluetoothHidDevice.SUBCLASS1_KEYBOARD,
  28.                             HidConfig.KEYBOARD_COMBO
  29.                     );
  30.                     if (mHidDevice != null) {
  31.    
  32.    
  33.                         Toast.makeText(context, "OK for HID profile", Toast.LENGTH_SHORT).show();
  34.                         Log.d(TAG, "HID_OK");
  35.                         Log.d(TAG, "Get in register");
  36.                         //getPermission();
  37.                         // 创建一个BluetoothHidDeviceAppSdpSettings对象
  38.                         if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
  39.    
  40.    
  41.                             // TODO: Consider calling
  42.                             Log.d(TAG,"return before register");
  43.                             String[] list = new String[] {
  44.    
  45.    
  46.                                     Manifest.permission.BLUETOOTH_SCAN,
  47.                                     Manifest.permission.BLUETOOTH_CONNECT
  48.                             };
  49.                             requestPermissions(activity,list,1);
  50.                             return;
  51.                         }
  52.                         BluetoothHidDeviceAppQosSettings inQos = new BluetoothHidDeviceAppQosSettings(
  53.                                 BluetoothHidDeviceAppQosSettings.SERVICE_GUARANTEED, 200, 2, 200,
  54.                                 10000 /* 10 ms */, 10000 /* 10 ms */);
  55.                         BluetoothHidDeviceAppQosSettings outQos = new BluetoothHidDeviceAppQosSettings(
  56.                                 BluetoothHidDeviceAppQosSettings.SERVICE_GUARANTEED, 900, 9, 900,
  57.                                 10000 /* 10 ms */, 10000 /* 10 ms */);
  58.                         mHidDevice.registerApp(Sdpsettings, null, null, Executors.newCachedThreadPool(), mCallback);
  59.                         // registerApp();// 注册
  60.                     } else {
  61.    
  62.    
  63.                         Toast.makeText(context, "Disable for HID profile", Toast.LENGTH_SHORT).show();
  64.                     }
  65.                     // 启用设备发现
  66.                     // requestLauncher.launch(new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE));
  67.                     Log.d(TAG, "Discover");
  68.                 }
  69.             }
  70.             @SuppressLint("MissingPermission")
  71.             @Override
  72.             public void onServiceDisconnected(int profile) {
  73.    
  74.    // 断开连接
  75.                 if (profile == BluetoothProfile.HID_DEVICE) {
  76.    
  77.    
  78.                     Log.d(TAG, "Unexpected Disconnected: " + profile);
  79.                     mHidDevice = null;
  80.                     mHidDevice.unregisterApp();
  81.                 }
  82.             }
  83.         }, BluetoothProfile.HID_DEVICE);
  84.     }
  85.     public final BluetoothHidDevice.Callback mCallback = new BluetoothHidDevice.Callback() {
  86.    
  87.    
  88.         private final int[] mMatchingStates = new int[]{
  89.    
  90.    
  91.                 BluetoothProfile.STATE_DISCONNECTED,
  92.                 BluetoothProfile.STATE_CONNECTING,
  93.                 BluetoothProfile.STATE_CONNECTED
  94.         };
  95.         @Override
  96.         public void onAppStatusChanged(BluetoothDevice pluggedDevice, boolean registered) {
  97.    
  98.    
  99.             Log.d(TAG, "ccccc_str");
  100.             if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
  101.    
  102.    
  103.                 // TODO: Consider calling
  104.                 return;
  105.             }
  106.             Log.d(TAG, "onAppStatusChanged: " + (pluggedDevice != null ? pluggedDevice.getName() : "null") + "registered:" + registered
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表