首页 > 代码库 > android 获取蓝牙已连接设备
android 获取蓝牙已连接设备
蓝牙如果手动配对并已连接,获取连接的设备:
1.检测连接状态:
Java代码
- int a2dp = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.A2DP);
- int headset = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
- int health = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEALTH);
2,根据是否有连接获取已连接的设备:
Java代码
- int flag = -1;
- if (a2dp == BluetoothProfile.STATE_CONNECTED) {
- flag = a2dp;
- } else if (headset == BluetoothProfile.STATE_CONNECTED) {
- flag = headset;
- } else if (health == BluetoothProfile.STATE_CONNECTED) {
- flag = health;
- }
- if (flag != -1) {
- bluetoothAdapter.getProfileProxy(MainActivity.this, new ServiceListener() {
- @Override
- public void onServiceDisconnected(int profile) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onServiceConnected(int profile, BluetoothProfile proxy) {
- // TODO Auto-generated method stub
- List<BluetoothDevice> mDevices = proxy.getConnectedDevices();
- if (mDevices != null && mDevices.size() > 0) {
- for (BluetoothDevice device : mDevices) {
- Log.i("W", "device name: " + device.getName());
- }
- } else {
- Log.i("W", "mDevices is null");
- }
- }
- }, flag);
- }
android 获取蓝牙已连接设备
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。