Java Code Examples for android.bluetooth.BluetoothAdapter#STATE_OFF

The following examples show how to use android.bluetooth.BluetoothAdapter#STATE_OFF . 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: DataLoggerService.java    From DataLogger with MIT License 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:
                updateBluetoothState(Constants.BLUETOOTH_STATE_DISABLED);
                break;
            case BluetoothAdapter.STATE_ON:
                updateBluetoothState(Constants.BLUETOOTH_STATE_ENABLED);
                break;
        }
    }
}
 
Example 2
Source File: P_Task_TurnBleOff.java    From SweetBlue with GNU General Public License v3.0 6 votes vote down vote up
@Override public void execute()
{
	if( getManager().managerLayer().getState() == BluetoothAdapter.STATE_OFF )
	{
		redundant();
	}
	else if( getManager().managerLayer().getState() == BluetoothAdapter.STATE_TURNING_OFF )
	{
		// DRK > Nothing to do, already turning off.
	}
	else
	{
		if( m_implicit )
		{
			this.fail();
		}
		else if( false == getManager().managerLayer().disable() )
		{
			this.fail();
		}
		else
		{
			// SUCCESS, for now...
		}
	}
}
 
Example 3
Source File: GattServerActivity.java    From sample-bluetooth-le-gattserver 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:
            startAdvertising();
            startServer();
            break;
        case BluetoothAdapter.STATE_OFF:
            stopServer();
            stopAdvertising();
            break;
        default:
            // Do nothing
    }

}
 
Example 4
Source File: BluetoothRadioStatusListener.java    From bitgatt with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Constructor for the listener, if desired as a convenience can start listening immediately
 *
 * @param context                   The android context
 * @param shouldInitializeListening Whether to attach to the global broadcast immediately
 */

BluetoothRadioStatusListener(Context context, boolean shouldInitializeListening) {
    this.context = context;
    BluetoothAdapter adapter = new GattUtils().getBluetoothAdapter(context);
    // if we are in this condition, something is seriously wrong
    this.currentState = (adapter != null) ? adapter.getState() : BluetoothAdapter.STATE_OFF;
    // this handler is to deliver the callbacks in the same way as they would usually
    // be delivered, but we want to avoid flapping ( user toggling on and off quickly )
    // so that our protocol stacks do not get set up in a half state
    this.mainHandler = new Handler(Looper.getMainLooper());
    if (shouldInitializeListening) {
        Timber.d("Starting listener");
        startListening();
    }
}
 
Example 5
Source File: BluetoothMainActivity.java    From AndroidDemo with MIT License 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
        int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
        if (BluetoothAdapter.STATE_OFF == state) {
            Log.d(TAG, "state: " + state);
        } else if (BluetoothAdapter.STATE_ON == state) {
            Log.d(TAG, "state: " + state);
            discoverDevice();
        }
    } else if (action.equals(BluetoothDevice.ACTION_FOUND)) {
        Log.d(TAG, "found");
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        foundDevices.add(device);
    } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
        loadDiscoverDevices();
    }
}
 
Example 6
Source File: BleUtils.java    From Bluefruit_LE_Connect_Android with MIT License 6 votes vote down vote up
private void onBleAdapterStatusChanged(int state, Context context) {
    switch (state) {
        case BluetoothAdapter.STATE_OFF: {
            // Turn off has finished. Turn it on again
            Log.d(TAG, "Ble adapter turned off. Turning on");
            BluetoothAdapter bleAdapter = BleUtils.getBluetoothAdapter(context);
            if (bleAdapter != null) {
                bleAdapter.enable();
            }
            break;
        }
        case BluetoothAdapter.STATE_TURNING_OFF:
            break;
        case BluetoothAdapter.STATE_ON: {
            Log.d(TAG, "Ble adapter turned on. Reset completed");
            // Turn on has finished.
            resetCompleted(context);
            break;
        }
        case BluetoothAdapter.STATE_TURNING_ON:
            break;
    }
}
 
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: BleManager.java    From react-native-ble-manager with Apache License 2.0 6 votes vote down vote up
@ReactMethod
public void checkState() {
	Log.d(LOG_TAG, "checkState");

	BluetoothAdapter adapter = getBluetoothAdapter();
	String state = "off";
	if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
		state = "unsupported";
	} else if (adapter != null) {
		switch (adapter.getState()) {
			case BluetoothAdapter.STATE_ON:
				state = "on";
				break;
			case BluetoothAdapter.STATE_OFF:
				state = "off";
		}
	}

	WritableMap map = Arguments.createMap();
	map.putString("state", state);
	Log.d(LOG_TAG, "state:" + state);
	sendEvent("BleManagerDidUpdateState", map);
}
 
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: AppRTCBluetoothManager.java    From react-native-incall-manager with ISC License 5 votes vote down vote up
/** Converts BluetoothAdapter states into local string representations. */
private String stateToString(int state) {
  switch (state) {
    case BluetoothAdapter.STATE_DISCONNECTED:
      return "DISCONNECTED";
    case BluetoothAdapter.STATE_CONNECTED:
      return "CONNECTED";
    case BluetoothAdapter.STATE_CONNECTING:
      return "CONNECTING";
    case BluetoothAdapter.STATE_DISCONNECTING:
      return "DISCONNECTING";
    case BluetoothAdapter.STATE_OFF:
      return "OFF";
    case BluetoothAdapter.STATE_ON:
      return "ON";
    case BluetoothAdapter.STATE_TURNING_OFF:
      // Indicates the local Bluetooth adapter is turning off. Local clients should immediately
      // attempt graceful disconnection of any remote links.
      return "TURNING_OFF";
    case BluetoothAdapter.STATE_TURNING_ON:
      // Indicates the local Bluetooth adapter is turning on. However local clients should wait
      // for STATE_ON before attempting to use the adapter.
      return  "TURNING_ON";
    default:
      return "INVALID";
  }
}
 
