80,490
社区成员
发帖
与我相关
我的任务
分享
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;
adapter.stopLeScan(mLeScanCallback);
invalidateOptionsMenu();
}
}, 10000);
mScanning = true;
adapter.startLeScan(mLeScanCallback); //mScanning = false;
//adapter.stopLeScan(mLeScanCallback);
//System.err.println("*************停止BLE扫描*************");
for(BluetoothDevice btd : bluetoothDevicesSet){
HashMap<String, String> mapName = new HashMap<String, String>();
mapName.put("ItemTitle", btd.getName()+"/address:"+btd.getAddress());
System.err.println("*************ItemTitle*************"+btd.getName());
mylist.add(mapName);
bluetoothDevicesList.add(btd);
simpleAdapter = new SimpleAdapter(getApplicationContext(), mylist, R.layout.bt_device_list, new String[] {"ItemTitle"}, new int[] {R.id.device_name});
//加载SimpleAdapter到ListView中
listview_newDevices.setAdapter(simpleAdapter);
}} else {
mScanning = false;
adapter.stopLeScan(mLeScanCallback);
}
invalidateOptionsMenu();
}
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
System.err.println("onLeScan:"+device.getAddress()+"*******"+device.getName()+"*******");
Toast.makeText(ControlActivity.this, "onLeScan:"+device.getAddress()+"*******"+device.getName()+"*******", Toast.LENGTH_SHORT).show();
bluetoothDevicesSet.add(device);
}
});
}
};