我的代码有问题:
private ScanCallback mLeScanCallback = new ScanCallback(){
//Callback when a BLE advertisement has been found.
@Override
public void onScanResult(int callbackType, final android.bluetooth.le.ScanResult result) {
super.onScanResult(callbackType, result);
new Thread(){
@Override
public void run() {
final BluetoothDevice device = result.getDevice();
runOnUiThread(new Runnable() {
@Override
public void run() {
if (device != null){
mDevices.add(device);
}
}
});
}
}.start();
}
//Callback when batch results are delivered.
@Override
public void onBatchScanResults(List<android.bluetooth.le.ScanResult> results) {
super.onBatchScanResults(results);
}
//Callback when scan could not be started.
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
}
目前我正在使用此代码来获取扫描结果。这基于:https://github.com/RedBearLab/Android/blob/master/Examples/Chat/src/com/redbear/chat/Main.java#L138
基于的应用程序具有较低的API级别,而我的应用程序具有较高的API级别。所以我把它改成了startScan()方法。
老实说,我被困住了,因为当我运行应用程序时,我没有错误。我检查了mDevices数组是否为空,它是否为空。这意味着代码没有将设备添加到阵列中,或者我的应用程序没有专门找到任何设备。
非常感谢任何帮助。
激活扫描代码
private void scanDevice(){
new Thread() {
@Override
public void run(){
BTScanner.startScan(mLeScanCallback);
try {
Thread.sleep(SCAN_PERIOD);
} catch (InterruptedException e){
e.printStackTrace();
}
BTScanner.stopScan(mLeScanCallback);
}
}.start();
}
答案 0 :(得分:0)
发现问题。它与ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION的权限有关,以获取扫描结果。