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; }
/** * 获取蓝牙的状态 * * @return 取值为BluetoothAdapter的四个静态字段:STATE_OFF, STATE_TURNING_OFF, * STATE_ON, STATE_TURNING_ON * @throws Exception * 没有找到蓝牙设备 */ public static int getBluetoothState() throws Exception { BluetoothAdapter bluetoothAdapter = BluetoothAdapter .getDefaultAdapter(); if (bluetoothAdapter == null) { throw new Exception("bluetooth device not found!"); } else { return bluetoothAdapter.getState(); } }
@Override public void onReceive(Context context, Intent intent) { if (listener != null) { final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); final NetworkInfo mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); final BluetoothAdapter bluetoothInfo = BluetoothAdapter.getDefaultAdapter(); final NetworkChangeEvent networkChangeEvent = new NetworkChangeEvent( (wifiInfo != null) ? wifiInfo.getState() : NetworkInfo.State.UNKNOWN, (mobileInfo != null) ? mobileInfo.getState() : NetworkInfo.State.UNKNOWN, (bluetoothInfo != null && hasBluetoothPermission(context)) ? getBluetoothState(bluetoothInfo.getState()) : BluetoothState.UNKNOWN); listener.post(networkChangeEvent); } }
BluetoothAdapter btAdapter = ((Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) ?((BluetoothManager)mContext.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter() :(BluetoothAdapter.getDefaultAdapter())); if(btAdapter==null){ return; } if(btAdapter.getState()==BluetoothAdapter.STATE_ON){ //Bluetooth is ON }
@Override public void btStatusChanged(Intent intent) { if ( bluetoothAdapter.getState()==BluetoothAdapter.STATE_OFF ){//蓝牙被关闭时强制打开 bluetoothAdapter.enable(); } if ( bluetoothAdapter.getState()==BluetoothAdapter.STATE_ON ){//蓝牙打开时搜索蓝牙 searchDeviceOrOpenBluetooth(); } } private String getPrinterName(){
/** * Ensure Bluetooth is turned on. * * @throws IllegalStateException If {@code adapter} is null or Bluetooth state is not * {@link BluetoothAdapter#STATE_ON}. */ @RequiresPermission(Manifest.permission.BLUETOOTH) static void checkAdapterStateOn(@Nullable final BluetoothAdapter adapter) { if (adapter == null || adapter.getState() != BluetoothAdapter.STATE_ON) { throw new IllegalStateException("BT Adapter is not turned ON"); } }
/** * Ensure Bluetooth is turned on. * * @throws IllegalStateException If {@code adapter} is null or Bluetooth state is not * {@link BluetoothAdapter#STATE_ON}. */ @RequiresPermission(Manifest.permission.BLUETOOTH) static void checkAdapterStateOn(BluetoothAdapter adapter) { if (adapter == null || adapter.getState() != BluetoothAdapter.STATE_ON) { throw new IllegalStateException("BT Adapter is not turned ON"); } }
@Override public BitmapDrawable getDrawableState(Context context) { if (available()) { switch (adapter.getState()) { case BluetoothAdapter.STATE_OFF: return (BitmapDrawable) context.getResources().getDrawable(R.drawable.ic_bluetooth_off); case BluetoothAdapter.STATE_ON: return (BitmapDrawable) context.getResources().getDrawable(R.drawable.ic_bluetooth_on); } } return (BitmapDrawable) context.getResources().getDrawable(R.drawable.ic_bluetooth_off); }
public static int getBluetoothState(Context context) { BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); if (adapter != null) return adapter.getState(); else { 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; } }
/** * Get current bluetooth state. * @return an integer value represent the bluetooth state, * Possible return values are STATE_OFF, STATE_TURNING_ON, STATE_ON, STATE_TURNING_OFF * in {@link android.bluetooth.BluetoothAdapter} class. * Requires android.Manifest.permission.BLUETOOTH permission. */ public int getBluetoothState() { if (!isAvailable()){ return BluetoothAdapter.STATE_OFF; } return mBluetoothAdapter.getState(); }
BluetoothAdapter myDevice = BluetoothAdapter.getDefaultAdapter(); if(myDevice.getState() == BluetoothAdapter.STATE_ON){ myDevice.setName("new name"); }
private void restartBLEAdapter(Context context){ final BluetoothManager btManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter btAdapter = btManager.getAdapter(); if (btAdapter.isEnabled()){ Log.i(CLASS_NAME,"Restarting BLEAdapter"); btAdapter.disable(); while(btAdapter.getState() != BluetoothAdapter.STATE_OFF); btAdapter.enable(); } else{ btAdapter.enable(); } while(btAdapter.getState() != BluetoothAdapter.STATE_ON); Log.i(CLASS_NAME,"BLEAdapter (Re)started"); }
public class BootTimeServiceStarter extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); if (bluetooth.getState() == BluetoothAdapter.STATE_ON || bluetooth.getState() == BluetoothAdapter.STATE_TURNING_ON) { bluetooth.disable(); } WifiManager wifiManager = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); if (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED || wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) { wifiManager.setWifiEnabled(false); } } }
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (!action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { return; } BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); int state = adapter.getState(); if (state == BluetoothAdapter.STATE_ON) { NeatleLogger.i("BluetoothAdapter turned on"); resume(); } else { NeatleLogger.i("BluetoothAdapter state changed to " + state +", turning off"); pause(); } } }
private void pause() { handler.removeCallbacks(pauseCallback); handler.removeCallbacks(resumeCallback); if (!scanning) { NeatleLogger.i("called pause, but there is no scanning in progress"); return; } BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); if (doStarted) { doStarted = false; onStop(adapter); } if (scanInterval > 0 && adapter.getState() == BluetoothAdapter.STATE_ON) { NeatleLogger.i("scanning paused, will resume in " + scanInterval + " milliseconds"); handler.postDelayed(resumeCallback, scanInterval); } else { NeatleLogger.i("no scan interval set or bluetooth off, stopping scanning"); } }
/** * Logs the state of the local Bluetooth adapter. */ protected void logBluetoothAdapterInfo(BluetoothAdapter localAdapter) { Log.d(TAG, "BluetoothAdapter: " + "enabled=" + localAdapter.isEnabled() + ", " + "state=" + stateToString(localAdapter.getState()) + ", " + "name=" + localAdapter.getName() + ", " + "address=" + localAdapter.getAddress()); // Log the set of BluetoothDevice objects that are bonded (paired) to the local adapter. Set<BluetoothDevice> pairedDevices = localAdapter.getBondedDevices(); if (!pairedDevices.isEmpty()) { Log.d(TAG, "paired devices:"); for (BluetoothDevice device : pairedDevices) { Log.d(TAG, " name=" + device.getName() + ", address=" + device.getAddress()); } } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setAmbientEnabled(); setContentView(R.layout.activity_bt_state); registerReceiver( bluetoothStateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); checkState(BluetoothAdapter.getDefaultAdapter().getState()); }
@Override @SuppressWarnings({"SpellCheckingInspection", "deprecation"}) protected void onStart(BluetoothAdapter adapter, int scanMode) { boolean ret; UUID uuids[] = scannerConfiguration.getServiceUUIDs(); if (uuids.length > 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { ret = adapter.startLeScan(uuids, callback); } else { ret = adapter.startLeScan(callback); } if (ret) { NeatleLogger.d("Bluetooth LE scan started."); } else { NeatleLogger.i("Bluetooth LE scan failed to start. State = " + adapter.getState()); } }