[wordpress搬家]Android Hack — BLE

[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();
		}
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值