Android 蓝牙发现设备

Android 蓝牙发现设备,第1张

Android 蓝牙发现设备
public class BluetoothUtil {

    public static final String BLUFI_PREFIX = "MH-";

    public static BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    
    public static boolean hasBlueTooth() {
        return bluetoothAdapter != null;
    }

    
    public static boolean isEnabled() {
        return bluetoothAdapter != null && bluetoothAdapter.isEnabled();
    }

    
    public static void startScanBluetooth(ScanCallback scanCallback) {
        if (isEnabled()) {
            BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner();
            if (scanner == null) {
                LogUtil.e("蓝牙不可用");
                return;
            }
            LogUtil.e("开始扫描");
            scanner.startScan(null,
                    new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER).build(),
                    scanCallback);
        }
    }

    
    public static void stopScanBluetooth(ScanCallback scanCallback) {
        if (isEnabled()) {
            BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner();
            if (scanner != null) {
                scanner.stopScan(scanCallback);
            }
            LogUtil.e("停止扫描");
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5709665.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存