下面列出了android.bluetooth.BluetoothAdapter#STATE_OFF 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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...
}
}
}
@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
}
}
/**
* 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();
}
}
@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();
}
}
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;
}
}
@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();
}
}
}
@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);
}
@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;
}
}
@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;
}
}
}
@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:
}
}
}
@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;
}
}
/** 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";
}
}
/**
* 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;
}
/** 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";
}
}
@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);
}
}
@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();
}
}
}
/** 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";
}
}
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";
}
}
@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;
}
}
}
@Override
public void onBluetoothStateChanged() {
if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_OFF) {
stopScan();
}
}