使用BluetoothHidDevice将安卓手机同时模拟成鼠标和键盘

一直以来就有一种想法,就是自己写一个APP将安卓手机模拟成鼠标/键盘,应急的时候可以用来代替鼠标/键盘。之前也在国内外的网站上找了各种方案,但是这些方案不是很好,直到谷歌发布的API28后终于有了很好的解决方案。为了实现这个想法也走了不少弯路,也许方法不对吧,但看到最终完美运行的APP,心中还是很有成就感的。经测试装了此APP的手机能与几乎所有安卓手机、WIN10笔记本电脑连接并操作,苹果设备需要IOS13及以上版本才能支持蓝牙鼠标/键盘。苹果系统下鼠标功能正常,键盘输入文字没问题,但是其它功能键(如:Win,Menu,PageUp/Down,上下左右键…)则没什么作用。

BluetoothHidDevice
android.bluetooth.BluetoothHidDevice是完成任务的核心类。通过它将我们的应用注册成具有HID特征的蓝牙设备,并传送HID设备的报告描述符。如果我们的报告描述符没有问题,那么我们的设备就会成功模拟想要的HID设备。

码砖思路

  1. 首先将我们的应用注册为HID设备;
BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, mProfileServiceListener,BluetoothProfile.HID_DEVICE);

public static BluetoothProfile.ServiceListener mProfileServiceListener = new BluetoothProfile.ServiceListener() {
    @Override
    public void onServiceDisconnected(int profile) { }
    @SuppressLint("NewApi") @Override
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        bluetoothProfile = proxy;
        if (profile == BluetoothProfile.HID_DEVICE) {
            HidDevice = (BluetoothHidDevice) proxy;
            HidConsts.HidDevice = HidDevice;
            BluetoothHidDeviceAppSdpSettings sdp = new BluetoothHidDeviceAppSdpSettings(HidConsts.NAME, HidConsts.DESCRIPTION, HidConsts.PROVIDER,BluetoothHidDevice.SUBCLASS1_COMBO, HidConsts.Descriptor);
            HidDevice.registerApp(sdp, null, null, Executors.newCachedThreadPool(), mCallback);
        }
    }
};
public static final BluetoothHidDevice.Callback mCallback = new BluetoothHidDevice.Callback() {
    @Override
    public void onAppStatusChanged(BluetoothDevice pluggedDevice, boolean registered) { }
    @Override
    public void onConnectionStateChanged(BluetoothDevice device, int state) {
        if(state == BluetoothProfile.STATE_DISCONNECTED){
            HidUitls.IsConnected(false);
            if(connectionStateChangeListener != null){
                connectionStateChangeListener.onDisConnected();
            }
        }else if(state == BluetoothProfile.STATE_CONNECTED){
            HidUitls.IsConnected(true);
            if(connectionStateChangeListener != null){
                connectionStateChangeListener.onConnected();
            }
        }else if(state == BluetoothProfile.STATE_CONNECTING){
            if(connectionStateChangeListener != null){
                connectionStateChangeListener.onConnecting();
            }
        }
    }
};
  1. 然后判断想要连接的蓝牙设备有没有配对过(双方都要配对好),如果没有配对则需要建立配对;
public static boolean Pair(String deviceAddress){
    if(BluetoothAdapter.checkBluetoothAddress(deviceAddress)){
        try {
            mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if(BtDevice == null){
                BtDevice = mBluetoothAdapter.getRemoteDevice(deviceAddress);
            }
            if(BtDevice.getBondState() == BluetoothDevice.BOND_NONE){
                BtDevice.createBond();
                return false;
            }else if(BtDevice.getBondState() == BluetoothDevice.BOND_BONDED){
                return true;
            }else if(BtDevice.getBondState() == BluetoothDevice.BOND_BONDING){
                return false;
            }
        }catch (Exception ex){ ex.printStackTrace(); }
    }
    return false;
}
  1. 配对完成后获取蓝牙设备的MAC地址,用MAC地址连接目标设备;
public static boolean Connect(String deviceAddress){
    if(TextUtils.isEmpty(deviceAddress)){return false;}
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if(BtDevice == null){
        BtDevice = mBluetoothAdapter.getRemoteDevice(deviceAddress);
    }
    boolean ret = HidDevice.connect(BtDevice);
    return ret;
}

IOS13相关设置
安装了HidDroid后的安卓机要控制苹果手机需要做如下设置,在苹果手机上找到:设置->辅助功能->触控->辅助触控->设备,选择已经配对并连接的安卓手机,设置成功后屏幕上出现一个白色的球,这个球就是鼠标指针。
使用BluetoothHidDevice将安卓手机同时模拟成鼠标和键盘_第1张图片

代码运行效果

让你的安卓手机变成键盘鼠标

下面一篇博客谈谈如何通过蓝牙将安卓手机模拟成游戏方向盘:通过蓝牙将安卓手机模拟成游戏方向盘-支持旋转轮胎

完整源码下载地址
链接:https://pan.baidu.com/s/1U2E5mRzc876I76ftvwgrww
提取码:bowc

你可能感兴趣的:(Android,蓝牙键盘,蓝牙鼠标)