private void checkConnectState() {
ArrayList deviceList = new ArrayList();
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Class bluetoothAdapterClass = BluetoothAdapter.class;//得到BluetoothAdapter的Class对象
try {
//得到连接状态的方法
Method method = bluetoothAdapterClass.getDeclaredMethod("getConnectionState", (Class[]) null);
//打开权限
method.setAccessible(true);
int state = (Integer) method.invoke(adapter, (Object[]) null);
if (state == BluetoothAdapter.STATE_CONNECTED) {
Log.i("BLUETOOTH", "BluetoothAdapter.STATE_CONNECTED");
Set devices = adapter.getBondedDevices();
Log.i("BLUETOOTH", "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) {
Log.i("BLUETOOTH", "connected:" + device.getName());
deviceList.add(device);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}