BLE扫描失败

时间:2018-01-10 10:19:53

标签: android bluetooth-lowenergy

我正在使用MI note 4(Android 7.0)和Moto x play(Android 7.1.1)

我在sperate服务中进行BLE扫描。 在扫描时我得到扫描响应为“扫描失败” 打开/关闭蓝牙不会影响扫描响应。 打开/关闭Wifi也不会影响扫描响应。

(但在这种情况下,内置的android(来自设置 - >蓝牙)蓝牙扫描工作正常。)

我也使用了BLE扫描仪应用程序,但该应用程序也未检测到BLE广告!

我尝试使用此方式打开/关闭飞行模式,我的设备能够无误扫描。

扫描功能:

mLeScanner.startScan(filters, scanSettings, mScanCallback);

ScanCallback:

ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
               Log.e("TAG","onScanResult");
        }

        @Override
        public void onScanFailed(int errorCode) {
            super.onScanFailed(errorCode);
            Log.e("TAG","onScanFailed");
            }
        }

ScanSettings:

   scanSettings = new ScanSettings.Builder()
                .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
                .build();

过滤器:

List<ScanFilter> filters = new ArrayList<>();
ScanFilter filter = new ScanFilter.Builder().setDeviceAddress("device address").build();
filters.add(filter);

信标扫描过滤器

ScanFilter.Builder builder = new ScanFilter.Builder();
      builder.setManufacturerData(0x004c, new byte[]{}); 

任何人都知道为什么它只适用于切换飞机模式?

网络会影响BLE扫描吗?

2 个答案:

答案 0 :(得分:0)

错误代码0x02表示SCAN_FAILED_APPLICATION_REGISTRATION_FAILED(由于无法注册app,无法启动扫描)。这意味着,在进行扫描之前,我们需要初始化蓝牙适配器

 /**
     * Initialize BluetoothAdapter
     * Check the device has the hardware feature BLE
     * Then enable the hardware,
     */
    public boolean init(Context context) {
        BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();

        return mBluetoothAdapter != null && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
    }

然后注册接收器

**
     * Register GATT update receiver
     */
    private void registerServiceReceiver() {
        this.registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
        registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    }

服务初始化方法也包括在答案中。服务创建是可选的。

  /**
     * Initialize Bluetooth service.
     */
    public void initBLEService(Context context) {
        try {
            this.mContext = context;

            if (mBLEService == null) {
                Intent gattServiceIntent = new Intent(mContext, BLEService.class);

                if (this.mContext != null) {
                    isBind = mContext.bindService(gattServiceIntent, mServiceConnection, mContext.BIND_AUTO_CREATE);
                }

            }

        } catch (Exception e) {
            AppLog.logError(TAG, e.getMessage());
        }
    }

我希望您已经在下面的清单中添加了权限

<uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
 <uses-feature
        android:name="android.hardware.bluetooth_le"
        android:required="true" />

我希望这会对你有所帮助。

答案 1 :(得分:0)

授予用户权限ACCESS_COARSE_LOCATION后,我的问题已解决。您可以在onStart()

中请求用户权限

Java语法

if (ContextCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
        != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(
        this,
        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
        PERMISSIONS_REQUEST_LOCATION);
}

Kotlin语法

if (ContextCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
        != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(
        this,
        arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
        PERMISSIONS_REQUEST_LOCATION)
}