Example usage for android.bluetooth BluetoothAdapter STATE_OFF

List of usage examples for android.bluetooth BluetoothAdapter STATE_OFF

Introduction

In this page you can find the example usage for android.bluetooth BluetoothAdapter STATE_OFF.

Prototype

int STATE_OFF

To view the source code for android.bluetooth BluetoothAdapter STATE_OFF.

Click Source Link

Document

Indicates the local Bluetooth adapter is off.

Usage

From source file:Main.java

public static void toggleBlueTooth(Context context) {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter != null) {
        switch (adapter.getState()) {
        case BluetoothAdapter.STATE_OFF:
            adapter.enable();/*ww w .  ja v  a  2  s  . c  om*/
            break;
        case BluetoothAdapter.STATE_ON:
            adapter.disable();
            break;
        }
    }
}

From source file:Main.java

static public String state2String(int state) {
    final String ON = "STATE_ON";
    final String OFF = "STATE_OFF";
    final String TURNING_ON = "STATE_TURNING_ON";
    final String TURNING_OFF = "STATE_TURNING_OFF";

    switch (state) {
    case BluetoothAdapter.STATE_ON:
        return ON;
    case BluetoothAdapter.STATE_OFF:
        return OFF;
    case BluetoothAdapter.STATE_TURNING_ON:
        return TURNING_ON;
    case BluetoothAdapter.STATE_TURNING_OFF:
        return TURNING_OFF;
    default://from  w ww. ja  v a  2  s . co m
        return UNKNOWN;
    }
}

From source file:Main.java

public static String getStateString(int state) {
    switch (state) {
    case BluetoothAdapter.STATE_CONNECTED:
        return "Connected";
    case BluetoothAdapter.STATE_CONNECTING:
        return "Connecting";
    case BluetoothAdapter.STATE_DISCONNECTED:
        return "Disconnected";
    case BluetoothAdapter.STATE_DISCONNECTING:
        return "Disconnecting";
    case BluetoothAdapter.STATE_OFF:
        return "Off";
    case BluetoothAdapter.STATE_ON:
        return "On";
    case BluetoothAdapter.STATE_TURNING_OFF:
        return "Turning Off";
    case BluetoothAdapter.STATE_TURNING_ON:
        return "Turning On";
    default:/*www.  j  av  a  2s. c  om*/
        return "Unknown";
    }
}

From source file:Main.java

public static int getBluetoothState(Context context) {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter != null)
        return adapter.getState();
    else {/*ww  w  . jav a  2  s.c o m*/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
            adapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
        if (adapter != null)
            return adapter.getState();
        else
            return BluetoothAdapter.STATE_OFF;
    }
}

From source file:Main.java

public static boolean isBluetoothAvailable() {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (!adapter.isEnabled()) {
        if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
            adapter.enable();// ww  w . j  a va2s . c o m
        }
    }
    return adapter.getState() == BluetoothAdapter.STATE_ON;
}

From source file:com.jaguarlandrover.auto.remote.vehicleentry.RviService.java

protected void starting(Intent intent) {
    if (intent != null && intent.getExtras() != null) {
        int btState = intent.getExtras().getInt("bluetooth", BluetoothAdapter.STATE_OFF);
        if (btState == BluetoothAdapter.STATE_ON) {
            Log.w(TAG, "BT on, start ranging");
            br.start();/*from w ww.  j a v a2  s.co  m*/
        } else if (btState == BluetoothAdapter.STATE_OFF) {
            Log.w(TAG, "BT off, stop ranging");
            br.stop();
        }
    }
}

From source file:is.hello.buruberi.bluetooth.stacks.android.NativeBluetoothStack.java

