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:pw.dedominic.csc311_final_project.MainActivity.java

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

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    MAC_ADDR = mBluetoothAdapter.getAddress();

    // ready GPS unit
    mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    mLocationListener = new LocationListener() {
        @Override/*from w  w  w.  j  av a 2 s  . co m*/
        public void onLocationChanged(Location location) {
            PLAYER_LATITUDE = location.getLatitude();
            PLAYER_LONGITUDE = location.getLongitude();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
    if (mLocationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER)) {
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener);
        PROVIDER = LocationManager.NETWORK_PROVIDER;
    } else {
        PROVIDER = LocationManager.GPS_PROVIDER;
    }

    mHttpService.recreateGetUserNameTask();
    mHttpService.getUserName(MAC_ADDR);
}

From source file:net.cherryzhang.sekuhara.BluetoothButtonAndChat.BluetoothChat.BluetoothChat.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (D)/*from   w  w w .j  av a2 s.c om*/
        Log.e(TAG, "+++ ON CREATE +++");
    getActivity().setTitle("");
    // Set up the window layout

    // Get local Bluetooth adapter
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // If the adapter is null, then Bluetooth is not supported
    if (mBluetoothAdapter == null) {
        Toast.makeText(getActivity(), "Bluetooth is not available", Toast.LENGTH_LONG).show();
        //            finish();
        return;
    }
}

From source file:com.ebrain.mazeball.BluetoothChatFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);//  ww  w .j  a  v a  2 s . com
    // Get local Bluetooth adapter
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    FragmentActivity activity = getActivity();
    // If the adapter is null, then Bluetooth is not supported
    if (mBluetoothAdapter == null) {
        Toast.makeText(activity, "Bluetooth is not available", Toast.LENGTH_SHORT).show();
        activity.finish();
    }

    // Get an instance of the SensorManager
    mSensorManager = (SensorManager) activity.getSystemService(Context.SENSOR_SERVICE);
    mSensor = new MySensor();

    activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

From source file:com.variable.demo.api.fragment.MainOptionsFragment.java

/**
 * Shows a Dialog for any bonded device.
 *
 * Additionally, when item is select, onDeviceSelected is invoked.
 * @param c//from  www . j av a 2s.c  o  m
 */
public static void showPairedNodesDialog(Context c) {
    //Only Allow One Occurence of the Dialog to be Shown.
    if (mDevicesDialog != null) {
        mDevicesDialog.dismiss();
    }

    final Set<BluetoothDevice> mBondedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
    final String[] bluetoothNames = new String[mBondedDevices.size()];
    int i = 0;
    for (BluetoothDevice device : mBondedDevices) {
        bluetoothNames[i++] = device.getName();
    }

    //Build the Dialog
    mDevicesDialog = new AlertDialog.Builder(c).setTitle("Select a NODE")
            .setItems(bluetoothNames, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int position) {

                    int index = 0;
                    for (BluetoothDevice device : mBondedDevices) {
                        if (index++ == position) {
                            onDeviceSelected(device);
                        }
                    }
                }
            }).setNegativeButton("Cancel", null).create();

    //Show the Dialog
    mDevicesDialog.show();

}

From source file:com.spondbob.bluetooth.BluetoothActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Setup the window
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.device_list);

    // Set result CANCELED in case the user backs out
    setResult(Activity.RESULT_CANCELED);

    // Initialize array adapters. One for already paired devices and
    // one for newly discovered devices
    mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);

    // Find and set up the ListView for newly discovered devices
    newDevicesListView = (ListView) findViewById(R.id.new_devices);
    newDevicesListView.setAdapter(mNewDevicesArrayAdapter);

    // Register for broadcasts when a device is discovered
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mReceiver, filter);

    // Register for broadcasts when discovery has finished
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    this.registerReceiver(mReceiver, filter);

    // Get the local Bluetooth adapter
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();
}

