我是Android应用开发的初学者。我已经尝试过阅读文档但却无处可去(Android {&#39}教程中的函数已被弃用,等等......)
是否有一个返回蓝牙设备列表的简单功能?
类似于StartLeScan()
- > (设备清单)?
谢谢
答案 0 :(得分:14)
基本上取决于你所针对的Android版本。因为api在棒棒糖中有所改变(21)。
在您的活动中,获取蓝牙适配器
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"/>
答案 1 :(得分:2)
如果您使用的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);
答案 2 :(得分:0)
我也认为这个问题持续了几个小时
我通过搜索官方文件找到了答案
你需要知道3类和方法ScanCallback,BluetoothLeScanner,ScanResult
//我的等级很低,我无法发布链接....
您可以在Android开发人员网站上看到,该网页会在右侧显示在API级别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'
}
}
}
这些代码适用于我的手机,即API 21 希望对你有所帮助。
答案 3 :(得分:0)
我一直在玩BLE扫描一段时间,直到我从北欧图书馆得到最简单的解决方案。
在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 { ...});
图书馆完成所有工作。