@RequiresPermission(Manifest.permission.BLUETOOTH)
public NativeBluetoothStack(@NonNull Context applicationContext, @NonNull ErrorListener errorListener,
        @NonNull LoggerFacade logger) {//from   w w w  .j a  v  a2  s.c o  m
    this.applicationContext = applicationContext;
    this.errorListener = errorListener;
    this.logger = logger;

    this.bluetoothManager = (BluetoothManager) applicationContext.getSystemService(Context.BLUETOOTH_SERVICE);
    this.adapter = bluetoothManager.getAdapter();
    if (adapter != null) {
        final BroadcastReceiver powerStateReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                final int newState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                if (newState == BluetoothAdapter.STATE_ON) {
                    enabled.onNext(true);
                } else if (newState == BluetoothAdapter.STATE_OFF || newState == BluetoothAdapter.ERROR) {
                    enabled.onNext(false);
                }
            }
        };
        applicationContext.registerReceiver(powerStateReceiver,
                new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
        enabled.onNext(adapter.isEnabled());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            final BroadcastReceiver pairingReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    final Intent broadcast = new Intent(ACTION_PAIRING_REQUEST);
                    broadcast.putExtra(GattPeripheral.EXTRA_NAME, device.getName());
                    broadcast.putExtra(GattPeripheral.EXTRA_ADDRESS, device.getAddress());
                    LocalBroadcastManager.getInstance(context).sendBroadcast(broadcast);
                }
            };
            applicationContext.registerReceiver(pairingReceiver,
                    new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST));
        }
    } else {
        logger.warn(LOG_TAG, "Host device has no bluetooth hardware!");
        enabled.onNext(false);
    }
}

From source file:org.deviceconnect.android.deviceplugin.sphero.setting.fragment.DeviceSelectionPageFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    isThreadRunning = true;//from  ww  w . j a va2  s .co m
    new Thread(new Runnable() {
        @Override
        public void run() {
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (mBluetoothAdapter == null) {
                return;
            }
            int prevState = mBluetoothAdapter.getState();
            while (isThreadRunning) {
                if (prevState != mBluetoothAdapter.getState()) {
                    if ((mBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON)
                            && (prevState == BluetoothAdapter.STATE_TURNING_ON
                                    || prevState == BluetoothAdapter.STATE_OFF)) {
                        threadHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                stopDiscovery();
                                startDiscovery();
                            }
                        });
                    }
                    prevState = mBluetoothAdapter.getState();
                }

                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}

From source file:org.chromium.ChromeBluetooth.java

private JSONObject getAdapterStateInfo() throws JSONException {
    JSONObject stateInfo = new JSONObject();
    stateInfo.put("address", bluetoothAdapter.getAddress());
    stateInfo.put("name", bluetoothAdapter.getName());
    stateInfo.put("powered", bluetoothAdapter.getState() != BluetoothAdapter.STATE_OFF);
    stateInfo.put("available", bluetoothAdapter.isEnabled());
    stateInfo.put("discovering", isLeScanning);
    return stateInfo;
}

