简体   繁体   中英

How to get my device bluetooth id.i try to get but they all time return 02:00:00:00:00:00

Hello I try to get my Bluetooth device id but that's not working. i try below code so can you check which mistake is i made in that.

public static String getBluetoothMacAddress(Context context) {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    String bluetoothMacAddress = "0";
    try {
        Field mServiceField = bluetoothAdapter.getClass().getDeclaredField("mService");
        mServiceField.setAccessible(true);

        Object btManagerService = mServiceField.get(bluetoothAdapter);

        if (btManagerService != null) {
            bluetoothMacAddress = (String) btManagerService.getClass()
                    .getMethod("getAddress").invoke(btManagerService);

            Utils.printLog("bluetoothMacAddress ", bluetoothMacAddress);
        }
     bluetoothMacAddress=   android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");
    } catch (Exception e) {
        Utils.printLog("bluetoothMacAddress1 ", e.getMessage());
    }

    if (bluetoothMacAddress == null || bluetoothMacAddress.equals("") || bluetoothMacAddress.equals("02:00:00:00:00:00")) {
        bluetoothMacAddress = "0";
    }
    Utils.printLog("bluetoothMacAddress ", bluetoothMacAddress);
    return bluetoothMacAddress;
}

You mac address is a sort of id actually but on network layer, so that network can identify your device. You should always get same mac address for particular device, though there are some exceptions in manufacturing when multiple device could have same mac (like Arduino controllers). However, this is EXCEPTIONAL , rather than general

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM