0
如何在Android中保存可發現的BLE設備列表?保存可發現的藍牙LE設備列表?
按照我的實現startDeviceScan()方法:
private String[][] mNearestDevices;
// ...
public String[][] startDeviceScan() {
Log.i(TAG, "Start device scan");
mNearestDevices = new String[n][2];
mCounter = 0;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mBleScanCallback);
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mBleScanCallback);
return mNearestDevices;
}
這是設備掃描回調:
private BluetoothAdapter.LeScanCallback mBleScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
Runnable r = new Runnable() {
@Override
public void run() {
Log.i(TAG, "Name: " + device.getName() + " (" + device.getAddress() + ")");
mNearestDevices[mCounter][0] = device.getName();
mNearestDevices[mCounter][1] = device.getAddress();
}
};
r.run();
}
};
另一個問題。 如何等待掃描完成?
你可以從mNearestDevices設備列表並保存。 –