Example usage for android.bluetooth BluetoothAdapter ACTION_REQUEST_ENABLE

List of usage examples for android.bluetooth BluetoothAdapter ACTION_REQUEST_ENABLE

Introduction

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

Prototype

String ACTION_REQUEST_ENABLE

To view the source code for android.bluetooth BluetoothAdapter ACTION_REQUEST_ENABLE.

Click Source Link

Document

Activity Action: Show a system activity that allows the user to turn on Bluetooth.

Usage

From source file:com.github.vgoliveira.panificadora.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);/*  w ww  .j  av  a 2  s .  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);

    checkLogin();

}

From source file:samples.piggate.com.piggateInfoDemo.Activity_Logged.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    _piggate = new Piggate(this, null); //Initialize the Piggate object
    setContentView(R.layout.activity_logged);
    getSupportActionBar().setTitle(PiggateUser.getEmail());

    startService(new Intent(getApplicationContext(), Service_Notify.class)); //Start the service

    //Initialize recycler view
    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    newBeaconsLayout = (LinearLayout) findViewById(R.id.newBeaconsLayout);

    //Initialize swipe layout
    mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    mSwipeLayout.setOnRefreshListener(this);

    errorDialog = new AlertDialog.Builder(this).create();
    errorDialog.setTitle("Logout error");
    errorDialog.setMessage("There is an error with the logout");
    errorDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
        @Override//from   w  w w .j av a  2s.c o  m
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    networkErrorDialog = new AlertDialog.Builder(this).create();
    networkErrorDialog.setTitle("Network error");
    networkErrorDialog.setMessage("There is an error with the network connection");
    networkErrorDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    fadeIn = AnimationUtils.loadAnimation(Activity_Logged.this, R.anim.fadein);
    fadeOut = AnimationUtils.loadAnimation(Activity_Logged.this, R.anim.fadeout);

    //Check if the bluetooth is switched on
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
}

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

private void menuDiscover() {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        Toast.makeText(BluetoothActivity.this, "Device does not support Bluetooth", Toast.LENGTH_SHORT).show();
    } else {//from   w  ww .ja  va2  s.com
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        } else {
            doDiscovery();
        }
    }
}

From source file:com.paulina.sadowska.robotwirelesscontroller.Bluetooth.BluetoothFragment.java

@Override
public void onStart() {
    super.onStart();
    // If BT is not on, request that it be enabled.
    // setupConnection() will then be called during onActivityResult
    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        // Otherwise, setup the chat session
    } else if (mBluetoothService == null) {
        setupConnection();/*from  www.  ja v a2  s  . c o m*/
    }
}

From source file:io.v.android.impl.google.discovery.plugins.ble.Driver.java

/**
 * Create a new BLE driver for Android./*  www . ja  va  2s .co m*/
 *
 * @param vContext Vanadium context.
 */
public Driver(VContext vContext) {
    mContext = V.getAndroidContext(vContext);
    if (mContext == null) {
        throw new IllegalStateException("AndroidContext not available");
    }
    mServices = new HashMap<>();

    BluetoothManager manager = ((BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE));
    mBluetoothAdapter = manager.getAdapter();
    if (mBluetoothAdapter == null) {
        Log.w(TAG, "BluetoothAdapter not available");
        return;
    }

    if (ContextCompat.checkSelfPermission(mContext,
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ContextCompat.checkSelfPermission(mContext,
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Log.w(TAG, "ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION not granted, "
                + "Bluetooth discovery will not be happening");
        return;
    }

    mContext.registerReceiver(new BluetoothAdapterStatusReceiver(),
            new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    if (!mBluetoothAdapter.isEnabled()) {
        // Prompt user to turn on Bluetooth.
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        enableBtIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(enableBtIntent);
        return;
    }
    resume();
}

From source file:com.wolkabout.hexiwear.service.DeviceDiscoveryService.java

boolean isEnabled() {
    if (bluetoothAdapter == null) {
        Log.e(TAG, "Bluetooth not supported");
        return false;
    } else if (!bluetoothAdapter.isEnabled()) {
        context.startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));
        return false;
    } else {/*w ww.ja v a 2  s  .com*/
        Log.v(TAG, "Bluetooth is enabled and functioning properly.");
        return true;
    }
}

From source file:com.example.mynsocial.BluetoothChat.java

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

    if (D)//ww w .j  a  v a  2 s .  com
        Log.e(TAG, "++ ON START ++");
    // If BT is not on, request that it be enabled.
    // setupChat() will then be called during onActivityResult
    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
    } else {// Otherwise, setup the chat session
        if (mChatService == null)
            setupChat();
    }
}

