1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.clj.fastble.scan;
 
 
import android.bluetooth.BluetoothDevice;
import android.text.TextUtils;
 
import com.clj.fastble.data.ScanResult;
 
import java.util.concurrent.atomic.AtomicBoolean;
 
/**
 * scan a known mac device, then connect
 */
public abstract class MacScanCallback extends PeriodScanCallback {
 
    private String mMac;
    private AtomicBoolean hasFound = new AtomicBoolean(false);
 
    public MacScanCallback(String mac, long timeoutMillis) {
        super(timeoutMillis);
        this.mMac = mac;
        if (TextUtils.isEmpty(mac)) {
            onDeviceNotFound();
        }
    }
 
    @Override
    public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
        if (device == null)
            return;
        if (TextUtils.isEmpty(device.getAddress())) {
            return;
        }
 
        if (!hasFound.get()) {
 
            ScanResult scanResult = new ScanResult(device, rssi, scanRecord,
                    System.currentTimeMillis());
 
            if (mMac.equalsIgnoreCase(device.getAddress())) {
                hasFound.set(true);
                bleBluetooth.stopScan(MacScanCallback.this);
                onDeviceFound(scanResult);
            }
        }
    }
 
    @Override
    public void onScanTimeout() {
        onDeviceNotFound();
    }
 
    @Override
    public void onScanCancel() {
 
    }
 
    public abstract void onDeviceFound(ScanResult scanResult);
 
    public abstract void onDeviceNotFound();
}