BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
try {
Method method = BluetoothAdapter.class.getDeclaredMethod("getConnectionState", null);
// 打开权限
method.setAccessible(true);
int state = (int) method.invoke(adapter, null);
} catch (Exception e) {
e.printStackTrace();
}
1.BluetoothAdapter.class.getDeclaredMethod("getConnectionState", null);
public Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException;
第一个参数传的是@hide的方法名称,后面是参数类型,可能是0个(传null),可能是一个(String.class),可能是多个
2.method.invoke(adapter, null);
public native Object invoke(Object obj, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException;
第一个参数之后传的是"getConnectionState"方法的具体入参,比如null "1" 0,invoke的返回值是getConnectionState的返回值