Android BLE Bluetooth uses one

Posted by bladecatcher on Mon, 30 Mar 2020 23:01:32 +0200

Android ble Bluetooth development

I. whether BLE Bluetooth is supported

 private boolean isSupportBluetooth(boolean istoast) {
        if (!this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            if (istoast) {
                Toast.makeText(this, "Your device does not support Bluetooth", Toast.LENGTH_SHORT).show();
            }
            return false;
            // ((MainActivity) context).finish();

        }
        final BluetoothManager bluetoothManager =
                (BluetoothManager) this.getSystemService(Context.BLUETOOTH_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            mBluetoothAdapter = bluetoothManager.getAdapter();
        }
        // Checks if Bluetooth is supported on the device.
        if (mBluetoothAdapter == null) {
            if (istoast) {
                Toast.makeText(this, "Your device does not support Ble Bluetooth", Toast.LENGTH_SHORT).show();
            }
            return false;
        }

        return true;
        }

Second, if it is supported, we register the broadcast receiver to receive the Bluetooth data we need, and then the specific Bluetooth operation is encapsulated in the mybluetooth service to bind the service. Finally, if you want to determine whether Bluetooth is enabled, if you want to enable Bluetooth scanning, we need to apply for permission before scanning. Here, we use the EasyPermissions framework to request permission. Finally, we need to enable scanning

  /**
     * Initializing Bluetooth device includes opening Bluetooth and other operations
     */
    private void initBluetooth() {
        supportBluetooth = isSupportBluetooth(true);
        if (supportBluetooth) {
            //Register broadcast receiver
            registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
            Intent gattServiceIntent = new Intent(this, MyBluetoothLeService.class);
            bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
            if (mBluetoothAdapter.isEnabled()) {
                if (EasyPermissions.hasPermissions(this, bluePermission)) {
                    startBluetoothSearch();
                } else {
                    EasyPermissions.requestPermissions(this, "", Constant.REQUESTBLUETOOTHKEY, bluePermission);
                }
            } else {
                initRequestOpenBluetooth();
            }
        }
    }

 /**
     * Request to turn on Bluetooth device
     */
    private void initRequestOpenBluetooth() {
        Intent mIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(mIntent, Constant.REQUESTTOOPENBLUE);
     // Use the enable() method to enable without asking the user (to enable Bluetooth devices without any voice), and then you need to use the android.permission.bluetooth  admin permission.
     // mBluetoothAdapter.enable();
    }
  /**
     * To enable Bluetooth scanning, you need to stop Bluetooth scanning by delaying
     */
    private void startBluetoothSearch() {
        mBluetoothAdapter.startLeScan(this); // Scan the Bluetooth devices of a specific service
        handler.sendEmptyMessageDelayed(0, delayMillis);//To turn off our Bluetooth scanning
         //The following methods are recommended for future optimization
       /* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mBluetoothAdapter.getBluetoothLeScanner().startScan(new ScanCallback() {
                @Override
                public void onScanResult(int callbackType, ScanResult result) {
                    super.onScanResult(callbackType, result);
                }

                @Override
                public void onBatchScanResults(List<ScanResult> results) {
                    super.onBatchScanResults(results);
                }

                @Override
                public void onScanFailed(int errorCode) {
                    super.onScanFailed(errorCode);
                }
            });
        }*/
        }
Today, I'd like to share whether BLE Bluetooth is supported, whether Bluetooth is enabled, and whether Bluetooth scanning is enabled. In this case, we recommend using the methods noted above for the previous version of Bluetooth scanning. Don't forget the application and declaration of permission BL Bluetooth needs to locate permission.

Topics: Android