Java Code Examples for android.bluetooth.BluetoothAdapter#STATE_ON

The following examples show how to use android.bluetooth.BluetoothAdapter#STATE_ON . 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: BluetoothBroadcastReceiver.java    From SimpleBluetoothLibrary with Apache License 2.0 7 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();
    if(action == BluetoothAdapter.ACTION_STATE_CHANGED) {

        int previousState = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.ERROR);
        int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
        if(previousState == BluetoothAdapter.STATE_OFF || previousState == BluetoothAdapter.STATE_TURNING_OFF) {
            if(state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_TURNING_ON) {
                mCallaback.onBluetoothEnabled();
            } else if(state == BluetoothAdapter.STATE_OFF || state == BluetoothAdapter.STATE_TURNING_OFF) {
                mCallaback.onBluetoothDisabled();
            }
        }
    }
    else {
        return;
    }
}
 
Example 2
Source File: RileyLinkBluetoothStateReceiver.java    From AndroidAPS with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    PumpInterface activePump = ConfigBuilderPlugin.getPlugin().getActivePump();

    if (action != null && activePump != null) {

        final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
        switch (state) {
            case BluetoothAdapter.STATE_OFF:
            case BluetoothAdapter.STATE_TURNING_OFF:
            case BluetoothAdapter.STATE_TURNING_ON:
                break;

            case BluetoothAdapter.STATE_ON: {
                    LOG.debug("RileyLinkBluetoothStateReceiver: Bluetooth back on. Sending broadcast to RileyLink Framework");
                    RileyLinkUtil.sendBroadcastMessage(RileyLinkConst.Intents.BluetoothReconnected);
            }
            break;
        }
    }
}
 
Example 3
Source File: SystemIconController.java    From GravityBox with Apache License 2.0 6 votes vote down vote up
private void updateBtIconVisibility() {
    if (mSbService == null || mBtMode == null) return;

    try {
        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
        if (btAdapter != null) {
            boolean enabled = btAdapter.getState() == BluetoothAdapter.STATE_ON;
            boolean connected = (Integer) XposedHelpers.callMethod(btAdapter, "getConnectionState") ==
                    BluetoothAdapter.STATE_CONNECTED;
            boolean visible;
            switch (mBtMode) {
                default:
                case DEFAULT: visible = enabled; break;
                case CONNECTED: visible = connected; break;
                case HIDDEN: visible = false; break;
            }
            if (DEBUG) log("updateBtIconVisibility: enabled=" + enabled + "; connected=" + connected +
                    "; visible=" + visible);
            XposedHelpers.callMethod(mSbService, "setIconVisibility", "bluetooth", visible);
        }
    } catch (Throwable t) {
        XposedBridge.log(t);
    }
}
 
Example 4
Source File: BluetoothStateReceiver.java    From EFRConnect-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (action == null) {
        return;
    }
    if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
        int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);

        switch (state) {
            case BluetoothAdapter.ERROR:
            case BluetoothAdapter.STATE_OFF:
            case BluetoothAdapter.STATE_TURNING_OFF:
                notifyState(false);
                break;
            case BluetoothAdapter.STATE_ON:
                notifyState(true);
                break;
        }
    }
}
 
Example 5
Source File: BaseScanner.java    From neatle with MIT License 6 votes vote down vote up
private void conditionalStart() {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter == null) {
        NeatleLogger.e("Bluetooth LE scan failed to start. No bluetooth adapter found");
        return;
    }
    int adapterState = adapter.getState();
    if (adapterState != BluetoothAdapter.STATE_ON) {
        NeatleLogger.e("Bluetooth off, will start scanning when it turns on.");
        pause();
        return;
    }

    onStart(adapter, scanMode);
    doStarted = true;
    if (scanDuration > 0) {
        handler.postDelayed(pauseCallback, scanDuration);
    }
}
 
Example 6
Source File: BluetoothBroadcastReceiver.java    From CarBusInterface with MIT License 6 votes vote down vote up
@Override
public void onReceive(final Context context, final Intent intent) {
    final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
    final boolean automatic = settings.getBoolean("auto_launch", false);

    if (D) Log.d(TAG, "onReceive() : automatic= " + automatic);

    if (automatic) {
        if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);

            if (D) Log.d(TAG, "onReceive() : state= " + state);

            final Intent i = new Intent(context, CBIServiceMain.class);

            if (state == BluetoothAdapter.STATE_ON) {
                context.stopService(i);
                context.startService(i);
            } else if (state == BluetoothAdapter.STATE_OFF) {
                context.stopService(i);
            }
        }
    }
}
 
