最近公司需要开发一个项目需要连接蓝牙设备,这个设备是一款手持移动端设备,类似外设蓝牙键盘,移动设备发送数据到PC电脑;
Android 蓝牙HID ——连接蓝牙鼠标、键盘等输入设备。
一、 HID简介
HID设备(Hunman Interface Device Profile),即人机交互设备,常见的有鼠标,键盘,游戏手柄,等等。一般有线方式都是通过USB连线连接到机器设备,作为用户输入设备。在蓝牙技术中,HID设备的接入就是无线的了。
二、搜索附件蓝牙
声明需要用到蓝牙对象
BluetoothHidDevice mBluetoothHidDevice;
BluetoothAdapter mBluetoothAdapter;
BluetoothDevice mConnectDevice;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { showToast("当前设备不支持蓝牙"); return; }
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mReceiver, intentFilter); // Don't forget to unregister during onDestroy
// 搜索蓝牙设备
mBluetoothAdapter.startDiscovery();
@Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mReceiver); }
// Create a BroadcastReceiver for ACTION_FOUND private final BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // When discovery finds a device if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) { showLoadingDialog("正在搜索附近的蓝牙设备"); } else if( BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { showToast("搜索结束"); hideLoadingDialog(); } else if (BluetoothDevice.ACTION_FOUND.equals(action)) { // Get the BluetoothDevice object from the Intent BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); deviceBeansList.add(new DeviceBean(bluetoothDevice.getName(),bluetoothDevice.getAddress(),ISPAR0)); notifyListChange(); } } };
注意:蓝牙搜索需要开启获取地理位置权限
三、获取BluetoothHidDevice
BluetoothAdapter.getDefaultAdapter().getProfileProxy(this, mProfileServiceListener,BluetoothProfile.HID_DEVICE);
public BluetoothProfile.ServiceListener mProfileServiceListener = new BluetoothProfile.ServiceListener() { @Override public void onServiceDisconnected(int profile) { } @SuppressLint("NewApi") @Override public void onServiceConnected(int profile, BluetoothProfile proxy) { if(profile==BluetoothProfile.HID_DEVICE) { mBluetoothProfile=proxy; mBluetoothHidDevice = (BluetoothHidDevice) proxy; BluetoothHidDeviceAppSdpSettings sdp = new BluetoothHidDeviceAppSdpSettings(HidBluetooth.NAME, HidBluetooth.DESCRIPTION, HidBluetooth.PROVIDER, BluetoothHidDevice.SUBCLASS1_COMBO, HidConstant.Descriptor); mBluetoothHidDevice.registerApp(sdp, null, null, Executors.newCachedThreadPool(), mCallback); } } };
@SuppressLint("NewApi") public 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(); // } } } };
四、匹配蓝牙
//匹配蓝牙 public boolean Pai(String deviceAddress){ if(BluetoothAdapter.checkBluetoothAddress(deviceAddress)){ try { mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if(mConnectDevice == null){ mConnectDevice = mBluetoothAdapter.getRemoteDevice(deviceAddress); } if(mConnectDevice.getBondState() == BluetoothDevice.BOND_NONE){ mConnectDevice.createBond(); return false; }else if(mConnectDevice.getBondState() == BluetoothDevice.BOND_BONDED){ return true; }else if(mConnectDevice.getBondState() == BluetoothDevice.BOND_BONDING){ return false; } }catch (Exception ex){ ex.printStackTrace(); } } return false; }
五、链接蓝牙
if(mConnectDevice == null){ mConnectDevice = mBluetoothAdapter.getRemoteDevice(HID_ADDR); } boolean ret = mBluetoothHidDevice.connect(mConnectDevice);
六、发送数据
HidDevice.sendReport(BtDevice, report.ReportId, report.ReportData);
蓝牙链接成功调用sendReport发送数据到PC端,PC端光标在哪里数据在哪里显示。
最后配置权限;
有问题加微信一起沟通