ScanCall回来没有进入Android Oreo

时间:2018-02-27 18:14:12

标签: android bluetooth android-bluetooth android-ble

我无法使我的ble扫描仪工作。没有scanCalll Back正在发生。

以下是我的代码: MainActivity:

public class MainActivity extends AppCompatActivity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BluetoothUtil b = new BluetoothUtil();
        b.startScan();

    }


}

}

BluetoothUtil:

public class BluetoothUtil extends ScanCallback {

BluetoothAdapter badap = BluetoothAdapter.getDefaultAdapter();
ScanSettings.Builder builder = new ScanSettings.Builder();


public BluetoothUtil() {

    super();
    builder.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);
}

@Override
public void onScanResult(int callbackType, ScanResult result) {
    Log.v("test","Scanning Occurred");
    super.onScanResult(callbackType, result);
}

public void startScan() {
 if(badap !=null)
{
    ScanSettings.Builder builder = new ScanSettings.Builder();
    builder.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);
    if (BluetoothAdapter.getDefaultAdapter().getState() == BluetoothAdapter.STATE_ON) {
        BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner().startScan(new ArrayList<ScanFilter>(), builder.build(), this);
//            
BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner().startScan(this);
    }
}
}
}

1 个答案:

答案 0 :(得分:0)

我的错。发布android 6你必须从app gui权限设置应用程序的权限。 你必须启用位置和蓝牙。 现在就工作了!