Example 7
Source File: Discovery.java    From EFRConnect-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onStateChanged(int bluetoothAdapterState) {
    bluetoothState = bluetoothAdapterState;

    if (bluetoothAdapterState == BluetoothAdapter.STATE_OFF) {
        isScanning = isDeviceStarted = false;
        isDeviceReady = null;

        if (isBluetoothEnabled) {
            isBluetoothEnabled = false;
            host.onAdapterDisabled();
            // Adapter was off, but became turned on:
            // Allow restarting the adapter (askForEnablingBluetoothAdapter will be called at some point)
        }
    } else if (bluetoothAdapterState == BluetoothAdapter.STATE_ON) {
        if (!isBluetoothEnabled) {
            isBluetoothEnabled = true;
            // The adapter was off and now turned on again. Re-start discovery to recover.
            //discoverDevices(false);
            //handleScanResults();
            host.onAdapterEnabled();
        }
    }
}
 
Example 8
Source File: BluetoothLinkLayerAdapter.java    From Rumble with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
        Log.d(TAG, "[!] BT State Changed");
        switch (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF)){
            case BluetoothAdapter.STATE_ON:
                linkStarted();
                break;
            case BluetoothAdapter.STATE_OFF:
                linkStopped();
                break;
            case BluetoothAdapter.STATE_TURNING_OFF:
            case BluetoothAdapter.STATE_TURNING_ON:
                break;
        }
    }

    if(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)){
    }
}
 
Example 9
Source File: GattClient.java    From blefun-androidthings with Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);

    switch (state) {
        case BluetoothAdapter.STATE_ON:
            startClient();
            break;
        case BluetoothAdapter.STATE_OFF:
            stopClient();
            break;
        default:
            // Do nothing
            break;
    }
}
 
Example 10
Source File: MainMenuActivity.java    From EFRConnect-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
        final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                BluetoothAdapter.ERROR);
        switch (state) {
            case BluetoothAdapter.STATE_OFF:
            case BluetoothAdapter.STATE_TURNING_OFF:
                isBluetoothAdapterEnabled = false;
                showEnableBluetoothAdapterBar();
                break;
            case BluetoothAdapter.STATE_ON:
                if (!isBluetoothAdapterEnabled) {
                    Toast.makeText(MainMenuActivity.this, R.string.toast_bluetooth_enabled, Toast.LENGTH_SHORT).show();
                }
                isBluetoothAdapterEnabled = true;
                bluetoothEnableBar.setVisibility(View.GONE);
                break;
            case BluetoothAdapter.STATE_TURNING_ON:
                isBluetoothAdapterEnabled = false;
                break;
        }
    }
}
 
Example 11
Source File: P_Task_TurnBleOn.java    From AsteroidOSSync with GNU General Public License v3.0 6 votes vote down vote up
@Override public void execute()
{
	if( getManager().managerLayer().getState() == BluetoothAdapter.STATE_ON )
	{
		redundant();
	}
	else if( getManager().managerLayer().getState() == BluetoothAdapter.STATE_TURNING_ON )
	{
		// DRK > Nothing to do, already turning on.
	}
	else
	{
		if( m_implicit )
		{
			fail();
		}
		else if( false == getManager().managerLayer().enable() )
		{
			fail();
		}
		else
		{
			// SUCCESS, so far...
		}
	}
}
 
Example 12
Source File: BluetoothManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private boolean bindService() {
    int state = BluetoothAdapter.STATE_OFF;
    try {
        mBluetoothLock.readLock().lock();
        if (mBluetooth != null) {
            state = mBluetooth.getState();
        }
    } catch (RemoteException e) {
        Slog.e(TAG, "Unable to call getState", e);
        return false;
    } finally {
        mBluetoothLock.readLock().unlock();
    }

    if (!mEnable || state != BluetoothAdapter.STATE_ON) {
        if (DBG) {
            Slog.d(TAG, "Unable to bindService while Bluetooth is disabled");
        }
        return false;
    }

    if (mIntent != null && mService == null && doBind(mIntent, this, 0,
            UserHandle.CURRENT_OR_SELF)) {
        Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE);
        msg.obj = this;
        mHandler.sendMessageDelayed(msg, TIMEOUT_BIND_MS);
        return true;
    }
    Slog.w(TAG, "Unable to bind with intent: " + mIntent);
    return false;
}
 
