Java Code Examples for android.bluetooth.BluetoothAdapter#getDefaultAdapter()

The following examples show how to use android.bluetooth.BluetoothAdapter#getDefaultAdapter() . These examples are extracted from open source projects. 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
@Override
public void handleUpdateState(Object state, Object arg) {
    mState.visible = true;
    mState.booleanValue = false;

    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();

    int btState = adapter == null ? BluetoothAdapter.ERROR : adapter.getState();
    if (isInErrorState(btState)) {
        mState.label = mGbContext.getString(R.string.qs_tile_bt_tethering_error);
        mState.icon = mGbContext.getDrawable(R.drawable.ic_qs_bt_tethering_off);
    } else if (btState == BluetoothAdapter.STATE_TURNING_ON ||
            btState == BluetoothAdapter.STATE_TURNING_OFF) {
        mState.label = "---";
        mState.icon = mGbContext.getDrawable(R.drawable.ic_qs_bt_tethering_off);
    } else if (btState == BluetoothAdapter.STATE_ON && isTetheringOn()) {
        mState.label = mGbContext.getString(R.string.qs_tile_bt_tethering_on);
        mState.icon = mGbContext.getDrawable(R.drawable.ic_qs_bt_tethering_on);
        mState.booleanValue = true;
    } else {
        mState.label = mGbContext.getString(R.string.qs_tile_bt_tethering_off);
        mState.icon = mGbContext.getDrawable(R.drawable.ic_qs_bt_tethering_off);
    }

    super.handleUpdateState(state, arg);
}
 
Example 2
@SuppressLint("MissingPermission")
boolean isBluetoothActuallyAvailable(){
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if(adapter == null || !adapter.isEnabled() ){
        //False positive
        return false;
    }
    if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH ){
        int state = adapter.getProfileConnectionState(BluetoothProfile.A2DP);
        if(state != BluetoothAdapter.STATE_CONNECTING && state != BluetoothAdapter.STATE_CONNECTED){
            //False positive
            return false;
        }
    }

    return true;
}
 
Example 3
Source Project: unity-bluetooth   File: BCLServiceBase.java    License: MIT License 6 votes vote down vote up
public BCLServiceBase(final Activity activity) {
    mActivity = activity;
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();

    // Check enable bluetooth
    if ((mBtAdapter == null) || (!mBtAdapter.isEnabled())) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        activity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

    Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
    if (pairedDevices.size() > 0) {
        for (BluetoothDevice device : pairedDevices) {
            mPairedDevices.add(device);
        }
    }
}
 
Example 4
@Override
protected void onResume() {
    super.onResume();
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter != null) {
        isBluetoothAdapterEnabled = bluetoothAdapter.isEnabled();
    } else {
        isBluetoothAdapterEnabled = false;
    }

    if (!isBluetoothAdapterEnabled) {
        showEnableBluetoothAdapterBar();
    }

    if (!isLocationEnabled()) {
        showLocationDisabledBar();
    } else {
        hideLocationDisabledBar();
    }

    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(bluetoothAdapterStateChangeListener, filter);
}
 
Example 5
Source Project: bluetooth   File: Help_Fragment.java    License: Apache License 2.0 6 votes vote down vote up
public void startbt() {
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        // Device does not support Bluetooth
        mkmsg("This device does not support bluetooth");
        return;
    }
    //make sure bluetooth is enabled.
    if (!mBluetoothAdapter.isEnabled()) {
        mkmsg("There is bluetooth, but turned off");
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    } else {
        mkmsg("The bluetooth is ready to use.");
        //bluetooth is on, so list paired devices from here.
        querypaired();
    }
}
 
Example 6
Source Project: react-native-obd2   File: OBD2Handler.java    License: MIT License 6 votes vote down vote up
public void ready() {

    // onStart
    final BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

    // onResume
    // get Bluetooth device
    mPreRequisites = btAdapter != null && btAdapter.isEnabled();
    if (!mPreRequisites) {
      mPreRequisites = btAdapter != null && btAdapter.enable();
    }

    if (!mPreRequisites) {
      sendDeviceStatus(EVENTNAME_BT_STATUS, "disabled");
    } else {
      sendDeviceStatus(EVENTNAME_BT_STATUS, "ready");
    }

    sendDeviceStatus(EVENTNAME_OBD_STATUS, "disconnected");
  }
 
Example 7
Source Project: android   File: ScanActivity.java    License: MIT License 5 votes vote down vote up
private void initBluetooth() {
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null) {
        finish();
        return;
    }
    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, 2333);
    }
}
 
Example 8
Source Project: rubik-robot   File: MainActivity.java    License: MIT License 5 votes vote down vote up
public void connectBluetooth() {
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if (!mBluetoothAdapter.isEnabled()) {
        Toast.makeText(this, "Enable Bluetooth and pair", Toast.LENGTH_LONG).show();
        return;
    }
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("98:D3:31:30:3B:D8");
    for (BluetoothDevice x : mBluetoothAdapter.getBondedDevices()) {
        Log.d("Main", "Address: " + x.getAddress());
        for (ParcelUuid uuid : x.getUuids()) {
            Log.d("Main", "parceluuid:" + uuid.toString());
        }
    }
    try {
        mBluetoothAdapter.cancelDiscovery();

        BluetoothSocket socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"));
        socket.connect();

        mRobotScheduler = new RobotScheduler(socket.getInputStream(), socket.getOutputStream(), 10);
        Toast.makeText(this, "Connected to arduino", Toast.LENGTH_SHORT).show();

        mMapper = new SlightlyMoreAdvancedMapper();

    } catch (IOException e) {
        Log.d("Main", "Exception connecting to bluetooth: ", e);
    }
}
 