From source file:com.piusvelte.taplock.client.core.TapLockService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        String action = intent.getAction();
        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF);
            if (state == BluetoothAdapter.STATE_ON) {
                if (mStartedBT) {
                    if (mUIInterface != null) {
                        try {
                            mUIInterface.setMessage("Bluetooth enabled");
                        } catch (RemoteException e) {
                            Log.e(TAG, e.getMessage());
                        }/* w  w w.  jav a2s .  c  om*/
                    }
                    if ((mQueueAddress != null) && (mQueueState != null))
                        requestWrite(mQueueAddress, mQueueState, mQueuePassphrase);
                    else if (mRequestDiscovery && !mBtAdapter.isDiscovering())
                        mBtAdapter.startDiscovery();
                    else if (mUIInterface != null) {
                        try {
                            mUIInterface.setBluetoothEnabled();
                        } catch (RemoteException e) {
                            Log.e(TAG, e.getMessage());
                        }
                    }
                }
            } else if (state == BluetoothAdapter.STATE_TURNING_OFF) {
                if (mUIInterface != null) {
                    try {
                        mUIInterface.setMessage("Bluetooth disabled");
                    } catch (RemoteException e) {
                        Log.e(TAG, e.getMessage());
                    }
                }
                stopThreads();
            }
        } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
                // connect if configured
                String address = device.getAddress();
                for (JSONObject deviceJObj : mDevices) {
                    try {
                        if (deviceJObj.getString(KEY_ADDRESS).equals(address)) {
                            // if queued
                            mDeviceFound = (mQueueAddress != null) && mQueueAddress.equals(address)
                                    && (mQueueState != null);
                            break;
                        }
                    } catch (JSONException e) {
                        Log.e(TAG, e.getMessage());
                    }
                }
            } else if (mRequestDiscovery && (mUIInterface != null)) {
                String unpairedDevice = TapLock
                        .createDevice(device.getName(), device.getAddress(), DEFAULT_PASSPHRASE).toString();
                try {
                    mUIInterface.setUnpairedDevice(unpairedDevice);
                } catch (RemoteException e) {
                    Log.e(TAG, e.getMessage());
                }
            }
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            if (mDeviceFound) {
                requestWrite(mQueueAddress, mQueueState, mQueuePassphrase);
                mDeviceFound = false;
            } else if (mRequestDiscovery) {
                mRequestDiscovery = false;
                if (mUIInterface != null) {
                    try {
                        mUIInterface.setDiscoveryFinished();
                    } catch (RemoteException e) {
                        Log.e(TAG, e.toString());
                    }
                }
            }
        } else if (ACTION_TOGGLE.equals(action) && intent.hasExtra(EXTRA_DEVICE_ADDRESS)) {
            String address = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);
            requestWrite(address, ACTION_TOGGLE, null);
        } else if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
            // create widget
            if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
                int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                        AppWidgetManager.INVALID_APPWIDGET_ID);
                if (intent.hasExtra(EXTRA_DEVICE_NAME)) {
                    // add a widget
                    String deviceName = intent.getStringExtra(EXTRA_DEVICE_NAME);
                    for (int i = 0, l = mDevices.size(); i < l; i++) {
                        String name = null;
                        try {
                            name = mDevices.get(i).getString(KEY_NAME);
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                        if ((name != null) && name.equals(deviceName)) {
                            JSONObject deviceJObj = mDevices.remove(i);
                            JSONArray widgetsJArr;
                            if (deviceJObj.has(KEY_WIDGETS)) {
                                try {
                                    widgetsJArr = deviceJObj.getJSONArray(KEY_WIDGETS);
                                } catch (JSONException e) {
                                    widgetsJArr = new JSONArray();
                                }
                            } else
                                widgetsJArr = new JSONArray();
                            widgetsJArr.put(appWidgetId);
                            try {
                                deviceJObj.put(KEY_WIDGETS, widgetsJArr);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            mDevices.add(i, deviceJObj);
                            TapLock.storeDevices(this, getSharedPreferences(KEY_PREFS, MODE_PRIVATE), mDevices);
                            break;
                        }
                    }
                }
                buildWidget(appWidgetId);
            } else if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS)) {
                int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
                if (appWidgetIds != null) {
                    for (int appWidgetId : appWidgetIds)
                        buildWidget(appWidgetId);
                }
            }
        } else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
            int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
            Log.d(TAG, "delete appWidgetId: " + appWidgetId);
            for (int i = 0, l = mDevices.size(); i < l; i++) {
                JSONObject deviceJObj = mDevices.get(i);
                if (deviceJObj.has(KEY_WIDGETS)) {
                    JSONArray widgetsJArr = null;
                    try {
                        widgetsJArr = deviceJObj.getJSONArray(KEY_WIDGETS);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    if (widgetsJArr != null) {
                        boolean wasUpdated = false;
                        JSONArray newWidgetsJArr = new JSONArray();
                        for (int widgetIdx = 0, wdigetsLen = widgetsJArr
                                .length(); widgetIdx < wdigetsLen; widgetIdx++) {
                            int widgetId;
                            try {
                                widgetId = widgetsJArr.getInt(widgetIdx);
                            } catch (JSONException e) {
                                widgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
                                e.printStackTrace();
                            }
                            Log.d(TAG, "eval widgetId: " + widgetId);
                            if ((widgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
                                    && (widgetId == appWidgetId)) {
                                Log.d(TAG, "skip: " + widgetId);
                                wasUpdated = true;
                            } else {
                                Log.d(TAG, "include: " + widgetId);
                                newWidgetsJArr.put(widgetId);
                            }
                        }
                        if (wasUpdated) {
                            try {
                                deviceJObj.put(KEY_WIDGETS, newWidgetsJArr);
                                mDevices.remove(i);
                                mDevices.add(i, deviceJObj);
                                TapLock.storeDevices(this, getSharedPreferences(KEY_PREFS, MODE_PRIVATE),
                                        mDevices);
                                Log.d(TAG, "stored: " + deviceJObj.toString());
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                } else {
                    JSONArray widgetsJArr = new JSONArray();
                    try {
                        deviceJObj.put(KEY_WIDGETS, widgetsJArr);
                        mDevices.remove(i);
                        mDevices.add(i, deviceJObj);
                        TapLock.storeDevices(this, getSharedPreferences(KEY_PREFS, MODE_PRIVATE), mDevices);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    return START_STICKY;
}