如何在設定在rssi值<=-50後執行動作


#1

我有宣告一個藍芽物件
我要在RSSI值<=-50後
傳送訊息給Hc-05
請問該如何做到:((((
以下是我的程式碼
/////////////////////////////////////////////////////
package com.aprilbrother.aprilbeacondemo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Message;
import android.os.RemoteException;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.aprilbrother.aprilbrothersdk.Beacon;
import com.aprilbrother.aprilbrothersdk.BeaconManager;
import com.aprilbrother.aprilbrothersdk.BeaconManager.MonitoringListener;
import com.aprilbrother.aprilbrothersdk.BeaconManager.RangingListener;
import com.aprilbrother.aprilbrothersdk.Region;
import com.aprilbrother.aprilbrothersdk.utils.AprilL;

import tw.com.flag.api.FlagBt;
import tw.com.flag.api.OnFlagMsgListener;

/**

  • 搜索展示beacon列表 scan beacon show beacon list
    */
    public class BeaconList extends Activity
    implements OnFlagMsgListener {
    //
    FlagBt bt;
    TextView txv;

    private static final int REQUEST_ENABLE_BT = 1234;
    private static final String TAG = “BeaconList”;
    // private static final Region ALL_BEACONS_REGION = new Region("",
    // “e2c56db5-dffb-48d2-b060-d0f5a71096e3”,
    // null, null);

    private static final Region ALL_BEACONS_REGION = new Region(“customRegionName”, null,
    null, null);
    private static final Region TEST_ALL_BEACONS_REGION = new Region(“customRegionName”,
    null, 2, null);
    // private static final Region ALL_BEACONS_REGION = new Region(“customRegionName”,
    // “e2c56db5-dffb-48d2-b060-d0f5a71096e0”,
    // 985,211);
    // 扫描所有uuid为"aa000000-0000-0000-0000-000000000000"的beacon
    // private static final Region ALL_BEACONS_REGION = new Region(“customRegionName”,
    // “aa000000-0000-0000-0000-000000000000”,
    // null, null);
    private BeaconAdapter adapter;
    private BeaconManager beaconManager;
    private ArrayList myBeacons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();

     this.setRequestedOrientation( // 讓手機螢幕保持直立模式
     		ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
     txv = (TextView)findViewById(R.id.txv);
     bt = new FlagBt(this);      // 建立藍牙物件
    

    }
    /////////////////////////////////////////////////

    /*
    private void onit(Beacon beacon, View view) {
    if(beacon.getProximity()==3)
    bt.write(“f”);
    }
    */
    ///////////////////////////////////////////////

    public void connect(View v) {
    if(!bt.connect()) // 選取已配對裝置進行連線
    txv.setText(“找不到任何已配對裝置”);
    }

    public void quit(View v) {
    bt.stop();
    finish();
    }

    public void goBack(View v) {
    finish(); // 結束 Activity, 即可回到前一個 Activity
    }

    @Override
    public void onFlagMsg(Message msg) {
    switch(msg.what) {
    case FlagBt.CONNECTING: // 嘗試與已配對裝置連線
    txv.setText(“正在連線到:” + bt.getDeviceName());
    break;
    case FlagBt.CONNECTED: // 與已配對裝置連線成功
    txv.setText(“已連線到:” + bt.getDeviceName());
    break;
    case FlagBt.CONNECT_FAIL: // 連線失敗
    txv.setText(“連線失敗!請重連”);
    break;
    case FlagBt.CONNECT_LOST: // 目前連線意外中斷
    txv.setText(“連線中斷!請重連”);
    break;
    }
    ///

    }
    //
    public void gotoControlActivity(View v) {
    Intent it = new Intent(this, Control.class); //建立 Intent 並設定目標 Activity
    startActivity(it); // 啟動 Intent 中的目標 Activity
    }

    public void gotoMonitorActivity(View v) {
    Intent it = new Intent(this, Monitor.class); //建立 Intent 並設定目標 Activity
    startActivity(it); // 啟動 Intent 中的目標 Activity
    }
    ////////////////////////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////////////////////////

    private void init() {

     myBeacons = new ArrayList<Beacon>();
     ListView lv = (ListView) findViewById(R.id.lv);
     adapter = new BeaconAdapter(this);
     lv.setAdapter(adapter);
     AprilL.enableDebugLogging(true);
    
     // 此处上下文需要是Activity或者Serivce
     // 若在FragmentActivit或者其他Activity子类中使用 上下文请使用getApplicationContext
     beaconManager = new BeaconManager(getApplicationContext());
     // beaconManager.setMonitoringExpirationMill(10L);
     // beaconManager.setRangingExpirationMill(10L);
     // beaconManager.setForegroundScanPeriod(2000, 0);
     beaconManager.setRangingListener(new RangingListener() {
    
     	@Override
     	public void onBeaconsDiscovered(Region region,
     									final List<Beacon> beacons) {
     		if (beacons != null && beacons.size() > 0) {
     			Log.i(TAG, "onBeaconsDiscovered");
     			Log.i(TAG, beacons.get(0).getName());
     		}
    
     		myBeacons.clear();
     		myBeacons.addAll(beacons);
     		getActionBar().setSubtitle("Found beacons: " + beacons.size());
     		ComparatorBeaconByRssi com = new ComparatorBeaconByRssi();
     		Collections.sort(myBeacons, com);
     		adapter.replaceWith(myBeacons);
     	}
     });
    
     beaconManager.setMonitoringListener(new MonitoringListener() {
    
     	@Override
     	public void onExitedRegion(Region region) {
     		Toast.makeText(BeaconList.this, "Notify in", 0).show();
    
     	}
    
     	@Override
     	public void onEnteredRegion(Region region, List<Beacon> beacons) {
     		Toast.makeText(BeaconList.this, "Notify out", 0).show();
     	}
     });
    
     lv.setOnItemClickListener(new OnItemClickListener() {
     	@Override
     	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
     							long arg3) {
     		Beacon beacon = myBeacons.get(arg2);
     		Intent intent;
     		/////////////////////////////////
     		if (beacon.getRssi()>=-1){
    
     			bt.write("f");
     		}
     		/////////////////////////////////
     		if (beacon.getName().contains("ABSensor")) {
     			intent = new Intent(BeaconList.this, SensorActivity.class);
     		} else {
     			intent = new Intent(BeaconList.this, ModifyActivity.class);
     		}
     		Bundle bundle = new Bundle();
     		bundle.putParcelable("beacon", beacon);
     		intent.putExtras(bundle);
     		startActivity(intent);
     	}
     });
    

    }
    ////////////////////////////////////////
    public void forward(View v) {
    bt.write(“f”);
    }
    public void stop(View v) {
    bt.write(“s”);
    }
    ///////////////////////////////////////
    /**

    • 连接服务 开始搜索beacon connect service start scan beacons
      */
      private void connectToService() {
      Log.i(TAG, “connectToService”);
      getActionBar().setSubtitle(“Scanning…”);
      adapter.replaceWith(Collections. emptyList());
      beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
      @Override
      public void onServiceReady() {
      try {
      Log.i(TAG, “connectToService”);
      beaconManager.startRanging(ALL_BEACONS_REGION);
      // beaconManager.startMonitoring(ALL_BEACONS_REGION);
      } catch (RemoteException e) {
      e.printStackTrace();
      }
      }
      });
      }

    @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);
    }

    @Override
    protected void onStart() {
    super.onStart();

     if (!beaconManager.hasBluetooth()) {
     	Toast.makeText(this, "Device does not have Bluetooth Low Energy",
     			Toast.LENGTH_LONG).show();
     	return;
     }
     if (!beaconManager.isBluetoothEnabled()) {
     	Intent enableBtIntent = new Intent(
     			BluetoothAdapter.ACTION_REQUEST_ENABLE);
     	startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
     } else {
     	connectToService();
     }
    

    }

    @Override
    protected void onDestroy() {
    super.onDestroy();
    }

    @Override
    protected void onStop() {
    try {
    myBeacons.clear();
    beaconManager.stopRanging(ALL_BEACONS_REGION);
    beaconManager.disconnect();
    } catch (RemoteException e) {
    Log.d(TAG, “Error while stopping ranging”, e);
    }
    super.onStop();
    }
    }


#2

你可以在onBeaconDescover中返回的beacons中遍历通过beacon.getRssi()筛选出你需要找到的rssi<=-50的beacon


#3

可以交我怎麼寫嗎:(
我還是學生 沒有很了解


#4

謝謝老闆 我寫出來了