Android 蓝牙HID协议 蓝牙手柄、键盘、鼠标连接协议 代码干货

原生蓝牙写法

前提:蓝牙外接设备连接需要当前设备协议栈支持HID_Host协议

其次添加HID_HOST协议工具类即可

package com.demo.bt.lib;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHidDevice;
import android.bluetooth.BluetoothHidHost;
import android.bluetooth.BluetoothProfile;
import android.os.Build;

import androidx.annotation.RequiresApi;

import java.util.ArrayList;
import java.util.List;

public class LocalHidProfile {
    private static final String TAG = "LocalHidWwch";
    private BluetoothHidHost mService = null;
    private static LocalHidProfile instance;

    public LocalHidProfile() {
        LogTool.d(TAG, "LocalHidProfile INIT ");
        BluetoothAdapter.getDefaultAdapter().getProfileProxy(Myapplication.getAppContext(), new HidListener(), BluetoothProfile.HID_HOST);
    }

    public static LocalHidProfile getInstance() {
        if (instance == null) {
            synchronized (LocalHidProfile.class) {
                if (instance == null) {
                    instance = new LocalHidProfile();
                }
            }
        }
        return instance;
    }
    public boolean getBluetoothHid() {
        return mService != null;
    }
    @RequiresApi(api = Build.VERSION_CODES.P)
    public boolean connect(BluetoothDevice device) {
        if (mService == null) {
            LogTool.d(TAG, "connect : mService is null");
            return false;
        }
        boolean ret = mService.connect(device);
        LogTool.d(TAG, "connect : ret = " + ret);
        return ret;
    }

    @RequiresApi(api = Build.VERSION_CODES.P)
    public boolean disconnect(BluetoothDevice device) {
        if (mService == null) {
            LogTool.d(TAG, "disconnect : mService is null");
            return false;
        }
        boolean ret = mService.disconnect(device);
        LogTool.d(TAG, "disconnect : ret = " + ret);
        return ret;
    }

    public int getConnectionState(BluetoothDevice device) {
        if (mService == null) {
            LogTool.d(TAG, "getConnectionState : mService is null");
            return BluetoothProfile.STATE_DISCONNECTED;
        }
        int state = mService.getConnectionState(device);
        LogTool.d(TAG, "getConnectionState : state = " + state);
        return state;
    }

    public List<BluetoothDevice> getConnectedDevices() {
        if (mService == null) {
           
            return new ArrayList<BluetoothDevice>(0);
        }
        return mService.getConnectedDevices();
    }



    public boolean isConnected() {
        if (mService == null) {
            
            return false;
        }
        return false;
    }

    public String getConnectedAddress() {
        if (mService == null) {
    
            return "";
        }
        return "";//mService.getConnectedAddress();TODO
    }



    private final class HidListener implements BluetoothProfile.ServiceListener {

        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            mService = (BluetoothHidHost) proxy;
            LogTool.d(TAG, "onServiceConnected : " + mService);
        }

        @Override
        public void onServiceDisconnected(int profile) {
            LogTool.d(TAG, "onServiceDisconnected");
            mService = null;

        }
    }
}

应用启动时调用,用于注册HID_HOST服务

LocalHidProfile.getInstance();

调用连接协议方法

LocalHidProfile.getInstance().connect(device);

调用断开协议方法

LocalHidProfile.getInstance().disconnect(device);

接收连接状态广播

private void registerReceivers() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothHidHost.ACTION_CONNECTION_STATE_CHANGED);
        mContext.registerReceiver(broadcastReceiver, filter);
    }

广播

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            LogTool.d(TAG, "broadcastReceiver--ACTION==" + intent.getAction());
            if (intent.getAction().equals(BluetoothHidHost.ACTION_CONNECTION_STATE_CHANGED)) {
              //获取蓝牙对象
	BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
               //最新协议状态 0:断开 1:连接中 2:连接成功 3:断开中
	int newState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
	//之前协议状态 0:断开 1:连接中 2:连接成功 3:断开中
 	int prevState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, -1);
            } 
        }
    };

如上为整个连接外接设备的HID_HOST协议代码连接+接收全流程

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值