Example 9
public BluetoothBackendHelper(Context context, Listener listener){
    super(context);
    if (listener == null)
        throw new IllegalArgumentException("listener must not be null");
    this.listener = listener;
    this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    bluetoothBroadcastFilter.addAction(BluetoothDevice.ACTION_FOUND);
    bluetoothBroadcastFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    bluetoothBroadcastFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
}
 
Example 10
@Nullable
private static BluetoothInfoModel extractBluetoothInfo(DeviceInfoHelper deviceInfoHelper) {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    return bluetoothAdapter == null
            ? null
            : new BluetoothInfoModel(deviceInfoHelper.toBluetoothStateString(bluetoothAdapter.getState()));
}
 
Example 11
private void stopDetection() {
  if (BluetoothAdapter.getDefaultAdapter() != null) {
    try {
      context.unregisterReceiver(bluetoothStateReceiver);
    } catch (IllegalArgumentException e) {
      if (RxCentralLogger.isError()) {
        RxCentralLogger.error("stopDetection - Unregister receiver failed!");
      }
    }
  }

  bluetoothEnabledRelay.accept(Capability.UNSUPPORTED);
}
 
Example 12
private void reDiscover(boolean clearCachedDiscoveries) {
    if (BluetoothAdapter.getDefaultAdapter() != null && !BluetoothAdapter.getDefaultAdapter().isEnabled()) {
        return;
    }

    startDiscovery(discovery,clearCachedDiscoveries);
}
 
Example 13
Source Project: BluetoothStudy   File: MainActivity.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();

    initUI();
}
 
Example 14
Source Project: GizwitsBLE   File: BroadcomBle.java    License: Apache License 2.0 5 votes vote down vote up
public BroadcomBle(BleService service) {
	mService = service;
	mBtAdapter = BluetoothAdapter.getDefaultAdapter();
	if (mBtAdapter == null) {
		mService.bleNoBtAdapter();
		return;
	}
	BluetoothGattAdapter.getProfileProxy(mService, mProfileServiceListener,
			BluetoothGattAdapter.GATT);
}
 
Example 15
public BluetoothService(Context context, Handler handler) {
    mAdapter = BluetoothAdapter.getDefaultAdapter();
    mState = BluetoothState.STATE_NONE;
    mHandler = handler;
}
 
Example 16
Source Project: RxAndroidBle   File: ClientComponent.java    License: Apache License 2.0 4 votes vote down vote up
@Provides
@Nullable
static BluetoothAdapter provideBluetoothAdapter() {
    return BluetoothAdapter.getDefaultAdapter();
}
 
Example 17
public static BluetoothAdapter getBluetoothAdapter() {
    if (mBluetoothAdapter == null) {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    }
    return mBluetoothAdapter;
}
 
Example 18
Source Project: libcommon   File: BluetoothManager.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * ペアリング済みのBluetooth機器一覧を取得する
 * @return Bluetoothに対応していないまたは無効なら空set
 */
@NonNull
public static Set<BluetoothDevice> getBondedDevices() {
	final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
	return ((adapter != null) && adapter.isEnabled()) ? adapter.getBondedDevices() : Collections.emptySet();
}
 
Example 19
Source Project: Android-BLE   File: Ble.java    License: Apache License 2.0 4 votes vote down vote up
private BluetoothAdapter getBluetoothAdapter(){
    if (bluetoothAdapter == null){
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    }
    return bluetoothAdapter;
}
 
Example 20
/**
 * Connects to the remote BluetoothDevice (server).
 * It bootstraps the connection to the paired (bonded) device BluetoothDevice if exists.<br>
 * The function does return immediately, when the connection process is yet in progress, with no result.<br>
 * You can receive a notification when the connection process is completed attaching a BluetoothHelperListener.
 * As an alternative you can check the connection status with the isConnected() method described below.<br>
 * An onBluetoothHelperConnectionStateChanged event occurs (if listener is attached) when the connection process terminates, returning the new status of the connection.
 * In case of success, the class will be ready to communicate with the remote device.
 * @param bluetoothDevice The remote BluetoothDevice
 * @see android.bluetooth.BluetoothAdapter
 * @see android.bluetooth.BluetoothAdapter#getBondedDevices()
 * @see android.bluetooth.BluetoothDevice
 */
public void Connect(BluetoothDevice bluetoothDevice) {
    Log.w("myApp", "[ # ] Connect(BluetoothDevice bluetoothDevice)");
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();                                       // Find adapter
    if ((mBluetoothAdapter != null) && (bluetoothDevice != null)) {
        if (isConnected()) Disconnect(false);
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();                               // Find adapter
        if (mBluetoothAdapter.isEnabled()) {                                                    // Adapter found
            CT = new ConnectThread(bluetoothDevice);
            CT.start();
        }
    }
}