Bluetooth с низким уровнем энергопотребления - PullRequest
0 голосов
/ 31 октября 2018

Я создаю приложение для сканирования устройств Bluetooth с низким энергопотреблением и реализовал функцию сканирования, однако получаю сообщение об ошибке:

не удалось запустить сканирование - выброшено исключение SecurityException: java.lang.SecurityException: необходимо разрешение ACCESS_COARSE_LOCATION или ACCESS_FINE_LOCATION для получения результатов сканирования

У меня есть необходимые разрешения в моем манифесте, но я все еще получаю эту ошибку. Я провел некоторое исследование и прочитал, что версии SDK> 23 требуют какой-то ручной проверки разрешений. Это правильное решение этой проблемы или есть более простая альтернатива?

  package com.example.jake.bluetooth;

import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_ENABLE_BT = 1;
    private String ble_not_supported = "BLE not supported";
    private BluetoothManager bluetoothManager;
    private BluetoothAdapter mBluetoothAdapter;
    private Button startScanBtn;
    private boolean mScanning;
    private Handler mHandler;
    private BluetoothAdapter.LeScanCallback mLeScanCallBack;
    private static final long SCAN_PERIOD = 10000;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();
        startScanBtn = findViewById(R.id.start_scan);
        mHandler = new Handler();
        mScanning = true;

        mLeScanCallBack = new BluetoothAdapter.LeScanCallback() {
            @Override
            public void onLeScan(final BluetoothDevice bluetoothDevice, int rssi, byte[] bytes) {

            }
        };

        if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }

        if(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)){
            Toast.makeText(this, ble_not_supported, Toast.LENGTH_SHORT).show();
            finish();
        }

        startScanBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                scanLeDevice(mScanning);
            }
        });

    }

    private void scanLeDevice(final boolean enable){
        if(enable){
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mScanning = false;
                    mBluetoothAdapter.stopLeScan(mLeScanCallBack);
                }
            },SCAN_PERIOD);

            mScanning = true;
            mBluetoothAdapter.startLeScan(mLeScanCallBack);
        } else {
            mScanning = false;
            mBluetoothAdapter.stopLeScan(mLeScanCallBack);
        }
    }

}

Ответы [ 3 ]

0 голосов
/ 31 октября 2018

Вы должны удалить предыдущую сборку:

Run > Edit Configurations > Application > Before launch section(on the right) Создайте gradle-aware Make add :app:uninstallAll

0 голосов
/ 01 ноября 2018

Нет, недостаточно просто иметь это в своем манифесте. Вы также должны явно спросить пользователя во время выполнения. См. https://developer.android.com/training/permissions/requesting для получения дополнительной информации.

0 голосов
/ 31 октября 2018

У меня есть приложение для использования BLE, и это мой AndroidManifest.xml файл, все отлично работает.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bluetoothlowenergy_gatt_sendreceive">

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ServiceActivity"></activity>
    </application>

</manifest>
...