应用场景
很多手机或者手机助手都会有拦截APP权限的功能,如果权限被拦截,就不能正常获取到需要的信息。不过系统对权限的控制其实并不完美,还是有方法可以绕过的。这里以蓝牙mac地址做为示例,分享一种绕过系统权限控制的方法。
常用的获取bluetooth mac代码如下:
String getBluetoothAddress(){
String blueMac="";
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter != null)
{
blueMac = mBluetoothAdapter.getAddress();
}
return blueMac;
}
不过这个方法需要权限
4.2以后的Android源码,新增了BluetoothManagerService.java,里面这样的实现
private static final String SECURE_SETTINGS_BLUETOOTH_ADDR_VALID="bluetooth_addr_valid";
private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address";
private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name";
/**
* Save the Bluetooth name and address in the persistent store.
* Only non-null values will be saved.
* @param name
* @param address
*/
private void storeNameAndAddress(String name, String address) {
if (name != null) {
Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME, name);
mName = name;
if (DBG) Log.d(TAG,"Stored Bluetooth name: " +
Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_NAME));
}
if (address != null) {
Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS, address);
mAddress=address;
if (DBG) Log.d(TAG,"Stored Bluetoothaddress: " +
Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS));
}
if ((name != null) && (address != null)) {
Settings.Secure.putInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 1);
}
}
会在/data/data/com.android.providers.settings/databases/settings.db数据库里增加bluetooth_address,保存蓝牙mac地址
数据库内容截图如下所示:
不过sdk里,并没有提供这样的接口去获取这个值,不过AndroidID也是保存在这个sqlite里的,使用类似的调用方法就可以获取这个值
String bluetoothAddress = Settings.Secure.getString(getContentResolver(), "bluetooth_address");
这个方法的优点是不需要权限,缺点是只有4.2之后的系统才可使用此方法
6.0以后的系统,已经不再能获取到蓝牙地址