最近公司需要開發一個項目需要連接藍牙設備,這個設備是一款手持移動端設備,類似外設藍牙鍵盤,移動設備發送數據到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端光標在哪里數據在哪里顯示。
最后配置權限;
有問題加微信一起溝通

