本文整理汇总了Java中android.bluetooth.BluetoothAdapter.getAddress方法的典型用法代码示例。如果您正苦于以下问题:Java BluetoothAdapter.getAddress方法的具体用法?Java BluetoothAdapter.getAddress怎么用?Java BluetoothAdapter.getAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.bluetooth.BluetoothAdapter
的用法示例。
在下文中一共展示了BluetoothAdapter.getAddress方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAddress
import android.bluetooth.BluetoothAdapter; //导入方法依赖的package包/类
public static @Nullable String getAddress(final BluetoothAdapter adapter) {
if (adapter == null)
return null;
final String address = adapter.getAddress();
if (!MARSHMELLOW_FAKE_MAC.equals(address))
return address;
// Horrible reflection hack needed to get the Bluetooth MAC for Marshmellow and above.
try {
final Field mServiceField = BluetoothAdapter.class.getDeclaredField("mService");
mServiceField.setAccessible(true);
final Object mService = mServiceField.get(adapter);
if (mService == null)
return null;
return (String) mService.getClass().getMethod("getAddress").invoke(mService);
} catch (final Exception x) {
throw new RuntimeException(x);
}
}
示例2: getBluetoothAddress
import android.bluetooth.BluetoothAdapter; //导入方法依赖的package包/类
public static String getBluetoothAddress(Context ctx,
BluetoothAdapter adapter) {
// Return the adapter's address if it's valid and not fake
String address = adapter.getAddress();
if (isValidBluetoothAddress(address)) return address;
// Return the address from settings if it's valid and not fake
address = Settings.Secure.getString(ctx.getContentResolver(),
"bluetooth_address");
if (isValidBluetoothAddress(address)) return address;
// Let the caller know we can't find the address
return "";
}
示例3: getBluetoothMAC
import android.bluetooth.BluetoothAdapter; //导入方法依赖的package包/类
@SuppressWarnings("MissingPermission")
public static String getBluetoothMAC(Context context) {
String result = null;
try {
if (context.checkCallingOrSelfPermission(Manifest.permission.BLUETOOTH)
== PackageManager.PERMISSION_GRANTED) {
BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
result = bta.getAddress();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
示例4: getBluMac
import android.bluetooth.BluetoothAdapter; //导入方法依赖的package包/类
public String getBluMac() {
try {
BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
return (defaultAdapter == null || defaultAdapter.isEnabled()) ? defaultAdapter.getAddress() : "";
} catch (Exception e) {
return null;
}
}
示例5: getBluetoothAdapterAddress
import android.bluetooth.BluetoothAdapter; //导入方法依赖的package包/类
private static String getBluetoothAdapterAddress(BluetoothAdapter bluetoothAdapter) {
@SuppressLint("HardwareIds") // Pair-free peer-to-peer communication should qualify as an "advanced telephony use case".
String address = bluetoothAdapter.getAddress();
if (address.equals(FAKE_MAC_ADDRESS)) {
Log.w(TAG, "bluetoothAdapter.getAddress() did not return the physical address");
// HACK HACK HACK: getAddress is intentionally broken (but not deprecated?!) on Marshmallow and up:
// * https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-notifications
// * https://code.google.com/p/android/issues/detail?id=197718
// However, we need it to establish pair-free Bluetooth Classic connections:
// * All BLE advertisements include a MAC address, but Android broadcasts a temporary, randomly-generated address.
// * Currently, it is only possible to listen for connections using the device's physical address.
// So we use reflection to get it anyway: http://stackoverflow.com/a/35984808
// This hack won't be necessary if getAddress is ever fixed (unlikely) or (preferably) we can listen using an arbitrary address.
Object bluetoothManagerService = new Mirror().on(bluetoothAdapter).get().field("mService");
if (bluetoothManagerService == null) {
Log.w(TAG, "Couldn't retrieve bluetoothAdapter.mService using reflection");
return null;
}
Object internalAddress = new Mirror().on(bluetoothManagerService).invoke().method("getAddress").withoutArgs();
if (internalAddress == null || !(internalAddress instanceof String)) {
Log.w(TAG, "Couldn't call bluetoothAdapter.mService.getAddress() using reflection");
return null;
}
address = (String) internalAddress;
}
return address;
}
示例6: getBluetoothMAC
import android.bluetooth.BluetoothAdapter; //导入方法依赖的package包/类
public static String getBluetoothMAC() {
BluetoothAdapter mBlueth = BluetoothAdapter.getDefaultAdapter();
return mBlueth.getAddress();
}
示例7: getDiviceId
import android.bluetooth.BluetoothAdapter; //导入方法依赖的package包/类
public static String getDiviceId() {
TelephonyManager TelephonyMgr = (TelephonyManager) Utils.getContext().getSystemService(Context.TELEPHONY_SERVICE);
String szImei = TelephonyMgr.getDeviceId();
String m_szDevIDShort = "35" + //we make this look like a valid IMEI
Build.BOARD.length() % 10 +
Build.BRAND.length() % 10 +
Build.CPU_ABI.length() % 10 +
Build.DEVICE.length() % 10 +
Build.DISPLAY.length() % 10 +
Build.HOST.length() % 10 +
Build.ID.length() % 10 +
Build.MANUFACTURER.length() % 10 +
Build.MODEL.length() % 10 +
Build.PRODUCT.length() % 10 +
Build.TAGS.length() % 10 +
Build.TYPE.length() % 10 +
Build.USER.length() % 10; //13 digits
String m_szAndroidID = Settings.Secure.getString(Utils.getContext().getContentResolver(), Settings.Secure.ANDROID_ID);
WifiManager wm = (WifiManager) Utils.getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
BluetoothAdapter m_BluetoothAdapter = null; // Local Bluetooth adapter
m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String m_szBTMAC = m_BluetoothAdapter.getAddress();
StringBuilder stringBuilder = new StringBuilder();
if (!TextUtils.isEmpty(szImei)) {
stringBuilder.append(szImei);
}
if (!TextUtils.isEmpty(m_szDevIDShort)) {
stringBuilder.append(m_szDevIDShort);
}
if (!TextUtils.isEmpty(m_szAndroidID)) {
stringBuilder.append(m_szAndroidID);
}
if (!TextUtils.isEmpty(m_szWLANMAC)) {
stringBuilder.append(m_szWLANMAC);
}
if (!TextUtils.isEmpty(m_szBTMAC)) {
stringBuilder.append(m_szBTMAC);
}
String m_szLongID = stringBuilder.toString();
Log.i("utils", "手机唯一标识为:" + m_szLongID);
MessageDigest m = null;
try {
m = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
m.update(m_szLongID.getBytes(), 0, m_szLongID.length());
byte p_md5Data[] = m.digest();
String m_szUniqueID = new String();
for (int i = 0; i < p_md5Data.length; i++) {
int b = (0xFF & p_md5Data[i]);
if (b <= 0xF)
m_szUniqueID += "0";
m_szUniqueID += Integer.toHexString(b);
}
m_szUniqueID = m_szUniqueID.toUpperCase();
Log.i("utils", "手机唯一标识MD5为:" + m_szUniqueID);
return m_szUniqueID;
}