|
|
|
Android - รบกวนช่วยดูโค๊ตชุดนี้หน่อยครับ คือผมต้องการที่จะเปลี่ยน flow มันนิดหน่อย |
|
|
|
|
|
|
|
อันนี้คือ file list นะครับ
Code (Android-Java)
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Region;
import com.estimote.sdk.utils.L;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Displays list of found beacons sorted by RSSI.
* Starts new activity with selected beacon if activity was provided.
*
* @author [email protected] (Wiktor Gworek)
*/
public class ListBeaconsActivity extends Activity {
private static final String TAG = ListBeaconsActivity.class.getSimpleName();
public static final String EXTRAS_TARGET_ACTIVITY = "extrasTargetActivity";
public static final String EXTRAS_BEACON = "extrasBeacon";
private static final int REQUEST_ENABLE_BT = 1234;
private static final String ESTIMOTE_BEACON_PROXIMITY_UUID = "xxxxx";
private static final String ESTIMOTE_IOS_PROXIMITY_UUID = "xxxxxxxx";
private static final Region ALL_ESTIMOTE_BEACONS_REGION = new Region("rid", null, null, null);
private BeaconManager beaconManager;
private LeDeviceListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getActionBar().setDisplayHomeAsUpEnabled(true);
// Configure device list.
adapter = new LeDeviceListAdapter(this);
ListView list = (ListView) findViewById(R.id.device_list);
list.setAdapter(adapter);
list.setOnItemClickListener(createOnItemClickListener());
// Configure verbose debug logging.
L.enableDebugLogging(true);
// Configure BeaconManager.
beaconManager = new BeaconManager(this);
beaconManager.setRangingListener(new BeaconManager.RangingListener() {
@Override
public void onBeaconsDiscovered(Region region, final List<Beacon> beacons) {
// Note that results are not delivered on UI thread.
runOnUiThread(new Runnable() {
@Override
public void run() {
// Note that beacons reported here are already sorted by estimated
// distance between device and beacon.
List<Beacon> estimoteBeacons = filterBeacons(beacons);
getActionBar().setSubtitle("Found beacons: " + estimoteBeacons.size());
adapter.replaceWith(estimoteBeacons);
}
});
}
});
}
private List<Beacon> filterBeacons(List<Beacon> beacons) {
List<Beacon> filteredBeacons = new ArrayList<Beacon>(beacons.size());
for (Beacon beacon : beacons) {
if (beacon.getProximityUUID().equalsIgnoreCase(ESTIMOTE_BEACON_PROXIMITY_UUID)
|| beacon.getProximityUUID().equalsIgnoreCase(ESTIMOTE_IOS_PROXIMITY_UUID)) {
filteredBeacons.add(beacon);
}
}
return filteredBeacons;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.scan_menu, menu);
MenuItem refreshItem = menu.findItem(R.id.refresh);
refreshItem.setActionView(R.layout.actionbar_indeterminate_progress);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy() {
beaconManager.disconnect();
super.onDestroy();
}
@Override
protected void onStart() {
super.onStart();
// Check if device supports Bluetooth Low Energy.
if (!beaconManager.hasBluetooth()) {
Toast.makeText(this, "Device does not have Bluetooth Low Energy", Toast.LENGTH_LONG).show();
return;
}
// If Bluetooth is not enabled, let user enable it.
if (!beaconManager.isBluetoothEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} else {
connectToService();
}
}
@Override
protected void onStop() {
try {
beaconManager.stopRanging(ALL_ESTIMOTE_BEACONS_REGION);
} catch (RemoteException e) {
Log.d(TAG, "Error while stopping ranging", e);
}
super.onStop();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_ENABLE_BT) {
if (resultCode == Activity.RESULT_OK) {
connectToService();
} else {
Toast.makeText(this, "Bluetooth not enabled", Toast.LENGTH_LONG).show();
getActionBar().setSubtitle("Bluetooth not enabled");
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private void connectToService() {
getActionBar().setSubtitle("Scanning...");
adapter.replaceWith(Collections.<Beacon>emptyList());
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
try {
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS_REGION);
} catch (RemoteException e) {
Toast.makeText(ListBeaconsActivity.this, "Cannot start ranging, something terrible happened",
Toast.LENGTH_LONG).show();
Log.e(TAG, "Cannot start ranging", e);
}
}
});
}
private AdapterView.OnItemClickListener createOnItemClickListener() {
return new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (getIntent().getStringExtra(EXTRAS_TARGET_ACTIVITY) != null) {
try {
Class<?> clazz = Class.forName(getIntent().getStringExtra(EXTRAS_TARGET_ACTIVITY));
Intent intent = new Intent(ListBeaconsActivity.this, clazz);
intent.putExtra(EXTRAS_BEACON, adapter.getItem(position));
startActivity(intent);
} catch (ClassNotFoundException e) {
Log.e(TAG, "Finding class by name failed", e);
}
}
}
};
}
}
ฟังชั่นที่รับค่าจากไฟ ลิส มาประมาณผลแล้วคืนค่ากลับไปครับ
Code (Android-Java)
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.estimote.sdk.Beacon;
import com.estimote.sdk.Utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
/**
* Displays basic information about beacon.
*
* @author [email protected] (Wiktor Gworek)
*/
public class LeDeviceListAdapter extends BaseAdapter {
private ArrayList<Beacon> beacons;
private LayoutInflater inflater;
public LeDeviceListAdapter(Context context) {
this.inflater = LayoutInflater.from(context);
this.beacons = new ArrayList<Beacon>();
}
public void replaceWith(Collection<Beacon> newBeacons) {
this.beacons.clear();
this.beacons.addAll(newBeacons);
notifyDataSetChanged();
}
@Override
public int getCount() {
return beacons.size();
}
@Override
public Beacon getItem(int position) {
return beacons.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
view = inflateIfRequired(view, position, parent);
bind(getItem(position), view);
return view;
}
private void bind(Beacon beacon, View view) {
ViewHolder holder = (ViewHolder) view.getTag();
holder.macTextView.setText(String.format("MAC: %s (%.2fm)", beacon.getMacAddress(), Utils.computeAccuracy(beacon)));
holder.majorTextView.setText("Major: " + beacon.getMajor());
holder.minorTextView.setText("Minor: " + beacon.getMinor());
holder.measuredPowerTextView.setText("MPower: " + beacon.getMeasuredPower());
holder.rssiTextView.setText("RSSI: " + beacon.getRssi());
}
private View inflateIfRequired(View view, int position, ViewGroup parent) {
if (view == null) {
view = inflater.inflate(R.layout.device_item, null);
view.setTag(new ViewHolder(view));
}
return view;
}
static class ViewHolder {
final TextView macTextView;
final TextView majorTextView;
final TextView minorTextView;
final TextView measuredPowerTextView;
final TextView rssiTextView;
ViewHolder(View view) {
macTextView = (TextView) view.findViewWithTag("mac");
majorTextView = (TextView) view.findViewWithTag("major");
minorTextView = (TextView) view.findViewWithTag("minor");
measuredPowerTextView = (TextView) view.findViewWithTag("mpower");
rssiTextView = (TextView) view.findViewWithTag("rssi");
}
}
}
คือผมอยากจะเปลี่ยน flow จาก การที่คลิ๊กที่แล้วแล้วให้โชรายละเอียดเป็นการ get ค่าแรกของ array มาเพื่อน้ำไปคำนวณต่ออะครับ
Tag : Mobile
|
|
|
|
|
|
Date :
2014-02-12 15:20:45 |
By :
kaisiamza |
View :
1662 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไม่ลองเขียนดูก่อนล่ะครับ
|
|
|
|
|
Date :
2014-02-13 08:58:57 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|