Example 11
Source File: BluetoothStateReceiver.java    From Android-BluetoothKit with Apache License 2.0 5 votes vote down vote up
private String getStateString(int state) {
    switch (state) {
        case BluetoothAdapter.STATE_ON: return "state_on";
        case BluetoothAdapter.STATE_OFF: return "state_off";
        case BluetoothAdapter.STATE_TURNING_OFF: return "state_turning_off";
        case BluetoothAdapter.STATE_TURNING_ON: return "state_turning_on";
        default: return "unknown";
    }
}
 
Example 12
Source File: AppRTCBluetoothManager.java    From flutter-incall-manager with ISC License 5 votes vote down vote up
/** Converts BluetoothAdapter states into local string representations. */
private String stateToString(int state) {
  switch (state) {
    case BluetoothAdapter.STATE_DISCONNECTED:
      return "DISCONNECTED";
    case BluetoothAdapter.STATE_CONNECTED:
      return "CONNECTED";
    case BluetoothAdapter.STATE_CONNECTING:
      return "CONNECTING";
    case BluetoothAdapter.STATE_DISCONNECTING:
      return "DISCONNECTING";
    case BluetoothAdapter.STATE_OFF:
      return "OFF";
    case BluetoothAdapter.STATE_ON:
      return "ON";
    case BluetoothAdapter.STATE_TURNING_OFF:
      // Indicates the local Bluetooth adapter is turning off. Local clients should immediately
      // attempt graceful disconnection of any remote links.
      return "TURNING_OFF";
    case BluetoothAdapter.STATE_TURNING_ON:
      // Indicates the local Bluetooth adapter is turning on. However local clients should wait
      // for STATE_ON before attempting to use the adapter.
      return  "TURNING_ON";
    default:
      return "INVALID";
  }
}
 
Example 13
Source File: SdlRouterService.java    From sdl_java_suite with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
	@SuppressWarnings("MissingPermission")
	public void onReceive(Context context, Intent intent) 
	{
		String action = intent.getAction();
    	if(action == null){
			Log.d(TAG, "Disconnect received with no action.");
		}else {
			Log.d(TAG, "Disconnect received. Action: " + intent.getAction());

			if(action.equalsIgnoreCase(BluetoothAdapter.ACTION_STATE_CHANGED)){
				int bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
				switch (bluetoothState) {
					case BluetoothAdapter.STATE_TURNING_ON:
					case BluetoothAdapter.STATE_ON:
						//There is nothing to do in the case the adapter is turning on or just switched to on
						return;
					case BluetoothAdapter.STATE_TURNING_OFF:
					case BluetoothAdapter.STATE_OFF:
						Log.d(TAG, "Bluetooth is shutting off, SDL Router Service is closing.");
						connectAsClient = false;
						if(!shouldServiceRemainOpen(intent)){
							closeSelf();
						}
						return;
					default:
						break;
				}
			}
			//Otherwise
			connectAsClient = false;
			if (legacyModeEnabled) {
				Log.d(TAG, "Legacy mode enabled and bluetooth d/c'ed, restarting router service bluetooth.");
				enableLegacyMode(false);
				onTransportDisconnected(new TransportRecord(TransportType.BLUETOOTH,null));
				initBluetoothSerialService();
			}
		}
}
 
Example 14
Source File: LinkBackground.java    From itracing2 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent)
{
    final int bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
    Log.d(BluetoothLEService.TAG, "bluetooth change state: " + bluetoothState);
    final Intent bleService = new Intent(context, BluetoothLEService.class);

    if (bluetoothState == BluetoothAdapter.STATE_ON) {
        context.startService(bleService);
    }

    if (bluetoothState == BluetoothAdapter.STATE_OFF) {
        context.stopService(bleService);
    }
}
 
