Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问怎么自动配对? #6

Closed
wushangjun opened this issue Jul 11, 2017 · 1 comment
Closed

请问怎么自动配对? #6

wushangjun opened this issue Jul 11, 2017 · 1 comment

Comments

@wushangjun
Copy link

请问怎么自动配对?

@davistsin
Copy link
Owner

davistsin commented Feb 11, 2018

你需要自己去写这套逻辑,或者可以参考下面这段代码:

`
private void scan() {
if (!LocationServiceUtils.isOpenLocService(this) && ApiLevelHelper.isAtLeast(Build.VERSION_CODES.N)) {
Toast.makeText(this, "Android 7.0以上扫描蓝牙,需要地理位置信息。请您开启。", Toast.LENGTH_LONG).show();
LocationServiceUtils.gotoLocServiceSettings(this);
return;
}
if (mBluetoothLe.getScanning()) {
Toast.makeText(this,"正在扫描中...",Toast.LENGTH_SHORT).show();
return;
}
mBluetoothLe.setScanWithDeviceName(DEVICE_NAME)
.setReportDelay(0)
.startScan(this, new OnLeScanListener() {
@OverRide
public void onScanResult(BluetoothDevice bluetoothDevice, int rssi, ScanRecord scanRecord) {
mBluetoothDevice = bluetoothDevice;
stopScan();
connect();
}

                @Override
                public void onBatchScanResults(List<ScanResult> results) {

                }

                @Override
                public void onScanCompleted() {
                    if (mBluetoothDevice == null) {
                        mTvScanBrasBluetoothNotice.setText("没有搜索到设备\n请确定设备开启未被连接");
                        if (animation != null && animation.hasStarted()) {
                            animation.cancel();
                        }
                    }
                }

                @Override
                public void onScanFailed(ScanBleException e) {

                }
            });
    mTvScanBrasBluetoothNotice.setText("正在扫描xxx设备...");
    animation = AnimationUtils.loadAnimation(this, R.anim.anim_progress_scan);
    mIvScanBrasBluetoothProgress.setAnimation(animation);
}

private void stopScan() {
    mBluetoothLe.stopScan();
}

private void connect() {
    mBluetoothLe.startConnect(false, mBluetoothDevice);
}

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants