Example usage for android.bluetooth BluetoothAdapter getDefaultAdapter

List of usage examples for android.bluetooth BluetoothAdapter getDefaultAdapter

Introduction

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

Prototype

public static synchronized BluetoothAdapter getDefaultAdapter() 

Source Link

Document

Get a handle to the default local Bluetooth adapter.

Usage

From source file:com.grandpaj.library2.bluetooth.BluetoothCommunication.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get local Bluetooth adapter
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // If the adapter is null, then Bluetooth is not supported
    if (mBluetoothAdapter == null) {
        FragmentActivity activity = getActivity();
        Toast.makeText(activity, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        activity.finish();//from www  .ja v a2s .com
    }
}

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

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mEnterpriseDeviceManager = (EnterpriseDeviceManager) getSystemService(
            EnterpriseDeviceManager.ENTERPRISE_POLICY_SERVICE);
    mRestrictionPolicy = mEnterpriseDeviceManager.getRestrictionPolicy();

    mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    findViewsInActivity();/*from   w  w w  . jav a2s .c o  m*/
    grantAdminPrivileges();
    setViewListeners();

    if (ActivateLicense.getPreferences(this, "ELM_ENROLLED")
            && ActivateLicense.getPreferences(this, "ELM_ENROLLED")) {
        updateSwitchesBasedOnStatus();
    }

    //Init Server Communication
    mServerCommunication = new Communication(this, getBaseContext());
    mServerCommunication.registerDevice();
    mHandler = new android.os.Handler();

    mPollServer = new Runnable() {
        @Override
        public void run() {
            try {
                mServerCommunication.getNextCommand();
                mHandler.postDelayed(this, 1000 * POLL_FREQUENCY);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    //Start Polling
    mHandler.postDelayed(mPollServer, 1000);

}

From source file:com.mattprecious.notisync.fragment.DeviceListFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.device_list, container, false);

    BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
    if (btAdapter == null) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }//from   www.j  ava 2s . c o  m

    localDeviceSet = Sets.newHashSet(Preferences.getDevices(getActivity()));

    listAdapter = new DeviceAdapter(getActivity(), Lists.newArrayList(btAdapter.getBondedDevices()));

    deviceList = (ListView) rootView.findViewById(R.id.deviceList);
    deviceList.setAdapter(listAdapter);

    return rootView;
}

From source file:com.example.multi_ndef.frag_bt.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.frag_bt, container, false);

    btSpinner = (Spinner) v.findViewById(R.id.spinnerbt);
    btSpinner.setOnItemSelectedListener(this);
    ma = (CNFCInterface) getActivity().getApplication();
    try {/*from   ww  w  . ja v a2  s .  c o m*/
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                String deviceBTName = device.getName();
                String address = device.getAddress();
                //String deviceBTMajorClass = getBTMajorDeviceClass(device.getBluetoothClass().getMajorDeviceClass());
                SpinnerArrayBT_Device_name.add(deviceBTName);
                SpinnerArrayBT_devices_mac.add(address);
            }
        }
    } catch (Exception e)
    //SpinnerArrayBT.a
    {

        Toast toast = Toast.makeText(getActivity().getApplicationContext(), "Bluetooth Code Error",
                Toast.LENGTH_SHORT);
        toast.show();

        int a1 = 10;
    }

    ArrayAdapter<String> btArrayAdapter = new ArrayAdapter<String>(this.getActivity(),
            android.R.layout.simple_spinner_item, SpinnerArrayBT_Device_name);
    btArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    btSpinner.setAdapter(btArrayAdapter);

    return v;
}

From source file:com.woxthebox.draglistview.sample.GameActivity.java

public void connectService() {
    try {//w w  w  . ja  va  2 s.co  m
        // status.setText("Connecting...");
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (bluetoothAdapter.isEnabled()) {
            bt.start();
            bt.connectDevice("HC-06");
            Log.d(TAG, "Btservice started - listening");
            //   status.setText("Connected");
        } else {
            Log.w(TAG, "Btservice started - bluetooth is not enabled");
            //   status.setText("Bluetooth Not enabled");
        }
    } catch (Exception e) {
        Log.e(TAG, "Unable to start bt ", e);
        //status.setText("Unable to connect " +e);
    }
}

From source file:edu.stanford.junction.provider.bluetooth.JunctionProvider.java

