BleBondUtil.java
1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.glsx.libble.util;
import android.bluetooth.BluetoothDevice;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @Package: com.glsx.module_carkey.util
* @ClassName: BleBondUtil
* @Company GLSX
* @Email yangks@didihu.com.cn
* @Description: 作用描述:
* @Author: yangks
* @CreateDate: 2021/4/22 20:15
* @Version:
*/
public class BleBondUtil {
//绑定
public static boolean createBond(BluetoothDevice bleDevice) {
boolean result = false;
try {
final Method createBond = bleDevice.getClass().getMethod("createBond");
final Boolean invoke = (Boolean) createBond.invoke(bleDevice);
result = invoke.booleanValue();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return result;
}
//与设备解除配对
public static boolean removeBond(BluetoothDevice bleDevice) {
boolean result = false;
try {
final Method removeBond = bleDevice.getClass().getMethod("removeBond");
final Boolean returnValue = (Boolean) removeBond.invoke(bleDevice);
result = returnValue.booleanValue();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}