android 怎么判断蓝牙版本,Android判断设备是否已连接经典蓝牙

/**

* 判断给定的设备mac地址是否已连接经典蓝牙

*

* @param macAddress 设备mac地址,例如"78:02:B7:01:01:16"

* @return

*/

public static boolean isConnectClassicBT(String macAddress) {

if (TextUtils.isEmpty(macAddress)) {

return false;

}

final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

Class bluetoothAdapterClass = BluetoothAdapter.class;//得到BluetoothAdapter的Class对象

try {

//是否存在连接的蓝牙设备

Method method = bluetoothAdapterClass.getDeclaredMethod("getConnectionState", (Class[]) null);

//打开权限

method.setAccessible(true);

int state = (int) method.invoke(bluetoothAdapter, (Object[]) null);

if (state == BluetoothAdapter.STATE_CONNECTED) {

Log.d("test", "BluetoothAdapter.STATE_CONNECTED");

Set devices = bluetoothAdapter.getBondedDevices();

Log.d("test", "devices:" + devices.size());

for (BluetoothDevice device : devices) {

Method isConnectedMethod = BluetoothDevice.class.getDeclaredMethod("isConnected", (Class[]) null);

method.setAccessible(true);

boolean isConnected = (boolean) isConnectedMethod.invoke(device, (Object[]) null);

if (isConnected) {

{

return macAddress.contains(device.getAddress());

}

} else {

Log.d("test", device.getName() + " connect false(" + device.getAddress() + ")");

}

}

}

return false;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值