android 查找多个蓝牙设备吗,android – 如何检测蓝牙设备是否已连接

从API 11(

Android 3.0)开始,您可以使用

BluetoothAdapter来发现连接到特定蓝牙配置文件的设备.我使用下面的代码来发现一个名称的设备:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {

public void onServiceConnected(int profile, BluetoothProfile proxy) {

if (profile == BluetoothProfile.A2DP) {

boolean deviceConnected = false;

BluetoothA2dp btA2dp = (BluetoothA2dp) proxy;

List a2dpConnectedDevices = btA2dp.getConnectedDevices();

if (a2dpConnectedDevices.size() != 0) {

for (BluetoothDevice device : a2dpConnectedDevices) {

if (device.getName().contains("DEVICE_NAME")) {

deviceConnected = true;

}

}

}

if (!deviceConnected) {

Toast.makeText(getActivity(), "DEVICE NOT CONNECTED", Toast.LENGTH_SHORT).show();

}

mBluetoothAdapter.closeProfileProxy(BluetoothProfile.A2DP, btA2dp);

}

}

public void onServiceDisconnected(int profile) {

// TODO

}

};

mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.A2DP);

您可以为每个蓝牙配置文件执行此操作.在Android的指南中查看Working with profiles.

但是,正如其他答案中所写,您可以注册BroadcastReceiver来监听连接事件(例如当您使用android< 3.0时).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值