@Override
public URI generateSessionUri() {
    try {/*from   www  .  j a va  2s  .  co  m*/
        //String uuid = UUID.randomUUID().toString();
        String uuid = mConfig.getUuid().toString();
        String mac = BluetoothAdapter.getDefaultAdapter().getAddress();
        return new URI("junction://" + mac + "/" + uuid + "#bt");
    } catch (URISyntaxException e) {
        throw new AssertionError("Invalid URI");
    }
}

From source file:com.morgan.sunrisealarm.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, getString(R.string.bluetooth_not_available), Toast.LENGTH_LONG).show();
        this.finish();
    }/*from w ww  .j a  v  a2 s  .  c  o m*/
}

From source file:com.example.android.adp.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if (mBluetoothAdapter == null) {
        Toast.makeText(getApplicationContext(), "Bluetooth is not available", Toast.LENGTH_LONG).show();
    }//from  w  ww . ja  v a  2s .c  o  m

}

From source file:org.jmpm.ethbadge.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    Thread.setDefaultUncaughtExceptionHandler(handleAppCrash);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {/*from  ww  w  .  j a  v  a  2 s . c  o  m*/
        GlobalAppSettings.getInstance().initialize(getApplicationContext());

        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, BluetoothConstants.REQUEST_ENABLE_BT);
        }

    } catch (Exception e) {
        Utils.sendFeedbackEmail(getApplicationContext(), (Exception) e);
        Toast.makeText(getApplicationContext(), GlobalAppConstants.UNEXPECTED_FATAL_EXCEPTION_MESSAGE,
                Toast.LENGTH_LONG).show();
    }

    try {
        final AppCompatActivity myself = this;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // Only ask for these permissions on runtime when running Android 6.0 or higher
            switch (ContextCompat.checkSelfPermission(getBaseContext(),
                    Manifest.permission.ACCESS_COARSE_LOCATION)) {
            case PackageManager.PERMISSION_DENIED:
                ((TextView) new AlertDialog.Builder(this).setTitle("IMPORTANT").setMessage(Html.fromHtml(
                        "<p>This app requires Bluetooth permissions to work. In addition, some devices require access to device's location permissions to find nearby Bluetooth devices. Please click \"Allow\" on the following runtime permissions popup.</p>"
                                + "<p>For more info see <a href=\"http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id\">here</a>.</p>"))
                        .setNeutralButton("Okay", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (ContextCompat.checkSelfPermission(getBaseContext(),
                                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                                    int i = 5;
                                    ActivityCompat.requestPermissions(myself,
                                            new String[] { Manifest.permission.ACCESS_COARSE_LOCATION }, 1);
                                }
                            }
                        }).show().findViewById(android.R.id.message))
                                .setMovementMethod(LinkMovementMethod.getInstance()); // Make the link clickable. Needs to be called after show(), in order to generate hyperlinks
                break;
            case PackageManager.PERMISSION_GRANTED:
                break;
            }
        }
    } catch (Exception e) {
        Utils.sendFeedbackEmail(getApplicationContext(), (Exception) e);
        Toast.makeText(getApplicationContext(), "Error: Could not set permissions", Toast.LENGTH_LONG).show();
    }
}

From source file:com.mattprecious.notisync.preferences.DevicePreferenceFragment.java

@Override
public void onResume() {
    super.onResume();

    BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
    if (btAdapter == null) {
        return;//ww w.  j  ava2 s.  c o m
    }

    PreferenceScreen screen = getPreferenceScreen();
    screen.removeAll();

    Set<String> selectedDevices = Preferences.getDevices(getActivity());

    // Get a set of currently paired devices
    Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
    for (BluetoothDevice device : pairedDevices) {
        CheckBoxPreference preference = new CheckBoxPreference(getActivity());
        preference.setOnPreferenceChangeListener(preferenceListener);
        preference.setTitle(device.getName());
        preference.setSummary(device.getAddress());

        int iconResId = Helpers.getBtClassDrawable(device);
        if (iconResId != 0) {
            preference.setIcon(iconResId);
        }

        if (selectedDevices.contains(device.getAddress())) {
            preference.setDefaultValue(true);
            localDeviceSet.add(device.getAddress());
        }

        getPreferenceScreen().addPreference(preference);
    }
}