以编程方式连接安卓设备和蓝牙耳机
Posted
技术标签:
【中文标题】以编程方式连接安卓设备和蓝牙耳机【英文标题】:Connection between android device and bluetooth headsets programatically 【发布时间】:2018-08-29 08:53:16 【问题描述】:我如何在 android 中制作一个可以在 android 设备和蓝牙扬声器之间提供连接的应用程序。
【问题讨论】:
您可能需要自己进行研究,如果遇到任何问题,请发布问题 【参考方案1】:如何配对:
private void pairDevice(BluetoothDevice device)
try
Method method = device.getClass().getMethod("createBond", (Class[]) null);
method.invoke(device, (Object[]) null);
catch (Exception e)
e.printStackTrace();
如何取消配对:
private void unpairDevice(BluetoothDevice device)
try
Method method = device.getClass().getMethod("removeBond", (Class[]) null);
method.invoke(device, (Object[]) null);
catch (Exception e)
e.printStackTrace();
接收器捕捉配对过程:
private final BroadcastReceiver mPairReceiver = new BroadcastReceiver()
public void onReceive(Context context, Intent intent)
String action = intent.getAction();
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action))
final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
final int prevState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR);
if (state == BluetoothDevice.BOND_BONDED && prevState == BluetoothDevice.BOND_BONDING)
showToast("Paired");
else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDED)
showToast("Unpaired");
;
注册接收者:
IntentFilter intent = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mPairReceiver, intent);
【讨论】:
它是否与音频设备连接??以上是关于以编程方式连接安卓设备和蓝牙耳机的主要内容,如果未能解决你的问题,请参考以下文章