本文整理汇总了Java中android.bluetooth.le.ScanCallback类的典型用法代码示例。如果您正苦于以下问题:Java ScanCallback类的具体用法?Java ScanCallback怎么用?Java ScanCallback使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ScanCallback类属于android.bluetooth.le包,在下文中一共展示了ScanCallback类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onCreate
import android.bluetooth.le.ScanCallback; //导入依赖的package包/类
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// initialize the right scan callback for the current API level
if (Build.VERSION.SDK_INT >= 21) {
mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
mRecyclerViewAdapter.addDevice(result.getDevice().getAddress());
}
};
} else {
mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
mRecyclerViewAdapter.addDevice(bluetoothDevice.getAddress());
}
};
}
// initialize bluetooth manager & adapter
BluetoothManager manager = (BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = manager.getAdapter();
}
示例2: errorCodeToBleErrorCode
import android.bluetooth.le.ScanCallback; //导入依赖的package包/类
@BleScanException.Reason private static int errorCodeToBleErrorCode(int errorCode) {
switch (errorCode) {
case ScanCallback.SCAN_FAILED_ALREADY_STARTED:
return BleScanException.SCAN_FAILED_ALREADY_STARTED;
case ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED:
return BleScanException.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED;
case ScanCallback.SCAN_FAILED_FEATURE_UNSUPPORTED:
return BleScanException.SCAN_FAILED_FEATURE_UNSUPPORTED;
case ScanCallback.SCAN_FAILED_INTERNAL_ERROR:
return BleScanException.SCAN_FAILED_INTERNAL_ERROR;
case 5: // ScanCallback.SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES
return BleScanException.SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES;
default:
RxBleLog.w("Encountered unknown scanning error code: %d -> check android.bluetooth.le.ScanCallback");
return BleScanException.UNKNOWN_ERROR_CODE;
}
}
示例3: startDiscoveringBle
import android.bluetooth.le.ScanCallback; //导入依赖的package包/类
private boolean startDiscoveringBle(){
if (D) Log.d(TAG, "+++ startDiscoveringBle() +++");
if (mAdapter.isDiscovering()){
Log.i(TAG, "startDiscoveringBle() : Already in classic discovering mode");
return true;
}
if (isLollipopApi){
Log.i(TAG, "startDiscoveringBle() : Choose startScan()");
mLeScanner = mAdapter.getBluetoothLeScanner();
if (null != mLeScanner){
((BluetoothLeScanner)mLeScanner).startScan((ScanCallback)mScanCallback);
return true;
}
// TODO
// return mAdapter.startScan(mScanCallback); ???
} else {
Log.i(TAG, "startDiscoveringBle() : Choose startLeScan()");
return mAdapter.startLeScan(mLeScanCallback);
}
return true;
}
示例4: onListItemClick
import android.bluetooth.le.ScanCallback; //导入依赖的package包/类
@Override
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@SuppressWarnings("deprecation")
protected void onListItemClick(ListView l, View v, int position, long id) {
final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
if (device == null) return;
final Intent intent = new Intent(this, DeviceControlActivity.class);
intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName());
intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());
if (mScanning) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBluetoothLeScanner.stopScan(new ScanCallback() {});
} else {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
mScanning = false;
}
startActivity(intent);
}
示例5: BluetoothLeScanL21Proxy
import android.bluetooth.le.ScanCallback; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public BluetoothLeScanL21Proxy(BluetoothAdapter bluetoothAdapter) {
mBluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
mLeScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
}
};
}
示例6: cancelDiscoveringBle
import android.bluetooth.le.ScanCallback; //导入依赖的package包/类
private boolean cancelDiscoveringBle(){
if (D) Log.d(TAG, "+++ cancelDiscoveringBle() +++");
if (mAdapter.isDiscovering()){
Log.i(TAG, "cancelDiscoveringBle() : In classic discovering mode");
return false;
}
if (isLollipopApi){
Log.i(TAG, "cancelDiscoveringBle() : Choose stopScan()");
if (null != mLeScanner){
((BluetoothLeScanner)mLeScanner).stopScan((ScanCallback)mScanCallback);
return true;
}
// TODO
// return mAdapter.stopScan(mScanCallback); ???
} else {
Log.i(TAG, "cancelDiscoveringBle() : Choose stopLeScan()");
mAdapter.stopLeScan(mLeScanCallback);
}
return true;
}
示例7: scanFailureToString
import android.bluetooth.le.ScanCallback; //导入依赖的package包/类
/**
* Returns the corresponding constant name for a
* given {@code ScanCallback#SCAN_FAILED_*} value.
*/
public static String scanFailureToString(int scanFailure) {
switch (scanFailure) {
case ScanCallback.SCAN_FAILED_ALREADY_STARTED:
return "SCAN_FAILED_ALREADY_STARTED";
case ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED:
return "SCAN_FAILED_APPLICATION_REGISTRATION_FAILED";
case ScanCallback.SCAN_FAILED_INTERNAL_ERROR:
return "SCAN_FAILED_INTERNAL_ERROR";
case ScanCallback.SCAN_FAILED_FEATURE_UNSUPPORTED:
return "SCAN_FAILED_FEATURE_UNSUPPORTED";
default:
return "UNKNOWN: " + scanFailure;
}
}
示例8: startScan
import android.bluetooth.le.ScanCallback; //导入依赖的package包/类
public static void startScan(ScanCallback callback) {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (null == adapter) {
Log.e(TAG, "BluetoothAdapter is null");
return;
}
BluetoothLeScanner scanner = adapter.getBluetoothLeScanner();
if (null == scanner) {
Log.e(TAG, "BluetoothLeScanner is null");
return;
}
scanner.startScan(callback);
}
示例9: stopScan
import android.bluetooth.le.ScanCallback; //导入依赖的package包/类
public static void stopScan(ScanCallback callback) {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (null == adapter) {
Log.e(TAG, "BluetoothAdapter is null");
return;
}
BluetoothLeScanner scanner = adapter.getBluetoothLeScanner();
if (null == scanner) {
Log.e(TAG, "BluetoothLeScanner is null");
return;
}
scanner.stopScan(callback);
}
示例10: BTLEScannerOld
import android.bluetooth.le.ScanCallback; //导入依赖的package包/类
public BTLEScannerOld(BluetoothAdapter bluetoothAdapter, Callback callback) {
this.bluetoothAdapter = bluetoothAdapter;
this.leScanCallback = callback::onScan;
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
this.leScanner = bluetoothAdapter.getBluetoothLeScanner();
this.scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
callback.onScan(result.getDevice(), result.getRssi(), result.getScanRecord().getBytes());
}
super.onScanResult(callbackType, result);
}
};
} else {
this.leScanner = null;
this.scanCallback = null;
}
}
示例11: startScan
import android.bluetooth.le.ScanCallback; //导入依赖的package包/类
public void startScan() {
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
leScanner.startScan(new android.bluetooth.le.ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
}
});
} else {
bluetoothAdapter.startLeScan(leScanCallback);
}
}
示例12: getScanCallback
import android.bluetooth.le.ScanCallback; //导入依赖的package包/类
@NonNull
private ScanCallback getScanCallback(final byte[] serviceData) {
return new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
processResult(result);
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
for (ScanResult s: results) {
processResult(s);
}
}
@Override
public void onScanFailed(int errorCode) {
Log.d(TAG, "Scan failed: " + String.valueOf(errorCode));
}
public void processResult(ScanResult sr) {
Log.d(TAG, "Found device " + sr.getDevice().getName() + "(" + sr.getDevice().getAddress() + ")");
/* confirm correct connection key */
byte[] sd = sr.getScanRecord().getServiceData(Constants.SERVICE_pUUID);
if (sd != null) {
if (Arrays.equals(sd, serviceData)) {
BTConnection.this.stopScan();
transfer.startServer(sr.getDevice());
} else {
Log.d(TAG, "Incorrect service data: " + new String(sd) + " instead of " + new String(serviceData) + ". Continuing.");
}
} else {
Log.d(TAG, "No service data received -- continuing.");
}
}
};
}
示例13: initCallbacks
import android.bluetooth.le.ScanCallback; //导入依赖的package包/类
private ScanCallback initCallbacks() {
return new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
if (result != null && result.getDevice() != null) {
if (isAdded(result.getDevice())) {
// No add
} else {
saveDevice(result.getDevice());
}
}
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
}
};
}
示例14: bleStopScan
import android.bluetooth.le.ScanCallback; //导入依赖的package包/类
/**
* Stop a BLE scan.
*
* @param callbackId The callbackId corresponding to the {@link
* BluetoothLeScannerSnippet#bleStartScan} call that started the scan.
* @throws BluetoothLeScanSnippetException
*/
@RpcMinSdk(Build.VERSION_CODES.LOLLIPOP_MR1)
@Rpc(description = "Stop a BLE scan.")
public void bleStopScan(String callbackId) throws BluetoothLeScanSnippetException {
ScanCallback callback = mScanCallbacks.remove(callbackId);
if (callback == null) {
throw new BluetoothLeScanSnippetException("No ongoing scan with ID: " + callbackId);
}
mScanner.stopScan(callback);
}
示例15: shutdown
import android.bluetooth.le.ScanCallback; //导入依赖的package包/类
@Override
public void shutdown() {
for (ScanCallback callback : mScanCallbacks.values()) {
mScanner.stopScan(callback);
}
mScanCallbacks.clear();
}