List of usage examples for android.bluetooth BluetoothAdapter isEnabled
@RequiresPermission(Manifest.permission.BLUETOOTH) public boolean isEnabled()
From source file:com.android.tests.lib.LibUnitTest.java
@Test public void mockFinalClass() { BluetoothAdapter adapter = mock(BluetoothAdapter.class); when(adapter.isEnabled()).thenReturn(true); assertTrue(adapter.isEnabled());/*from w w w. j a v a2 s.com*/ verify(adapter).isEnabled(); verifyNoMoreInteractions(adapter); }
From source file:com.google.android.apps.forscience.whistlepunk.devicemanager.ManageDevicesActivity.java
private void setFragment() { BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); Fragment fragment;// www.j a va 2 s .c o m if (adapter.isEnabled() && ScanDisabledFragment.hasScanPermission(this)) { mManageFragment = new ManageDevicesFragment(); fragment = mManageFragment; } else { fragment = new ScanDisabledFragment(); mManageFragment = null; } Bundle args = new Bundle(); args.putString(EXTRA_EXPERIMENT_ID, getIntent().getStringExtra(EXTRA_EXPERIMENT_ID)); fragment.setArguments(args); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.fragment, fragment); ft.commitAllowingStateLoss(); }
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 w w w . j av a 2s. com 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.amazonaws.devicefarm.android.referenceapp.Fragments.FixturesFragment.java
/** * Updates the Bluetooth status/*from ww w . ja va2 s.c o m*/ */ private void updateBluetoothStatusDisplay() { final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); bluetooth.setText(Boolean.toString(bluetoothAdapter != null && bluetoothAdapter.isEnabled())); }
From source file:com.orange.beaconme_sdk.control.BeaconMonitor.java
/** * Check if Bluetooth is enabled. If it is enabled then start scanner, ask to enable Bluetooth otherwise. *//*from ww w. j ava2 s. co m*/ private void startScan() { BluetoothAdapter btAdapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)) .getAdapter(); if (btAdapter == null || !btAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); enableBtIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(enableBtIntent); } else { context.sendBroadcast(new Intent(BLEDeviceScanner.START_SCAN_SERVICE_ACTION)); } }
From source file:org.physical_web.physicalweb.MainActivity.java
/** * Ensures Bluetooth is available on the beacon and it is enabled. If not, * displays a dialog requesting user permission to enable Bluetooth. *//*from www .j a va 2s . com*/ private void checkPermissions(BluetoothAdapter bluetoothAdapter) { // Acquire lock PermissionCheck.getInstance().setCheckingPermissions(true); if (!bluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); return; } ensureLocationPermissionIsEnabled(); }
From source file:com.hari.autotasx.GeofenceTransitionsIntentService.java
@Override protected void onHandleIntent(Intent intent) { GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); if (geofencingEvent.hasError()) { String errorMessage = GeofenceErrorMessages.getErrorString(this, geofencingEvent.getErrorCode()); // Log.e(TAG, errorMessage); return;/*from w w w .j a va 2 s . c o m*/ } int geofenceTransition = geofencingEvent.getGeofenceTransition(); int smsDb = 0; int wifiDb = 0; int silDb = 0; int RemDb = 0; int blueDb = 0; int carDb = 0; String remMsgDb = null; int RemDb_carPark = 0; String remMsgDb_carPark = null; //Creating database object to fetch values ManageDB autodb = new ManageDB(this); autodb.open(); if (!CarParkService.car_flag_static) { Cursor cursor = autodb.getEntry(ActionFragment.geoFence); cursor.moveToFirst(); //System.out.println("cursor count"+cursor.getCount()); //Log.i("logcat",cursor.getString(0)+", "+cursor.getString(1)+" , "+cursor.getString(2)); smsDb = cursor.getInt(5); wifiDb = cursor.getInt(6); silDb = cursor.getInt(7); RemDb = cursor.getInt(8); remMsgDb = cursor.getString(9); blueDb = cursor.getInt(10); carDb = cursor.getInt(11); cursor.close(); } else { Cursor cursor_carpark = autodb.getEntryCarPark(CarParkService.geofence_carpark); cursor_carpark.moveToFirst(); carDb = cursor_carpark.getInt(11); remMsgDb_carPark = cursor_carpark.getString(9); cursor_carpark.close(); } autodb.close(); //Checking Enter/Exit transitions if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) { List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences(); String geofenceTransitionDetails = getGeofenceTransitionDetails(this, geofenceTransition, triggeringGeofences); System.out.println("smsdb" + smsDb); if (RemDb == 1) { sendNotification(geofenceTransitionDetails, remMsgDb); } if (carDb == 1) { sendNotification(geofenceTransitionDetails, remMsgDb_carPark); } //Perform actions if set on Actions set if (silDb == 1) { Toast.makeText(this, "silent", Toast.LENGTH_SHORT).show(); AudioManager am = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE); am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE); } if (wifiDb == 1) { WifiManager wifiManager = (WifiManager) getBaseContext().getSystemService(Context.WIFI_SERVICE); wifiManager.setWifiEnabled(true); } if (blueDb == 1) { BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (!mBluetoothAdapter.isEnabled()) { mBluetoothAdapter.enable(); } } if (smsDb == 1) { Toast.makeText(this, "inside sendsms()", Toast.LENGTH_SHORT).show(); SmsManager sm = SmsManager.getDefault(); System.out.printf("sm", sm); sm.sendTextMessage(ActionFragment.smsNo, null, "Hi, I am in " + ActionFragment.geoFence.getNameLoc() + " now!!!!", null, null); } if (carDb == 1) { Intent mapIntent = new Intent(this, CarParkMap.class); mapIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } } else if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) { List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences(); String geofenceTransitionDetails = getGeofenceTransitionDetails(this, geofenceTransition, triggeringGeofences); if (silDb == 1) { Toast.makeText(this, "general", Toast.LENGTH_SHORT).show(); AudioManager am1 = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE); am1.setRingerMode(AudioManager.RINGER_MODE_NORMAL); } if (wifiDb == 1) { WifiManager wifiManager1 = (WifiManager) getBaseContext().getSystemService(Context.WIFI_SERVICE); wifiManager1.setWifiEnabled(false); } if (blueDb == 1) { BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter.isEnabled()) { mBluetoothAdapter.disable(); } } if (smsDb == 1) { Toast.makeText(this, "inside sendsms()", Toast.LENGTH_SHORT).show(); SmsManager sm = SmsManager.getDefault(); System.out.printf("smsNo :" + ActionFragment.smsNo); //fetch sm.sendTextMessage(ActionFragment.smsNo, null, "Hi, I am outside " + ActionFragment.geoFence.getNameLoc() + " now !!!", null, null); } if (RemDb == 1) { sendNotification(geofenceTransitionDetails, remMsgDb); } } }
From source file:com.google.sample.beaconservice.MainActivityFragment.java
private void createScanner() { BluetoothManager btManager = (BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter btAdapter = btManager.getAdapter(); if (btAdapter == null || !btAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, Constants.REQUEST_CODE_ENABLE_BLE); }// ww w.j a v a2 s .c om if (btAdapter == null || !btAdapter.isEnabled()) { Log.e(TAG, "Can't enable Bluetooth"); Toast.makeText(getActivity(), "Can't enable Bluetooth", Toast.LENGTH_SHORT).show(); return; } scanner = btAdapter.getBluetoothLeScanner(); }
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 2 s . 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.woxthebox.draglistview.sample.GameActivity.java
public void connectService() { try {//from w w w .j av a 2s . c om // 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); } }