android.bluetooth.BluetoothManager Java Examples
The following examples show how to use
android.bluetooth.BluetoothManager.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EspMainActivity.java From esp-idf-provisioning-android with Apache License 2.0 | 6 votes |
private void addDeviceClick() { if (BuildConfig.isQrCodeSupported) { gotoQrCodeActivity(); } else { if (deviceType.equals(AppConstants.DEVICE_TYPE_BLE) || deviceType.equals(AppConstants.DEVICE_TYPE_BOTH)) { final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter bleAdapter = bluetoothManager.getAdapter(); if (!bleAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } else { startProvisioningFlow(); } } else { startProvisioningFlow(); } } }
Example #2
Source File: DfuBaseService.java From microbit with Apache License 2.0 | 6 votes |
/** * Initializes bluetooth adapter * * @return <code>true</code> if initialization was successful */ private boolean initialize() { // For API level 18 and above, get a reference to BluetoothAdapter through // BluetoothManager. final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); if (bluetoothManager == null) { loge("Unable to initialize BluetoothManager."); return false; } mBluetoothAdapter = bluetoothManager.getAdapter(); if (mBluetoothAdapter == null) { loge("Unable to obtain a BluetoothAdapter."); return false; } return true; }
Example #3
Source File: DeviceScanActivity.java From IoT-Firstep with GNU General Public License v3.0 | 6 votes |
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scan); mHandler = new Handler(); mLView = (ListView) findViewById(R.id.scan_device_lv); // 判断硬件是否支持蓝牙4.0 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show(); finish(); } // 获取蓝牙适配器 final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); // 判断设备是否支持蓝牙 if (mBluetoothAdapter == null) { Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show(); finish(); } }
Example #4
Source File: AdvertiserService.java From android-BluetoothAdvertisements with Apache License 2.0 | 6 votes |
/** * Get references to system Bluetooth objects if we don't have them already. */ private void initialize() { if (mBluetoothLeAdvertiser == null) { BluetoothManager mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); if (mBluetoothManager != null) { BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter(); if (mBluetoothAdapter != null) { mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser(); } else { Toast.makeText(this, getString(R.string.bt_null), Toast.LENGTH_LONG).show(); } } else { Toast.makeText(this, getString(R.string.bt_null), Toast.LENGTH_LONG).show(); } } }
Example #5
Source File: ACSUtility.java From ESeal with Apache License 2.0 | 6 votes |
public ACSUtility(Context context, IACSUtilityCallback cb) { // TODO Auto-generated constructor stub this.context = context; userCallback = cb; _lengthOfPackage = 10; bScanning = false; Log.d(TAG, "acsUtility 1"); bluetoothManager = (BluetoothManager) context.getSystemService(context.BLUETOOTH_SERVICE); mBtAdapter = bluetoothManager.getAdapter(); if (mBtAdapter == null) { Log.d(TAG, "error, mBtAdapter == null"); return; } //after andrioid m, must request Permission on runtime // getPermissions(context); //context.registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter()); Intent intent = new Intent(); intent.setClass(context, ACSUtilityService.class); context.startService(intent); context.bindService(intent, conn, Context.BIND_AUTO_CREATE); }
Example #6
Source File: CycledLeScanner.java From android-beacon-library with Apache License 2.0 | 6 votes |
protected BluetoothAdapter getBluetoothAdapter() { try { if (mBluetoothAdapter == null) { // Initializes Bluetooth adapter. final BluetoothManager bluetoothManager = (BluetoothManager) mContext.getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); if (mBluetoothAdapter == null) { LogManager.w(TAG, "Failed to construct a BluetoothAdapter"); } } } catch (SecurityException e) { // Thrown by Samsung Knox devices if bluetooth access denied for an app LogManager.e(TAG, "Cannot consruct bluetooth adapter. Security Exception"); } return mBluetoothAdapter; }
Example #7
Source File: BlunoLibrary.java From BlunoAccessoryShieldDemo with GNU General Public License v3.0 | 6 votes |
boolean initiate() { // Use this check to determine whether BLE is supported on the device. // Then you can // selectively disable BLE-related features. if (!mainContext.getPackageManager().hasSystemFeature( PackageManager.FEATURE_BLUETOOTH_LE)) { return false; } // Initializes a Bluetooth adapter. For API level 18 and above, get a // reference to // BluetoothAdapter through BluetoothManager. final BluetoothManager bluetoothManager = (BluetoothManager) mainContext.getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); // Checks if Bluetooth is supported on the device. if (mBluetoothAdapter == null) { return false; } return true; }
Example #8
Source File: JamBaseBluetoothSequencer.java From xDrip with GNU General Public License v3.0 | 6 votes |
public static boolean isConnectedToDevice(final String mac) { if (JoH.emptyString(mac)) { return false; } final BluetoothManager bluetoothManager = (BluetoothManager) xdrip.getAppContext().getSystemService(Context.BLUETOOTH_SERVICE); if (bluetoothManager == null) { return false; } boolean foundConnectedDevice = false; for (BluetoothDevice bluetoothDevice : bluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) { if (bluetoothDevice.getAddress().equalsIgnoreCase(mac)) { foundConnectedDevice = true; break; } } return foundConnectedDevice; }
Example #9
Source File: ScanActivity.java From blefun-androidthings with Apache License 2.0 | 6 votes |
private void prepareForScan() { if (isBleSupported()) { // Ensures Bluetooth is enabled on the device BluetoothManager btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter btAdapter = btManager.getAdapter(); if (btAdapter.isEnabled()) { // Prompt for runtime permission if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { startLeScan(); } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION_LOCATION); } } else { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } } else { Toast.makeText(this, "BLE is not supported", Toast.LENGTH_LONG).show(); finish(); } }
Example #10
Source File: GattServer.java From blefun-androidthings with Apache License 2.0 | 6 votes |
public void onCreate(Context context, GattServerListener listener) throws RuntimeException { mContext = context; mListener = listener; mBluetoothManager = (BluetoothManager) context.getSystemService(BLUETOOTH_SERVICE); BluetoothAdapter bluetoothAdapter = mBluetoothManager.getAdapter(); if (!checkBluetoothSupport(bluetoothAdapter)) { throw new RuntimeException("GATT server requires Bluetooth support"); } // Register for system Bluetooth events IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); mContext.registerReceiver(mBluetoothReceiver, filter); if (!bluetoothAdapter.isEnabled()) { Log.d(TAG, "Bluetooth is currently disabled... enabling"); bluetoothAdapter.enable(); } else { Log.d(TAG, "Bluetooth enabled... starting services"); startAdvertising(); startServer(); } }
Example #11
Source File: BleManager.java From Bluefruit_LE_Connect_Android_V2 with MIT License | 6 votes |
public boolean start(Context context) { // Init Manager mManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE); // Init Adapter if (mManager != null) { mAdapter = mManager.getAdapter(); } else { mAdapter = null; } final boolean isEnabled = mAdapter != null && mAdapter.isEnabled(); if (!isEnabled) { Log.e(TAG, "Unable to obtain a BluetoothAdapter."); } return isEnabled; }
Example #12
Source File: ScannerService.java From com.ruuvi.station with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public void onCreate() { super.onCreate(); Foreground.init(getApplication()); Foreground.get().addListener(listener); foreground = true; scanSettings = new ScanSettings.Builder() .setReportDelay(0) .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) .build(); final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); bluetoothAdapter = bluetoothManager.getAdapter(); scanner = bluetoothAdapter.getBluetoothLeScanner(); if (getForegroundMode()) startFG(); handler = new Handler(); handler.post(reStarter); bgScanHandler = new Handler(); }
Example #13
Source File: BlunoLibrary.java From BlunoBasicDemo with GNU General Public License v3.0 | 6 votes |
boolean initiate() { // Use this check to determine whether BLE is supported on the device. // Then you can // selectively disable BLE-related features. if (!mainContext.getPackageManager().hasSystemFeature( PackageManager.FEATURE_BLUETOOTH_LE)) { return false; } // Initializes a Bluetooth adapter. For API level 18 and above, get a // reference to // BluetoothAdapter through BluetoothManager. final BluetoothManager bluetoothManager = (BluetoothManager) mainContext.getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); // Checks if Bluetooth is supported on the device. if (mBluetoothAdapter == null) { return false; } return true; }
Example #14
Source File: AddDeviceActivity.java From esp-idf-provisioning-android with Apache License 2.0 | 6 votes |
@Override public void onClick(View v) { String deviceType = sharedPreferences.getString(AppConstants.KEY_DEVICE_TYPES, AppConstants.DEVICE_TYPE_DEFAULT); if (deviceType.equals(AppConstants.DEVICE_TYPE_BLE) || deviceType.equals(AppConstants.DEVICE_TYPE_BOTH)) { final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter bleAdapter = bluetoothManager.getAdapter(); if (!bleAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } else { startProvisioningFlow(); } } else { startProvisioningFlow(); } }
Example #15
Source File: SystemStatusFragment.java From xDrip-plus with GNU General Public License v3.0 | 6 votes |
private void set_current_values() { notes.setText(""); activeBluetoothDevice = ActiveBluetoothDevice.first(); if (Build.VERSION.SDK_INT >= 18) { mBluetoothManager = (BluetoothManager) safeGetContext().getSystemService(Context.BLUETOOTH_SERVICE); } setVersionName(); //setCollectionMethod(); setCurrentDevice(); if (Home.get_follower()) { setConnectionStatusFollower(); } else if (prefs.getString("dex_collection_method", "bogus").equals("WifiWixel")) { setConnectionStatusWifiWixel(); } else { setConnectionStatus(); } setSensorStatus(); setTransmitterStatus(); setNotes(); futureDataCheck(); /* if (notes.getText().length()==0) { notes.setText("Swipe for more status pages!"); }*/ }
Example #16
Source File: JamBaseBluetoothSequencer.java From xDrip-plus with GNU General Public License v3.0 | 6 votes |
public static boolean isConnectedToDevice(final String mac) { if (JoH.emptyString(mac)) { return false; } final BluetoothManager bluetoothManager = (BluetoothManager) xdrip.getAppContext().getSystemService(Context.BLUETOOTH_SERVICE); if (bluetoothManager == null) { return false; } boolean foundConnectedDevice = false; for (BluetoothDevice bluetoothDevice : bluetoothManager.getConnectedDevices(BluetoothProfile.GATT)) { if (bluetoothDevice.getAddress().equalsIgnoreCase(mac)) { foundConnectedDevice = true; break; } } return foundConnectedDevice; }
Example #17
Source File: BleService.java From beacons-android with Apache License 2.0 | 6 votes |
private void initializeService() { if(D) Log.d(TAG, "initializeService"); Beacons.initialize(this); mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); IntentFilter localIntentFilter = new IntentFilter(ACTION_ITEM_STATE); LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, localIntentFilter); // Bluetooth events are not received when using LocalBroadcastManager IntentFilter systemIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver(mBroadcastReceiver, systemIntentFilter); BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE); if (null != bluetoothManager) { mAdvertisersManager = new AdvertisersManager(bluetoothManager, this); restoreSavedState(); } }
Example #18
Source File: BlePermissionHelper.java From BlueSTSDK_Android with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * check that the bluetooth is enabled * @return true if the bluetooth is enable false if we ask to the user to enable it */ private boolean enableBluetoothAdapter(){ final BluetoothManager bluetoothManager = ContextCompat.getSystemService(mCtx,BluetoothManager.class); if(bluetoothManager==null) throw new IllegalStateException("Bluetooth adapter is needed by this app!"); //the adapter is !=null since we request in the manifest to have the bt capability final BluetoothAdapter btAdapter = bluetoothManager.getAdapter(); // Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled, // fire an intent to display a dialog asking the user to grant permission to enable it. if (!btAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); return false; }else return true; }
Example #19
Source File: EddystoneScannerService.java From nearby-beacons with MIT License | 6 votes |
@Override public void onCreate() { super.onCreate(); sRunning = true; mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE); mBluetoothLeScanner = manager.getAdapter().getBluetoothLeScanner(); mDetectedBeacons = new ArrayMap<>(); startScanning(); }
Example #20
Source File: BLEService.java From android with MIT License | 6 votes |
@Override public int onStartCommand(Intent intent, int flags, int startId) { if (null != intent) { mMAC = intent.getStringExtra(KEY_MAC); } if (TextUtils.isEmpty(mMAC)) { mMAC = MAC_BT5; } mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = mBluetoothManager.getAdapter(); if (null != mBluetoothAdapter) { BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(mMAC); if (null != device) { mBluetoothGatt = device.connectGatt(this, false, mCallBack); mBluetoothGatt.connect(); return START_STICKY; } } stopSelf(); return START_NOT_STICKY; }
Example #21
Source File: PebbleWatchSync.java From xDrip with GNU General Public License v3.0 | 6 votes |
private void check_and_enable_bluetooth() { if (Build.VERSION.SDK_INT > 17) { try { final BluetoothManager bluetooth_manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); if (!bluetooth_manager.getAdapter().isEnabled()) { if (Pref.getBoolean("automatically_turn_bluetooth_on", true)) { JoH.setBluetoothEnabled(getApplicationContext(), true); //Toast.makeText(this, "Trying to turn Bluetooth on", Toast.LENGTH_LONG).show(); //} else { //Toast.makeText(this, "Please turn Bluetooth on!", Toast.LENGTH_LONG).show(); } } } catch (Exception e) { UserError.Log.e(TAG, "Error checking/enabling bluetooth: " + e); } } }
Example #22
Source File: DeviceScanActivity.java From IoT-Firstep with GNU General Public License v3.0 | 6 votes |
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scan); mHandler = new Handler(); mLView = (ListView) findViewById(R.id.scan_device_lv); // 判断硬件是否支持蓝牙4.0 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show(); finish(); } // 获取蓝牙适配器 final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); // 判断设备是否支持蓝牙 if (mBluetoothAdapter == null) { Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show(); finish(); } }
Example #23
Source File: CubeConnectionManager.java From puck-central-android with Apache License 2.0 | 5 votes |
public synchronized void checkAndConnectToPuck(final Puck puck) { if (mConnectedCubes.contains(puck) || !puck.getServiceUUIDs().contains(GattServices.CUBE_SERVICE_UUID)) { return; } L.i("Found Cube Puck. Initiating gatt subscribe to " + puck); mConnectedCubes.add(puck); new SimpleAsyncTask<Void>(mCtx, null) { @Override protected Void onExecute() throws Exception { BluetoothAdapter adapter = ((BluetoothManager) mCtx.getSystemService (Context.BLUETOOTH_SERVICE)).getAdapter(); BluetoothDevice device = adapter.getRemoteDevice(puck.getAddress()); mGattManager.queue(new GattSetNotificationOperation( device, GattServices.CUBE_SERVICE_UUID, GattServices.CUBE_CHARACTERISTIC_DIRECTION_UUID, GattServices.CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID)); // Android BLE stack might have issues connecting to // multiple Gatt services right after another. // See: http://stackoverflow.com/questions/21237093/android-4-3-how-to-connect-to-multiple-bluetooth-low-energy-devices Thread.sleep(1000); return null; } }.execute(); }
Example #24
Source File: Amazfitservice.java From xDrip-plus with GNU General Public License v3.0 | 5 votes |
public String getCurrentDevice() { activeBluetoothDevice = ActiveBluetoothDevice.first(); mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); String currentdevice; if (activeBluetoothDevice != null) { currentdevice = activeBluetoothDevice.name; } else { currentdevice = "None Set"; } String collection_method = prefs.getString("dex_collection_method", "BluetoothWixel"); if (collection_method.compareTo("DexcomG5") == 0) { Transmitter defaultTransmitter = new Transmitter(prefs.getString("dex_txid", "ABCDEF")); if (Build.VERSION.SDK_INT >= 18) { mBluetoothAdapter = mBluetoothManager.getAdapter(); } if (mBluetoothAdapter != null) { Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); if ((pairedDevices != null) && (pairedDevices.size() > 0)) { for (BluetoothDevice device : pairedDevices) { if (device.getName() != null) { String transmitterIdLastTwo = Extensions.lastTwoCharactersOfString(defaultTransmitter.transmitterId); String deviceNameLastTwo = Extensions.lastTwoCharactersOfString(device.getName()); if (transmitterIdLastTwo.equals(deviceNameLastTwo)) { currentdevice = defaultTransmitter.transmitterId; } } } } } else { currentdevice = "No Bluetooth"; } } return currentdevice; }
Example #25
Source File: RangeTestAdvertisementHandler.java From EFRConnect-android with Apache License 2.0 | 5 votes |
private BluetoothAdapter getBluetoothAdapter(Context context) { BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE); if (bluetoothManager == null) { throw new UnsupportedOperationException(); } return bluetoothManager.getAdapter(); }
Example #26
Source File: BeaconsFragment.java From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Checks whether the Bluetooth adapter is enabled. */ private boolean isBleEnabled() { final BluetoothManager bm = (BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE); final BluetoothAdapter ba = bm.getAdapter(); return ba != null && ba.isEnabled(); }
Example #27
Source File: TimeAttendantFastFragment.java From iBeacon-Android with Apache License 2.0 | 5 votes |
private void settingBlueTooth() { // init BLE btManager = (BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE); btAdapter = btManager.getAdapter(); if (Build.VERSION.SDK_INT >= 21) { mLEScanner = btAdapter.getBluetoothLeScanner(); settings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) .build(); filters = new ArrayList<>(); } }
Example #28
Source File: BTUtils.java From OpenXiaomiScale with Apache License 2.0 | 5 votes |
private static void prepareBTAdapter(final Activity activity) { // Initializes Bluetooth adapter. BluetoothManager bluetoothManager = (BluetoothManager) activity.getSystemService(Context.BLUETOOTH_SERVICE); bluetoothAdapter = bluetoothManager.getAdapter(); }
Example #29
Source File: FatBeaconBroadcastService.java From physical-web with Apache License 2.0 | 5 votes |
@Override public void onCreate() { super.onCreate(); mBluetoothLeAdvertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser(); mNotificationManager = NotificationManagerCompat.from(this); mBluetoothManager = (BluetoothManager) this.getSystemService(Context.BLUETOOTH_SERVICE); }
Example #30
Source File: PBluetoothLE.java From PHONK with GNU General Public License v3.0 | 5 votes |
public PBluetoothLE start() { // mDevices = new ArrayList<BluetoothDevice>(); mBleAdapter = ((BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter(); mAppRunner.whatIsRunning.add(this); mHandler = new Handler(Looper.getMainLooper()); return this; }