Example 15
Source File: AppRTCBluetoothManager.java    From imsdk-android with MIT License 5 votes vote down vote up
/** Converts BluetoothAdapter states into local string representations. */
private String stateToString(int state) {
  switch (state) {
    case BluetoothAdapter.STATE_DISCONNECTED:
      return "DISCONNECTED";
    case BluetoothAdapter.STATE_CONNECTED:
      return "CONNECTED";
    case BluetoothAdapter.STATE_CONNECTING:
      return "CONNECTING";
    case BluetoothAdapter.STATE_DISCONNECTING:
      return "DISCONNECTING";
    case BluetoothAdapter.STATE_OFF:
      return "OFF";
    case BluetoothAdapter.STATE_ON:
      return "ON";
    case BluetoothAdapter.STATE_TURNING_OFF:
      // Indicates the local Bluetooth adapter is turning off. Local clients should immediately
      // attempt graceful disconnection of any remote links.
      return "TURNING_OFF";
    case BluetoothAdapter.STATE_TURNING_ON:
      // Indicates the local Bluetooth adapter is turning on. However local clients should wait
      // for STATE_ON before attempting to use the adapter.
      return  "TURNING_ON";
    default:
      return "INVALID";
  }
}
 
Example 16
Source File: BluetoothManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 *  if on is true, wait for state become ON
 *  if off is true, wait for state become OFF
 *  if both on and off are false, wait for state not ON
 */
private boolean waitForOnOff(boolean on, boolean off) {
    int i = 0;
    while (i < 10) {
        try {
            mBluetoothLock.readLock().lock();
            if (mBluetooth == null) {
                break;
            }
            if (on) {
                if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) {
                    return true;
                }
            } else if (off) {
                if (mBluetooth.getState() == BluetoothAdapter.STATE_OFF) {
                    return true;
                }
            } else {
                if (mBluetooth.getState() != BluetoothAdapter.STATE_ON) {
                    return true;
                }
            }
        } catch (RemoteException e) {
            Slog.e(TAG, "getState()", e);
            break;
        } finally {
            mBluetoothLock.readLock().unlock();
        }
        if (on || off) {
            SystemClock.sleep(300);
        } else {
            SystemClock.sleep(50);
        }
        i++;
    }
    Slog.e(TAG, "waitForOnOff time out");
    return false;
}
 
Example 17
Source File: ScannerFragment.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
	// This will be executed only once
	final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);

	switch (state) {
		case BluetoothAdapter.STATE_TURNING_OFF:
		case BluetoothAdapter.STATE_OFF:
			stopScan();
			mAdapter.clearDevices();
			break;
	}
}
 
Example 18
Source File: FatBeaconBroadcastService.java    From physical-web with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
  String action = intent.getAction();

  if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
    int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
    switch (state) {
      case BluetoothAdapter.STATE_OFF:
        stopSelf();
        break;
      default:

    }
  }
}
 
Example 19
Source File: BleScanner.java    From EasyBle with Apache License 2.0 4 votes vote down vote up
@Override
public void onBluetoothStateChanged() {
    if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_OFF) {
        stopScan();
    }
}
 
Example 20
Source File: P_BluetoothCrashResolver.java    From AsteroidOSSync with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
        if (recoveryInProgress) {
            if (isDebugEnabled()) Log.d(TAG, "Bluetooth discovery finished");
            finishRecovery();
        }
        else {
            if (isDebugEnabled()) Log.d(TAG, "Bluetooth discovery finished (external)");
        }
    }
    if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)) {
        if (recoveryInProgress) {
            discoveryStartConfirmed = true;
            if (isDebugEnabled()) Log.d(TAG, "Bluetooth discovery started");
        }
        else {
            if (isDebugEnabled()) Log.d(TAG, "Bluetooth discovery started (external)");
        }
    }

    if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
        final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                BluetoothAdapter.ERROR);
        switch (state) {
            case BluetoothAdapter.ERROR:
                if (isDebugEnabled()) Log.d(TAG, "Bluetooth state is ERROR");
                break;
            case BluetoothAdapter.STATE_OFF:
                if (isDebugEnabled()) Log.d(TAG, "Bluetooth state is OFF");
                lastBluetoothOffTime = new Date().getTime();
                break;
            case BluetoothAdapter.STATE_TURNING_OFF:
                break;
            case BluetoothAdapter.STATE_ON:
                if (isDebugEnabled()) Log.d(TAG, "Bluetooth state is ON");
                if (isDebugEnabled()) Log.d(TAG, "Bluetooth was turned off for "+(lastBluetoothTurningOnTime - lastBluetoothOffTime)+" milliseconds");
                if (lastBluetoothTurningOnTime - lastBluetoothOffTime < SUSPICIOUSLY_SHORT_BLUETOOTH_OFF_INTERVAL_MILLIS) {
                    crashDetected();
                }
                break;
            case BluetoothAdapter.STATE_TURNING_ON:
                lastBluetoothTurningOnTime = new Date().getTime();
                if (isDebugEnabled()) Log.d(TAG, "Bluetooth state is TURNING_ON");
                break;
        }
    }
}