繁体   English   中英

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

[英]How to detect if bluetooth device is connected

在android中,我的Activity如何知道蓝牙A2DP设备是否连接到我的设备。
那有广播接收器吗?
如何编写这个广播接收器?

从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指南中的使用配置文件

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

您无法通过调用任何API获取已连接设备的列表。 您需要收听通知ACTION_ACL_CONNECTED,ACTION_ACL_DISCONNECTED,以通知有关正在连接或断开的设备。 无法获得连接设备的初始列表。

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

muslidrikk的答案大致正确; 但是你也可以使用fetchUUIDsWithSDP()看看你得到了什么......虽然它有点像黑客 - 你必须知道你可以从设备中获得什么样的UUID(功能),如果它被打开的话。 这可能很难保证。

对于BluetoothHeadset,您可以调用getConnectedDevices()来获取此特定配置文件的连接设备。

参考: http//developer.android.com/reference/android/bluetooth/BluetoothHeadset.html

您需要注册接收器的其他情况。

在您的活动中,定义广播接收器......

// 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM