[2013.10.9]
现在虽然很多手机都有蓝牙4.0的硬件,但是软件上还没支持,需要有Android 4.3版本时系统才支持。
蓝牙4.0强大之处在于BLE技术(Bluetooth Low Energy),让蓝牙设备可以待机超长时间,并且广播/查找速度到了3ms,比起3.0的搜索快了很多。同时蓝牙4.0官方还规定了20多种profile,这个我现在还不是很了解,不过目前拿到手上的开发模块与现在市面上买得到的防丢器用的都是keyfob profile(不是很sure)。
这次带来的是Samsung的一个开发包,里面封装了隐藏API,仅在Samsung的S3和Note2上能够反射出来。具体两个封装类如下:
public class BluetoothAdapterHidden {
static Method startLeDiscoveryMethod = null;
private static final String TAG = "BluetoothAdapterHidden";
public static void startLeDiscovery(
BluetoothAdapter bluetoothAdapter) {
try {
if (startLeDiscoveryMethod == null) // find the method if necessary
{
@SuppressWarnings("rawtypes")
Class classToInvestigate = Class.forName("android.bluetooth.BluetoothAdapter");
Method[] aClassMethods = classToInvestigate.getDeclaredMethods();
for (Method m : aClassMethods) {
if (m.getName().equals("startLeDiscovery")) {
startLeDiscoveryMethod = m;
break;
}
}
}
if (startLeDiscoveryMethod == null) {
Log.i(TAG,
"不支持蓝牙4.0");
} else
startLeDiscoveryMethod.invoke(bluetoothAdapter,
(Object[]) null);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
Log.e(TAG,
"Failure");
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
Log.e(TAG,
"Failure");
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
Log.e(TAG,
"Failure");
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
Log.e(TAG,
"Failure");
e.printStackTrace();
}
}
}
public class BluetoothDeviceHidden {
public static final String TAG = "BluetoothDeviceHidden";
static Method getDeviceTypeMethod = null;
static Method createBondMethod = null;
static Method isLEDeviceConnectedMethod = null;
static Method removeBondMethod = null;
public static int getDeviceType(
BluetoothDevice device) {
int result = -1;
try {
// hunt for the getDeviceType() method
if (getDeviceTypeMethod == null) {
@SuppressWarnings("rawtypes")
Class classToInvestigate = Class.forName("android.bluetooth.BluetoothDevice");
Method[] aClassMethods = classToInvestigate.getDeclaredMethods();
for (Method m : aClassMethods) {
if (m.getName().equals("getDeviceType")) {
getDeviceTypeMethod = m;
break;
}
}
}
if (getDeviceTypeMethod == null) {
Log.i(TAG,
"Not support BLE");
} else {
result = (Integer) getDeviceTypeMethod.invoke(device,
(Object[]) null);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public static boolean isLEDeviceConnected(
BluetoothDevice device) {
boolean result = false;
try {
// hunt for the getDeviceType() method
if (isLEDeviceConnectedMethod == null) {
@SuppressWarnings("rawtypes")
Class classToInvestigate = Class.forName("android.bluetooth.BluetoothDevice");
Method[] aClassMethods = classToInvestigate.getDeclaredMethods();
for (Method m : aClassMethods) {
if (m.getName().equals("isLEDeviceConnected")) {
isLEDeviceConnectedMethod = m;
break;
}
}
}
result = (Boolean) isLEDeviceConnectedMethod.invoke(device,
(Object[]) null);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public static boolean createBond(
BluetoothDevice device) {
boolean result = false;
try {
// hunt for the getDeviceType() method
if (createBondMethod == null) {
@SuppressWarnings("rawtypes")
Class classToInvestigate = Class.forName("android.bluetooth.BluetoothDevice");
Method[] aClassMethods = classToInvestigate.getDeclaredMethods();
for (Method m : aClassMethods) {
if (m.getName().equals("createBond")) {
createBondMethod = m;
break;
}
}
}
result = (Boolean) createBondMethod.invoke(device,
(Object[]) null);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public static void removeBond(
BluetoothDevice device) {
try {
if (removeBondMethod == null) {
removeBondMethod = Class.forName("android.bluetooth.BluetoothDevice").getMethod("removeBond",
(Class[]) null);
}
removeBondMethod.invoke(device,
(Object[]) null);
} catch (Exception e) {
e.printStackTrace();
}
}
}