如何检测蓝牙设备是否连接

5

在Android中,我的Activity如何知道蓝牙A2DP设备是否连接到我的设备?
是否有相应的广播接收器?
如何编写此广播接收器?

5个回答

15

从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<BluetoothDevice> 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指南中的“与配置文件一起使用”页面:http://developer.android.com/guide/topics/connectivity/bluetooth.html#Profiles
但是,如其他答案所述,您可以在Android < 3.0上注册BroadcastReceiver以侦听连接事件。

当 getProfileProxy() 被执行或者被注册为接收器时,onServiceConnected() 函数会被调用(这意味着对于每个连接的设备它都会被触发)? - Nicolò Ghielmetti
根据文档所述,只有当与蓝牙服务建立连接时,onServiceConnected()才会触发,因此当getProgileProxy()执行时...请注意,这是一个异步回调... - DSoldo

11

通过调用任何API都无法获取已连接设备的列表。 相反,您需要监听ACTION_ACL_CONNECTED、ACTION_ACL_DISCONNECTED意图通知有关已连接或断开连接设备的信息。 无法获取初始已连接设备列表。

我在我的应用程序中遇到了这个问题,我处理方式是(没有找到更好的方法...)在应用程序启动时打开/关闭蓝牙,以确保以一个空的连接设备列表开始,然后监听上述意图。


2
蓝牙从API 16开始得到改进(在API 19中引入了BluetoothManager,非常出色)...但对于较旧的API级别,这是正确的解决方案。 - andrea.rinaldi

1
muslidrikk的回答基本正确;但你也可以选择使用fetchUUIDsWithSDP(),然后查看返回的内容...不过这有点取巧--你必须知道设备可能具有哪些UUID(功能),如果它已经打开的话。而这可能很难保证。

对我没用。我连接和未连接的设备都有相同的结果。 - DarkTrick

0

-2
在您的活动中,定义广播接收器...
// 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 (BluetoothDevice.ACTION_FOUND.equals(action)) {
        // Get the BluetoothDevice object from the Intent
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        // Add the name and address to an array adapter to show in a ListView
        mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
    }
}

};

 // Register the BroadcastReceiver

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy

您提供的解决方案扫描可以被访问的设备。 - andrea.rinaldi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接