android app 扫描 BLE 设备

1. 初始化

BluetoothManager bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter btAdapter = bluetoothManager.getAdapter();
BluetoothLeScanner btLeScanner = bleAdapter.getBluetoothLeScanner();
2. 创建扫描回调
ScanCallback btLeScanCallback = new ScanCallback() {

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

			for (ScanResult result : results) {
				// result.getDevice() 得到设备
			}
		}

		@Override
		public void onScanResult(int callbackType, ScanResult result) {
			super.onScanResult(callbackType, result);

			// result.getDevice(); 得到设备
		}

		@Override
		public void onScanFailed(int errorCode) {
			super.onScanFailed(errorCode);
		}
	};

3. 开始扫描

btLeScanner.startScan(btLeScanCallback);

4. 停止扫描

btLeScanner.stopScan(bluetoothLeScanCallback);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章