From source file:org.portlandrobotics.nrfUARTv2.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);//from   w  ww  .  ja v a 2  s  . c  o m
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBtAdapter == null) {
        Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        finish();
        return;
    }
    messageListView = (ListView) findViewById(R.id.listMessage);
    listAdapter = new ArrayAdapter<String>(this, R.layout.message_detail);
    messageListView.setAdapter(listAdapter);
    messageListView.setDivider(null);
    btnConnectDisconnect = (Button) findViewById(R.id.btn_select);
    btnSend = (Button) findViewById(R.id.sendButton);
    btnRotateNinety = (Button) findViewById(R.id.rotateNinetyButton);
    edtMessage = (EditText) findViewById(R.id.sendText);
    mSwv = (ServoWheelView) findViewById(R.id.view);
    motorSwitch = (Switch) findViewById(R.id.switch1);
    service_init();

    mSwv.setAngleUpdate(new MyAngle());

    // 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 Send button  
    btnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText editText = (EditText) findViewById(R.id.sendText);
            String message = editText.getText().toString();

            if (message.equals("dump")) {
                readRegister(BTSERVO_REG_TARGET);
                readRegister(BTSERVO_REG_MODE);
                readRegister(BTSERVO_REG_SPEED);
                readRegister(BTSERVO_REG_LOCATION);
                readRegister(BTSERVO_REG_PWM);
            } else {
                //send data to service
                //byte[] value = message.getBytes(uartCharset);
                //mService.writeRXCharacteristic(value);
                queueCommand(message);
            }
            //Update the log with time stamp
            String currentDateTimeString = DateFormat.getTimeInstance().format(new Date());
            listAdapter.add("[" + currentDateTimeString + "] TX: " + message);
            messageListView.smoothScrollToPosition(listAdapter.getCount() - 1);
            edtMessage.setText("");

        }
    });

    btnRotateNinety.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mServoState.target = (mServoState.target + 45) % 1024;
            writeRegister(0, mServoState.target);
        }
    });

    motorSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked)
                writeRegister(1, 1);
            else
                writeRegister(1, 0);
        }
    });

    // Set initial UI state

}

From source file:com.amobletool.bluetooth.le.downexample.ui.DeviceScanActivity.java

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

    // ??, ???,???????
    if (!mBluetoothAdapter.isEnabled()) {
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }//from w ww.  j a v  a 2 s .c  o  m
    }

    // Initializes list view adapter.
    mLeDeviceListAdapter = new LeDeviceListAdapter();
    setListAdapter(mLeDeviceListAdapter);
    scanLeDevice(true);
}

From source file:com.hwx.wheel.steeringwheel.bluetooth.ScaleActivity.java

public void startOpen() {
    super.onStart();
    if (D)//from   w w  w.  ja v a2s.c  o  m
        Log.e(TAG, "++ ON START ++");
    if (BluetoothService.getInstance().getState() == BluetoothService.STATE_CONNECTED) {
        return;
    }
    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
    } else {
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();//?
        if (pairedDevices.size() > 0) {
            boolean has = false;
            for (BluetoothDevice device : pairedDevices) {
                if (toCheck(device)) {
                    has = true;
                    break;
                }
            }
            if (!has)
                doDiscovery();
        } else {
            doDiscovery();
        }
    }
}