android - 检查蓝牙适配器是否连接到可穿戴设备
问题描述
我正在开发一个 android 应用程序,我需要知道蓝牙何时与可穿戴设备连接。我尝试使用带有蓝牙适配器的方法,但效果不佳。我该如何编程这个方法?
public static boolean btConnected() {
if( /*Bluetooth is connected*/ )
return true;
else
return false;
}
解决方案
在您的 Manifest.xml 中添加此权限
<uses-permission android:name="android.permission.BLUETOOTH" />
并创建一个方法来检查蓝牙是否已连接
private boolean isBluetootConnected() {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()
&& mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED;
}
推荐阅读
- matplotlib - 从 fill_between 的 PolyCollection 中提取 x/y 数据
- c# - 可空对象在存储过程中必须有一个值
- django - 我可以根据另一个在 django-model 中创建一个字段吗?
- ruby-on-rails - 将多个 .map 调用链接在一起,更有效的数据查询方式?
- iis - ColdFusion - Web 服务器 - 目录服务 - IIS
- laravel - 将 Stripe 支付方式传递给 Laravel View
- postgresql - 没有为 postgres-service 设置端点
- c++ - 为什么在使用原始头文件编译 TSS 文件时出现 typedef 错误?
- arrays - 如何根据指定的开始时间从数据框中提取多个 5 分钟平均值?
- javascript - 板球实时比分更新的客户端代码