Java Code Examples for android.bluetooth.BluetoothAdapter#STATE_OFF

The following examples show how to use android.bluetooth.BluetoothAdapter#STATE_OFF . 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 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 2
@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 3
/**
 * 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 4
Source Project: AndroidDemo   File: BluetoothMainActivity.java    License: 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 5
Source Project: Bluefruit_LE_Connect_Android   File: BleUtils.java    License: 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 6
Source Project: EFRConnect-android   File: Discovery.java    License: 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 7
@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 8
Source Project: blefun-androidthings   File: GattClient.java    License: 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 9
Source Project: DataLogger   File: DataLoggerService.java    License: 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 10
@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 11
@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 12
/** 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
/**
 *  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 14
Source Project: imsdk-android   File: AppRTCBluetoothManager.java    License: 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 15
@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 16
@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 17
/** 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 18
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 19
@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;
        }
    }
}
 
Example 20
Source Project: EasyBle   File: BleScanner.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void onBluetoothStateChanged() {
    if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_OFF) {
        stopScan();
    }
}