这里我有一个列表视图,显示扫描的ble设备列表。这在ListActivity中工作正常,但我希望在按钮单击时对DialogFragment执行相同的扫描。我在ListActivity中使用的工作代码在DialogFragment中不起作用。当我点击Button触发对话框时,它甚至没有显示Dialogfragment本身。所以我用1500ms的处理程序添加了一个时间。然后它只显示Dialog标题,但不显示扫描设备列表。在Dialogfragment中实现扫描设备列表的任何想法
public class DeviceScanDialogFragment extends DialogFragment
{
private static final String TAG = "DeviceScan";
private LeDeviceListAdapter mLeDeviceListAdapter;
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler;
private static final int REQUEST_ENABLE_BT = 1;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;
private BluetoothDevice selectedDevice;
private Logger logger = LoggerFactory.getLogger(this.getClass());
private String calledFrom;
//persist the value about where to start the application
public static boolean isVerified = false, isPinChanged = false, isFingerPrintRegistered = false;
private SharedPreferences sharedPreferences;
private ListView listview;
public DeviceScanDialogFragment() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler();
// Use this check to determine whether BLE is supported on the device. Then you can
// selectively disable BLE-related features.
if (!getActivity().getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(getActivity(), R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
getActivity().finish();
}
// Initializes a Bluetooth adapter. For API level 18 and above, get a reference to
// BluetoothAdapter through BluetoothManager.
final BluetoothManager bluetoothManager =
(BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
// Checks if Bluetooth is supported on the device.
if (mBluetoothAdapter == null) {
// logger.info("Bluetooth adapter is null");
Toast.makeText(getActivity(), R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
getActivity().finish();
return;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.list_dialog_fragment, container);
listview = (ListView) rootview.findViewById(R.id.list);
Toast.makeText(getActivity(), "Executed", Toast.LENGTH_SHORT).show();
// getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().setTitle("Scan List Title");
return rootview;
}
@Override
public void onResume() {
super.onResume();
// Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled,
// fire an intent to display a dialog asking the user to grant permission to enable it.
if (!mBluetoothAdapter.isEnabled()) {
// logger.info("Enabling bluetooth");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
listview.postDelayed(new Runnable() {
@Override
public void run() {
// Initializes list view adapter.
mLeDeviceListAdapter = new LeDeviceListAdapter();
listview.setAdapter(mLeDeviceListAdapter);
scanLeDevice(true);
}
},1500);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// User chose not to enable Bluetooth.
if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) {
getActivity().finish();
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onPause() {
super.onPause();
scanLeDevice(false);
mLeDeviceListAdapter.clear();
// save(isVerified, isPinChanged, isFingerPrintRegistered);
}
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
if (mBluetoothAdapter != null) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
if (mBluetoothAdapter.getBondedDevices().size() == 0)
Toast.makeText(getActivity(), "No Device Found", Toast.LENGTH_SHORT).show();
if (getActivity() != null) {
if (isVisible())
dismiss();
}
}
// invalidateOptionsMenu();
}
}, SCAN_PERIOD);
mScanning = true;
// logger.info("Scanning without UUIDs");
mBluetoothAdapter.startLeScan(mLeScanCallback);
/*UUID[] uuids = new UUID[1];
Log.e(TAG, "Length of UUID[]= " + uuids.length);
uuids[0] = UUID.fromString(SampleGattAttributes.JACKET_UUID);
logger.info("Scanning with UUIDs");
mBluetoothAdapter.startLeScan(uuids, mLeScanCallback);*/
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
// invalidateOptionsMenu();
}
// Adapter for holding devices found through scanning.
private class LeDeviceListAdapter extends BaseAdapter {
private ArrayList<BluetoothDevice> mLeDevices;
private LayoutInflater mInflater;
public LeDeviceListAdapter() {
super();
mLeDevices = new ArrayList<BluetoothDevice>();
mInflater = getActivity().getLayoutInflater();
}
public void addDevice(BluetoothDevice device) {
if (!mLeDevices.contains(device)) {
mLeDevices.add(device);
}
}
public BluetoothDevice getDevice(int position) {
return mLeDevices.get(position);
}
public void clear() {
mLeDevices.clear();
}
@Override
public int getCount() {
return mLeDevices.size();
}
@Override
public Object getItem(int i) {
return mLeDevices.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
// General ListView optimization code.
if (view == null) {
view = mInflater.inflate(R.layout.listitem_device, null);
viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
BluetoothDevice device = mLeDevices.get(position);
final String deviceName = device.getName();
Log.i(TAG, "name:" + deviceName + ", Address :" + device.getAddress());
if (deviceName != null && deviceName.length() > 0) {
viewHolder.deviceName.setText(deviceName);
if (deviceName.contains("Test")) {
selectedDevice = mLeDeviceListAdapter.getDevice(position);
if (selectedDevice != null) {
Intent intent;
intent = new Intent(getActivity(), NavigationDrawerActivity.class);
intent.putExtra("address", selectedDevice.getAddress());
if (mScanning) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
mScanning = false;
}
startActivity(intent);
}
}
} else {
viewHolder.deviceName.setText(R.string.unknown_device);
}
viewHolder.deviceAddress.setText(device.getAddress());
return view;
}
}
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
};
static class ViewHolder {
TextView deviceName;
TextView deviceAddress;
}
}
按钮单击我正在调用此方法以触发Dialogfragment
public void showScanDialog() {
FragmentManager manager = getSupportFragmentManager();
DeviceScanDialogFragment dialog = new DeviceScanDialogFragment();
dialog.show(manager, "devicescan");
}
答案 0 :(得分:0)
当我关闭wifi时,一切都工作正常,因为BLE在wifi上不会稳定,而在Marshmallow设备中使用BLE将无法显示附近的扫描设备。所以我已经为Marshmallow支持的设备添加了位置访问权限,然后它运行正常。