Example 13
Source File: Ble.java    From JayPS-AndroidApp with MIT License 5 votes vote down vote up
private void reconnectLater(BluetoothGatt gatt) {
    // Note: reconnectLater can be called just after STATE_TURNING_OFF
    if (mBluetoothAdapter == null || mBluetoothAdapter.getState() != BluetoothAdapter.STATE_ON) {
        return;
    }

    _nbReconnect++;
    try {
        int sleep;
        if (_nbReconnect < 5) {
            sleep = 5;
        } else if(_nbReconnect <= 20) {
            sleep = 15;
        } else {
            sleep = 60;
        }
        Log.w(TAG, display(gatt) + " reconnectLater, _nbReconnect: " + _nbReconnect + " wait " + sleep + "s");
        Thread.sleep(1000 * sleep);
    } catch (InterruptedException e) {
    }
    if (_bleStarted) {

        if (mGattsConnectionPending.containsKey(gatt.getDevice().getAddress())) {
            Log.w(TAG, display(gatt) + " reconnectLater already in mGattsConnectionPending, skip it");
        } else {
            Log.w(TAG, display(gatt) + " reconnectLater connectionQueue.add");
            connectionQueue.add(gatt.getDevice());
        }
    }
}
 
Example 14
Source File: PBluetoothLEClient.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onBluetoothAdapterStateChanged(int state) {
    MLog.d(TAG, "bluetooth adapter changed state to %d " + state);
    if(state == BluetoothAdapter.STATE_ON) {
        // Bluetooth is on now, start scanning again
        // Scan for peripherals with a certain service UUIDs
        central.startPairingPopupHack();
        // central.scanForPeripheralsWithServices(new UUID[]{BLP_SERVICE_UUID, HTS_SERVICE_UUID, HRS_SERVICE_UUID});
    }
}
 
Example 15
Source File: CycledLeScannerForLollipop.java    From android-beacon-library with Apache License 2.0 5 votes vote down vote up
private boolean isBluetoothOn() {
    try {
        BluetoothAdapter bluetoothAdapter = getBluetoothAdapter();
        if (bluetoothAdapter != null) {
            return (bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON);
        }
        LogManager.w(TAG, "Cannot get bluetooth adapter");
    }
    catch (SecurityException e) {
        LogManager.w(TAG, "SecurityException checking if bluetooth is on");
    }
    return false;
}
 
Example 16
Source File: CbtManager.java    From ClassicBluetooth with Apache License 2.0 5 votes vote down vote up
@Override
public void onStateSwitch(int state) {
    if (mStateSwitchCallback == null) {
        return;
    }
    if (state == BluetoothAdapter.STATE_ON) {
        mStateSwitchCallback.onStateChange(true);
    } else if (state == BluetoothAdapter.STATE_OFF) {
        mStateSwitchCallback.onStateChange(false);
    }
}
 
Example 17
Source File: BluetoothManager.java    From tap-android-sdk with Apache License 2.0 5 votes vote down vote up
public void registerBluetoothListener(@NonNull BluetoothListener listener) {
    isClosed = false;

    bluetoothListeners.registerListener(listener);
    if (bluetoothAdapter != null && bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) {
        listener.onBluetoothTurnedOn();
        establishConnections();
    } else {
        notifyOnError(EMPTY_DEVICE_ADDRESS, ERR_C_BLUETOOTH_OFF, ErrorStrings.BLUETOOTH_OFF);
    }
}
 
Example 18
Source File: BleProfileService.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onReceive(final Context context, final Intent intent) {
	final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);

	switch (state) {
		case BluetoothAdapter.STATE_ON:
			onBluetoothEnabled();
			break;
		case BluetoothAdapter.STATE_TURNING_OFF:
		case BluetoothAdapter.STATE_OFF:
			onBluetoothDisabled();
			break;
	}
}
 
Example 19
Source File: BluetoothWrapper.java    From phonegap-bluetooth-plugin with MIT License 5 votes vote down vote up
/**
 * Check whether Bluetooth is on or off.
 *
 * @return Flag indicating if Bluetooth is enabled on this device.
 * @throws Exception When there is an error deducing adapter state.
 */
public boolean isEnabled() throws Exception
{
	try
	{
		return _adapter.getState() == BluetoothAdapter.STATE_ON;
	}
	catch(Exception e)
	{
		throw e;
	}
}
 
Example 20
Source File: UnitTestManagerLayer.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
protected void setToOn()
{
    NativeUtil.sendBluetoothStateChange(BleManager.s_instance, m_nativeState, BluetoothAdapter.STATE_ON);
    m_nativeState = BluetoothAdapter.STATE_ON;
}