Android 6.0.1 API 23 - Beacon Library无法正常工作

时间:2017-01-15 10:16:02

标签: android api bluetooth bluetooth-lowenergy altbeacon

我遇到了AltBeacon Android Beacon Library的问题,我已经设置了所有权限(BLUETOOTH, BLUETOOTH_ADMIN, ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION),但由于某些原因我的应用程序无法检测到任何信标(尝试了几个不同的信号)。我正在使用三星Galaxy Tab与android 6.0.1。 当我运行我的应用程序时,我收到以下消息:

I/BeaconActivity: I have just switched from seeing/not seeing beacons: 0
D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 48 - 0, 0) vi=Rect(0, 48 - 0,     0) or=1
D/libGLESv1: STS_GLApi : DTS is not allowed for Package : com.example.tgesior.beacons
D/BluetoothAdapter: STATE_ON
D/BluetoothAdapter: STATE_ON
D/BluetoothLeScanner: Start Scan
D/BluetoothAdapter: STATE_ON
D/BluetoothAdapter: STATE_ON
D/BluetoothAdapter: STATE_ON
D/BluetoothAdapter: STATE_ON
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@8c2357 time:32794199
D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=7
D/ScanRecord: parseFromBytes
D/ScanRecord: first manudata for manu ID
D/ScanRecord: parseFromBytes
D/ScanRecord: first manudata for manu ID
D/ScanRecord: parseFromBytes
D/ScanRecord: first manudata for manu ID
D/ScanRecord: parseFromBytes
D/ScanRecord: first manudata for manu ID
D/ScanRecord: parseFromBytes
D/ScanRecord: first manudata for manu ID
D/ScanRecord: parseFromBytes
D/ScanRecord: first manudata for manu ID
D/ScanRecord: parseFromBytes
D/ScanRecord: first manudata for manu ID
D/ScanRecord: parseFromBytes
D/ScanRecord: first manudata for manu ID
D/ScanRecord: parseFromBytes
D/ScanRecord: first manudata for manu ID
D/ScanRecord: parseFromBytes
D/ScanRecord: first manudata for manu ID
D/ScanRecord: parseFromBytes
D/ScanRecord: first manudata for manu ID
D/ScanRecord: parseFromBytes
D/ScanRecord: first manudata for manu ID
D/ScanRecord: parseFromBytes
D/ScanRecord: first manudata for manu ID
D/ScanRecord: parseFromBytes
D/ScanRecord: first manudata for manu ID
D/ScanRecord: parseFromBytes
D/ScanRecord: first manudata for manu ID
D/BluetoothAdapter: STATE_ON
D/BluetoothAdapter: STATE_ON
D/BluetoothLeScanner: Stop Scan
D/BluetoothLeScanner: Start Scan
D/BluetoothAdapter: STATE_ON
D/BluetoothAdapter: STATE_ON
D/BluetoothAdapter: STATE_ON
D/BluetoothAdapter: STATE_ON
D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=7

I/BeaconActivity: I have just switched from seeing/not seeing beacons: 0 D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 48 - 0, 0) vi=Rect(0, 48 - 0, 0) or=1 D/libGLESv1: STS_GLApi : DTS is not allowed for Package : com.example.tgesior.beacons D/BluetoothAdapter: STATE_ON D/BluetoothAdapter: STATE_ON D/BluetoothLeScanner: Start Scan D/BluetoothAdapter: STATE_ON D/BluetoothAdapter: STATE_ON D/BluetoothAdapter: STATE_ON D/BluetoothAdapter: STATE_ON I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@8c2357 time:32794199 D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=7 D/ScanRecord: parseFromBytes D/ScanRecord: first manudata for manu ID D/ScanRecord: parseFromBytes D/ScanRecord: first manudata for manu ID D/ScanRecord: parseFromBytes D/ScanRecord: first manudata for manu ID D/ScanRecord: parseFromBytes D/ScanRecord: first manudata for manu ID D/ScanRecord: parseFromBytes D/ScanRecord: first manudata for manu ID D/ScanRecord: parseFromBytes D/ScanRecord: first manudata for manu ID D/ScanRecord: parseFromBytes D/ScanRecord: first manudata for manu ID D/ScanRecord: parseFromBytes D/ScanRecord: first manudata for manu ID D/ScanRecord: parseFromBytes D/ScanRecord: first manudata for manu ID D/ScanRecord: parseFromBytes D/ScanRecord: first manudata for manu ID D/ScanRecord: parseFromBytes D/ScanRecord: first manudata for manu ID D/ScanRecord: parseFromBytes D/ScanRecord: first manudata for manu ID D/ScanRecord: parseFromBytes D/ScanRecord: first manudata for manu ID D/ScanRecord: parseFromBytes D/ScanRecord: first manudata for manu ID D/ScanRecord: parseFromBytes D/ScanRecord: first manudata for manu ID D/BluetoothAdapter: STATE_ON D/BluetoothAdapter: STATE_ON D/BluetoothLeScanner: Stop Scan D/BluetoothLeScanner: Start Scan D/BluetoothAdapter: STATE_ON D/BluetoothAdapter: STATE_ON D/BluetoothAdapter: STATE_ON D/BluetoothAdapter: STATE_ON D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=7 等等,这是我的代码:

private BeaconManager beaconManager =null;

private static final String ALTBEACON_LAYOUT = "m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25";

private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;


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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("This app needs location access");
            builder.setMessage("Please grant location access so this app can detect beacons.");
            builder.setPositiveButton(android.R.string.ok, null);
            builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
                }
            });
            builder.show();
        }
    }

    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(ALTBEACON_LAYOUT));
    beaconManager.setForegroundScanPeriod(30000l);
    beaconManager.setForegroundBetweenScanPeriod(21000l);

    beaconManager.bind(this);

}

public void onRequestPermissionsResult(int requestCode,
                                       String permissions[],
                                       int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_COARSE_LOCATION: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.d(TAG, "coarse location permission granted");
            } else {
                final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Functionality limited");
                builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons when in the background.");
                builder.setPositiveButton(android.R.string.ok, null);
                builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                    }
                });
                builder.show();
            }
            return;
        }
    }
}

@Override
public void onBeaconServiceConnect() {
    beaconManager.addMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(Region region) {
            Log.i(TAG, "I just saw an beacon for the first time!");
        }

        @Override
        public void didExitRegion(Region region) {
            Log.i(TAG, "I no longer see an beacon");
        }

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+state);
        }
    });

    try {
        beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,看起来在这里我们可以找到解决方案:https://github.com/AltBeacon/android-beacon-library/issues/412 它声明应用程序需要配置为设置中的设置 - >位置(高精度或省电模式)