Android蓝牙——HID开发

代码地址如下:
http://www.demodashi.com/demo/13891.html

原文地址:

https://blog.csdn.net/VNanyesheshou/article/details/61914974

一 环境

开发环境:
 jdk1.6 Eclipse
 or jdk1.8 AS3.0.1
运行环境:
 华为V10(Android8.0)
实现功能:
 Android 蓝牙Hid——连接蓝牙鼠标、键盘等输入设备。

二 代码结构

AS

Eclipse

三、代码

1 Hid简介

HID设备(Hunman Interface Device Profile),即人机交互设备,常见的有鼠标,键盘,游戏手柄,等等。一般有线方式都是通过USB连线连接到机器设备,作为用户输入设备。在蓝牙技术中,HID设备的接入就是无线的了。

网上查资料说hid从android4.0开始支持(可能是usb hid),不过蓝牙hid应该从android4.2开始支持的,如下图所示:

android4.1.2中的Bluetooth应用中没有hid的相关代码,而android4.2源码中Bluetooth应用中才有hid的代码。

2 实现

连接hid设备步骤:

  1. 开启蓝牙
  2. 获得inputDevice profile
  3. 扫描
  4. 配对
  5. 连接
2.1 开启蓝牙,通过广播接收者监听蓝牙相关状态。
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
    
    Toast.makeText(this, "不支持蓝牙功能", 0).show();
    // 不支持蓝牙
    return;
}
// 如果没有打开蓝牙
if (!mBluetoothAdapter.isEnabled()) {
    
    mBluetoothAdapter.enable();
}
// 初始化广播接收者
mBroadcastReceiver = new BlueBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intentFilter.addAction("android.bluetooth.input.profile.action.CONNECTION_STATE_CHANGED");
this.registerReceiver(mBroadcastReceiver, intentFilter);
2.2 获得inputDevice profile

// 4.2以上才支持HID模式
if (Build.VERSION.SDK_INT >= 17) {
  mHidUtil = HidUtil.getInstance(this);
}

public static HidUtil getInstance(Context context){
    
    if(null == instance){
    
        instance = new HidUtil(context);
    }
    return instance;
}
 
private HidUtil(Context context) {
    
    this.context = context;
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    try {
    
        mBtAdapter.getProfileProxy(context,
                mListener, INPUT_DEVICE);
    } catch (Exception e) {
    
        e.printStackTrace();
    }
}
<
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值