原生蓝牙写法
前提:蓝牙外接设备连接需要当前设备协议栈支持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协议代码连接+接收全流程