在 Android 中检测蓝牙 Le 设备
detect bluetooth Le devices in Android
我是 android 应用程序开发的初学者。我已经尝试阅读文档,但一无所获(Android 教程中的函数,例如 StartLeScan()
已被弃用,等等...)
是否有一个简单的函数 returns 蓝牙设备列表?
类似于 getDevices()
->(设备列表)?
谢谢
基本上这取决于您的目标 android 版本。因为 api 在 lollipop (21) 中发生了一些变化。
在您的 activity 中,获取蓝牙适配器
BluetoothManager bm = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE)
BluetoothAdapter mBluetoothAdapter = bm.getAdapter();
// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
那么您应该检查您的目标 android 版本
int apiVersion = android.os.Build.VERSION.SDK_INT;
if (apiVersion > android.os.Build.VERSION_CODES.KITKAT){
BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner();
// scan for devices
scanner.startScan(new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
// get the discovered device as you wish
// this will trigger each time a new device is found
BluetoothDevice device = result.getDevice();
}
});
} else {
// targetting kitkat or bellow
mBluetoothAdapter.startLeScan(new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
// get the discovered device as you wish
}
});
// rest of your code that will run **before** callback is triggered since it's asynchronous
不要忘记在清单中添加权限
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
如果您使用的 api 级别低于 21,那么您会发现 StartLeScan 已被弃用,在 android lollipop StartLeScan() 中引入了新的扫描设置功能。您可以使用以下代码扫描 BLE 设备。
ScanSettings.Builder scanSettingsBuilder = new ScanSettings.Builder();
scanSettingsBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER);
scanSettings = scanSettingsBuilder.build();
BluetoothScanCallback mScanCallback = new BluetoothScanCallback();
mBluetoothUtils.getBluetoothAdapter().getBluetoothLeScanner()
.startScan(scanFilters, scanSettings, mScanCallback);
这个问题我也想了几个小时
我通过搜索官方文档找到了答案。
你需要知道 3 class 和方法 ScanCallback,BluetoothLeScanner,ScanResult
//我的水平太低了,我不能post link.....
你可以在android developer web中看到,网页会在右侧显示Added in API level 21"。
这是我的代码,已修改 from these project
//declare
private BluetoothLeScanner mBluetoothLeScanner;
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
//start and stop scan
mBluetoothLeScanner.startScan(mScanCallback);
mBluetoothLeScanner.stopScan(mScanCallback);
//Scan call back function
private ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
mLeDeviceListAdapter.addDevice(result.getDevice());
mLeDeviceListAdapter.notifyDataSetChanged();
}
};
我爷爷:
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.polkapolka.bluetooth.le"
minSdkVersion 21
targetSdkVersion 22
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
这些代码在我的 phone 上运行良好,即 API 21
希望对你有帮助。
我一直在玩 BLE 扫描,直到我从 Nordic 库中得到最简单的解决方案。
在您的 build.gradle 中添加一行:
dependencies {
....
compile 'no.nordicsemi.android.support.v18:scanner:1.0.0'
}
并使用 BluetoothLeScannerCompat:
import no.nordicsemi.android.support.v18.scanner.BluetoothLeScannerCompat;
...
BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
scaner.startScan(new ScanCallback { ...});
图书馆包办所有工作。
我是 android 应用程序开发的初学者。我已经尝试阅读文档,但一无所获(Android 教程中的函数,例如 StartLeScan()
已被弃用,等等...)
是否有一个简单的函数 returns 蓝牙设备列表?
类似于 getDevices()
->(设备列表)?
谢谢
基本上这取决于您的目标 android 版本。因为 api 在 lollipop (21) 中发生了一些变化。
在您的 activity 中,获取蓝牙适配器
BluetoothManager bm = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE)
BluetoothAdapter mBluetoothAdapter = bm.getAdapter();
// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
那么您应该检查您的目标 android 版本
int apiVersion = android.os.Build.VERSION.SDK_INT;
if (apiVersion > android.os.Build.VERSION_CODES.KITKAT){
BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner();
// scan for devices
scanner.startScan(new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
// get the discovered device as you wish
// this will trigger each time a new device is found
BluetoothDevice device = result.getDevice();
}
});
} else {
// targetting kitkat or bellow
mBluetoothAdapter.startLeScan(new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
// get the discovered device as you wish
}
});
// rest of your code that will run **before** callback is triggered since it's asynchronous
不要忘记在清单中添加权限
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
如果您使用的 api 级别低于 21,那么您会发现 StartLeScan 已被弃用,在 android lollipop StartLeScan() 中引入了新的扫描设置功能。您可以使用以下代码扫描 BLE 设备。
ScanSettings.Builder scanSettingsBuilder = new ScanSettings.Builder();
scanSettingsBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER);
scanSettings = scanSettingsBuilder.build();
BluetoothScanCallback mScanCallback = new BluetoothScanCallback();
mBluetoothUtils.getBluetoothAdapter().getBluetoothLeScanner()
.startScan(scanFilters, scanSettings, mScanCallback);
这个问题我也想了几个小时
我通过搜索官方文档找到了答案。
你需要知道 3 class 和方法 ScanCallback,BluetoothLeScanner,ScanResult
//我的水平太低了,我不能post link.....
你可以在android developer web中看到,网页会在右侧显示Added in API level 21"。
这是我的代码,已修改 from these project
//declare
private BluetoothLeScanner mBluetoothLeScanner;
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
//start and stop scan
mBluetoothLeScanner.startScan(mScanCallback);
mBluetoothLeScanner.stopScan(mScanCallback);
//Scan call back function
private ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
mLeDeviceListAdapter.addDevice(result.getDevice());
mLeDeviceListAdapter.notifyDataSetChanged();
}
};
我爷爷:
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.polkapolka.bluetooth.le"
minSdkVersion 21
targetSdkVersion 22
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
这些代码在我的 phone 上运行良好,即 API 21 希望对你有帮助。
我一直在玩 BLE 扫描,直到我从 Nordic 库中得到最简单的解决方案。
在您的 build.gradle 中添加一行:
dependencies {
....
compile 'no.nordicsemi.android.support.v18:scanner:1.0.0'
}
并使用 BluetoothLeScannerCompat:
import no.nordicsemi.android.support.v18.scanner.BluetoothLeScannerCompat;
...
BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
scaner.startScan(new ScanCallback { ...});
图书馆包办所有工作。