赞
踩
仅作为记录
获取当前连接的蓝牙设备代码如下:
权限:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
方法:
//检查已连接的蓝牙设备
private void getConnectBt() {
int a2dp = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.A2DP);
int headset = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
int health = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEALTH);
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 BluetoothProfile.ServiceListener() {
@Override
public void onServiceDisconnected(int profile) {
Toast.makeText(MainActivity.this,profile+"",Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
List<BluetoothDevice> mDevices = proxy.getConnectedDevices();
if (mDevices != null && mDevices.size() > 0) {
for (BluetoothDevice device : mDevices) {
Toast.makeText(MainActivity.this, device.getName() + "," + device.getAddress(), Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "mDevices is null", Toast.LENGTH_SHORT).show();
}
}
}, flag);
}
}
说明:只要成功连接耳机之类的媒体设备,再调用这个方法就能获取到已连接的蓝牙设备名称、mac地址等信息。如果在没有连接任何设备调用该方法不会有任何反应(toast或者log)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。