From source file:com.ahmedabdelmeged.bluetoothmc.ui.BluetoothDevices.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetooth_devices);
    initializeScreen();/*from  w  ww.  j  a  v  a 2 s .c o m*/

    //check if the device has a bluetooth or not
    //and show Toast message if it does't have
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    //set the bluetooth adapter
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,
            false);
    deviceRecycler.setLayoutManager(linearLayoutManager);
    deviceRecycler.setHasFixedSize(true);
    bluetoothDevicesAdapter = new BluetoothDevicesAdapter(new ArrayList<String>(), this);
    deviceRecycler.setAdapter(bluetoothDevicesAdapter);

    if (mBluetoothAdapter == null) {
        Toast.makeText(this, R.string.does_not_have_bluetooth, Toast.LENGTH_LONG).show();
        finish();
    } else if (!mBluetoothAdapter.isEnabled()) {
        Intent enableIntentBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntentBluetooth, REQUEST_ENABLE_BT);
    } else if (mBluetoothAdapter.isEnabled()) {
        PairedDevicesList();
    }

    setBroadCastReceiver();

    //request location permission for bluetooth scanning for android API 23 and above
    ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
            REQUEST_ENABLE_FINE_LOCATION);

    //press the button to start search new Devices
    searchForNewDevices.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            searchForNewDevices.setEnabled(false);
            searchProgressbar.setVisibility(View.VISIBLE);
            bluetoothDevicesAdapter.clear();
            PairedDevicesList();
            NewDevicesList();
        }
    });

}

From source file:org.physical_web.physicalweb.BluetoothSite.java

/**
 * Connects to the Gatt service of the device to download a web page and displays a progress bar
 * for the title./*  ww w .  j  a v  a  2  s .  c om*/
 * @param deviceAddress The mac address of the bar
 * @param title The title of the web page being downloaded
 */
public void connect(String deviceAddress, String title) {
    running = true;
    String progressTitle = activity.getString(R.string.page_loading_title) + " " + title;
    progress = new ProgressDialog(activity);
    progress.setCancelable(true);
    progress.setOnCancelListener(new OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialogInterface) {
            Log.i(TAG, "Dialog box canceled");
            close();
        }
    });
    progress.setTitle(progressTitle);
    progress.setMessage(activity.getString(R.string.page_loading_message));
    progress.show();
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mBluetoothGatt = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceAddress)
                    .connectGatt(activity, false, BluetoothSite.this);
        }
    });
}

From source file:com.wordpress.bennthomsen.ble_uart_remote.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);//from w w w  .  ja  v a 2s .  c  o  m

    mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBtAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
        return;
    }
    btnConnectDisconnect = (Button) findViewById(R.id.btn_select);
    time_more = (Button) findViewById(R.id.button_time_more);
    time_less = (Button) findViewById(R.id.button_time_less);
    dough_qnt = (Button) findViewById(R.id.button_dough_qnt);
    options = (Button) findViewById(R.id.button_options);
    color = (Button) findViewById(R.id.button_color);
    init_stop = (Button) findViewById(R.id.button_init_stop);
    startRecipe = (Button) findViewById(R.id.startRecipesBookSelectionActivity);

    // dont allow the user click on these buttons if not connected and GATT service discovered.
    time_more.setEnabled(false);
    time_less.setEnabled(false);
    dough_qnt.setEnabled(false);
    options.setEnabled(false);
    color.setEnabled(false);
    init_stop.setEnabled(false);

    isConnected = false;

    service_init();

    // Handler Disconnect & Connect button
    btnConnectDisconnect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!mBtAdapter.isEnabled()) {
                Log.i(TAG, "onClick - BT not enabled yet");
                Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
            } else {
                if (btnConnectDisconnect.getText().equals("Connect")) {

                    //Connect button pressed, open DeviceListActivity class, with popup windows that scan for devices
                    Intent newIntent = new Intent(MainActivity.this, DeviceListActivity.class);
                    startActivityForResult(newIntent, REQUEST_SELECT_DEVICE);
                } else {
                    //Disconnect button pressed
                    if (mDevice != null) {
                        mService.disconnect();

                    }
                }
            }
        }
    });

    //Handler startRecipe
    startRecipe.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent booksIntent = new Intent(MainActivity.this, SelectionListActivity.class);
            booksIntent.putExtra("file", "recipes_books");
            booksIntent.putExtra("level", "books");
            booksIntent.putExtra("list_title", "Livros de receitas");

            booksIntent.putExtra("isConnected", isConnected);
            startActivityForResult(booksIntent, REQUEST_SELECT_RECIPE);
        }
    });

    time_more.setOnTouchListener(this);
    time_less.setOnTouchListener(this);
    options.setOnTouchListener(this);
    dough_qnt.setOnTouchListener(this);
    color.setOnTouchListener(this);
    init_stop.setOnTouchListener(this);
}

From source file:com.fpil.android.remotesensor.MainDisplay.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);//from   w  ww.  j a v  a  2  s .  c o m
    // Get local Bluetooth adapter
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    mNewButton = (FloatingActionButton) getActivity().findViewById(R.id.new_measure);

    // 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();
        mNewButton.hide();
        // activity.finish();
    }
}

From source file:com.marekscholtz.bluetify.utilities.BluetoothChatService.java

public BluetoothChatService() {
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    mState = STATE_NONE;
}