Example usage for android.bluetooth BluetoothAdapter getAddress

List of usage examples for android.bluetooth BluetoothAdapter getAddress

Introduction

In this page you can find the example usage for android.bluetooth BluetoothAdapter getAddress.

Prototype

@RequiresPermission(Manifest.permission.BLUETOOTH)
public String getAddress() 

Source Link

Document

Returns the hardware address of the local Bluetooth adapter.

Usage

From source file:Main.java

public static boolean isSupportBluetooth(BluetoothAdapter adapter) {
    if (adapter != null && !TextUtils.isEmpty(adapter.getAddress())) {
        return true;
    }/*  w  w w.  j av  a  2s  .  c om*/
    return false;
}

From source file:Main.java

@SuppressWarnings("MissingPermission")
public static String getBluetoothMAC(Context context) {
    String result = null;/*from  w ww .  ja  v  a  2  s  . c o  m*/
    try {
        if (context.checkCallingOrSelfPermission(
                Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_GRANTED) {
            BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
            result = bta.getAddress();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String getBluetoothMac() {
    BluetoothAdapter adapter = null;
    String bluetoothMac = null;//w  ww .j  av a2s  .c  o m
    try {
        adapter = BluetoothAdapter.getDefaultAdapter();
        bluetoothMac = adapter.getAddress();
    } catch (Exception ignored) {
    }
    return bluetoothMac;
}

From source file:Main.java

public static final String getOblyDevicesID(Context mContext) {

    String m_szImei = "";
    try {//from  www. j  av  a2  s . c o  m
        TelephonyManager TelephonyMgr = null;
        TelephonyMgr = (TelephonyManager) mContext.getSystemService(Activity.TELEPHONY_SERVICE);
        m_szImei = TelephonyMgr.getDeviceId();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    String m_szDevIDShort = null;
    try {
        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
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    String m_szAndroidID = "";
    try {
        m_szAndroidID = "";
        Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.ANDROID_ID);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    String m_szWLANMAC = "";
    try {
        WifiManager wm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    String m_szBTMAC = null;
    try {
        BluetoothAdapter m_BluetoothAdapter = null; // Local Bluetooth adapter
        m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        m_szBTMAC = m_BluetoothAdapter.getAddress();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    String m_szLongID = m_szImei + m_szDevIDShort + m_szAndroidID + m_szWLANMAC + m_szBTMAC;
    // compute md5
    MessageDigest m = null;
    try {
        m = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    m.update(m_szLongID.getBytes(), 0, m_szLongID.length());
    // get md5 bytes
    byte p_md5Data[] = m.digest();
    // create a hex string
    String m_szUniqueID = new String();
    for (int i = 0; i < p_md5Data.length; i++) {
        int b = (0xFF & p_md5Data[i]);
        // if it is a single digit, make sure it have 0 in front (proper padding)
        if (b <= 0xF)
            m_szUniqueID += "0";
        // add number to string
        m_szUniqueID += Integer.toHexString(b);
    } // hex string to uppercase
    m_szUniqueID = m_szUniqueID.toUpperCase();

    return m_szUniqueID;
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.ProfileObj.java

public static Obj getLocalProperties(Context c) {
    JSONObject obj = new JSONObject();
    try {// ww  w. ja  va  2  s  .  com
        // TODO: Framework.
        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
        if (btAdapter != null) {
            UUID btUuid = ContentCorral.getLocalBluetoothServiceUuid(c);
            String btMac = btAdapter.getAddress();
            obj.put(Contact.ATTR_BT_MAC, btMac);
            obj.put(Contact.ATTR_BT_CORRAL_UUID, btUuid.toString());
        }
        obj.put(Contact.ATTR_PROTOCOL_VERSION, App.POSI_VERSION);
    } catch (JSONException e) {
    }
    return new MemObj("userAttributes", obj);
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.ProfileObj.java

/**
 * Returns an Obj of type {@link #TYPE} with a profile representing
 * the local user. The profile is set with the given name and info fields.
 *//*from  w  w  w.ja  va 2 s  .  co m*/
public static Obj forLocalUser(Context c, String name, String about) {
    JSONObject obj = new JSONObject();
    try {
        obj.put("name", name);
        obj.put("about", about);

        // TODO: Framework.
        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
        if (btAdapter != null) {
            UUID btUuid = ContentCorral.getLocalBluetoothServiceUuid(c);
            String btMac = btAdapter.getAddress();
            obj.put(Contact.ATTR_BT_MAC, btMac);
            obj.put(Contact.ATTR_BT_CORRAL_UUID, btUuid.toString());
        }
        obj.put(Contact.ATTR_PROTOCOL_VERSION, App.POSI_VERSION);
    } catch (JSONException e) {
    }
    return new MemObj(TYPE, obj);
}

From source file:com.example.bluetooth_faster_connection.MainActivity.java

public static String getBluetoothMacAddress() {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // if device does not support Bluetooth
    if (mBluetoothAdapter == null) {
        Log.d(TAG, "device does not support bluetooth");
        return null;
    }/*from   w w w  . j a  v  a2s  .c  o m*/

    return mBluetoothAdapter.getAddress();
}

From source file:com.ecocitizen.common.HttpHelper.java

public HttpHelper(Context context) {
    String userAgentString = null;
    try {/*from w w  w  .jav a2  s  . c  o m*/
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        userAgentString = String.format("EcoCitizen-Android/%d/%s", packageInfo.versionCode,
                packageInfo.versionName);
    } catch (NameNotFoundException e) {
        userAgentString = "EcoCitizen-Android/unknown";
    }
    HTTP_USER_AGENT = userAgentString;

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);

    String username = settings.getString("username", "");
    String api_key = settings.getString("api_key", "");
    String map_server_url = settings.getString("map_server_url", "");
    map_server_url = map_server_url.replaceFirst("/*$", "");

    String bt_address = "";
    try {
        /*
        Log.d(TAG, "ANDROID_ID = " + Settings.System.getString(getContentResolver(), Settings.System.ANDROID_ID));
        Log.d(TAG, "MAC_ADDRESS = " + ((WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE)).getConnectionInfo().getMacAddress());
         */
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        bt_address = bluetoothAdapter.getAddress();
    } catch (Exception e) {
    }

    SENSORMAP_REGISTER_CLIENT_URL = String.format("%s/register/%s/", map_server_url,
            bt_address.replaceAll(":", "_"));
    SENSORMAP_LOGIN_URL = String.format("%s/login/%s/%s/", map_server_url, username, api_key);
    SENSORMAP_STATUS_URL = String.format("%s/status/", map_server_url);
    SENSORMAP_STARTSESSION_URL = String.format("%s/startsession/%s/%s/1/", map_server_url, username, api_key);
    SENSORMAP_STORE_URL = String.format("%s/store/%s/", map_server_url, username);
    SENSORMAP_ENDSESSION_URL = String.format("%s/endsession/%s/", map_server_url, username);
    SENSORMAP_UPLOADFILE_URL = String.format("%s/uploadfile/%s/%s/", map_server_url, username, api_key);

}

From source file:project.cs.lisa.application.html.NetInfWebViewClient.java

/**
 * Returns a NetInfPublish request object that can be used in order
 * to publish an IO./*from  w  w  w  .  j av  a  2 s .  co m*/
 * 
 * @param file         The file corresponding to the IO
 * @param url         The url where that file was downloaded from
 * @param hash         The hash identifying the file
 * @param contentType   The content type of the file
 * @return            Returns a publish request object
 * @throws IOException   Thrown, if Bluetooth is not available
 */
private NetInfPublish createPublishRequest(File file, URL url, String hash, String contentType)
        throws IOException {

    // Create metadata
    Metadata metadata = new Metadata();
    metadata.insert("filesize", String.valueOf(file.length()));
    metadata.insert("filepath", file.getAbsolutePath());
    metadata.insert("time", Long.toString(System.currentTimeMillis()));
    metadata.insert("url", url.toString());

    // Try to get the Bluetooth MAC
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter == null) {
        throw new IOException("Error: Bluetooth not supported");

    } else if (!adapter.isEnabled()) {
        throw new IOException("Error: Bluetooth not enabled");

    } else {
        // Create Locator set to be used in the publish
        HashSet<Locator> locators = new HashSet<Locator>();
        locators.add(new Locator(Locator.Type.BLUETOOTH, adapter.getAddress()));

        // Create the publish, adding locators, content type, and metadata
        NetInfPublish publishRequest = new NetInfPublish(HASH_ALG, hash, locators);
        publishRequest.setContentType(contentType);
        publishRequest.setMetadata(metadata);

        // Check for full put
        SharedPreferences sharedPref = PreferenceManager
                .getDefaultSharedPreferences(MainApplicationActivity.getActivity().getApplicationContext());
        boolean isFullPutAvailable = sharedPref.getBoolean("pref_key_fullput", false);
        if (isFullPutAvailable) {
            publishRequest.setFile(file);
        }

        return publishRequest;
    }
}

From source file:org.imdea.panel.MainActivity.java

public void enableBt() {

    BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();

    if (!mAdapter.isEnabled())
        mAdapter.enable(); //Turn On Bluetooth without Permission

    /*if (!mAdapter.isEnabled())
    startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));*/

    while (!mAdapter.isEnabled()) {
    }/*w  w w .  ja  v a2 s .c  om*/

    String restoredMAC = SP.getString("MAC", null);
    if (restoredMAC != null) {
        Global.DEVICE_ADDRESS = restoredMAC;
        if (restoredMAC.equals("00:00:00:00:00:00")) {
            Global.DEVICE_ADDRESS = mAdapter.getAddress();
            SharedPreferences.Editor editor = SP.edit();
            editor.putString("MAC", Global.DEVICE_ADDRESS);
            editor.apply();
        }
    } else {
        Global.DEVICE_ADDRESS = mAdapter.getAddress();
        SharedPreferences.Editor editor = SP.edit();
        editor.putString("MAC", Global.DEVICE_ADDRESS);
        editor.apply();
    }

    Log.w("MAC", Global.DEVICE_ADDRESS);

    if (mAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
        startActivity(discoverableIntent);
    }

}