如何在proximity = far的時候 對HC-05傳送訊息


#1

我目前有寫了藍芽的連線程式+上原本的偵測Beacon的程式!
但是可以宣告我的藍芽物件,但卻不能建立我的藍芽物件( this)會變紅字.
因為這個Activity 沒有 OnClick所以我也不知道要怎麼建立…

package com.aprilbrother.aprilbeacondemo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.aprilbrother.aprilbrothersdk.Beacon;

import tw.com.flag.api.FlagBt;

public class BeaconAdapter extends BaseAdapter {
FlagBt bt;

private ArrayList<Beacon> beacons;
private LayoutInflater inflater;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////


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

public BeaconAdapter(Context context) {
	bt = new FlagBt(this);      // 建立藍牙物件
	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("MAC : " + beacon.getMacAddress() + "("
			+ beacon.getDistance() + "m)");
	holder.uuidTextView.setText("UUID: " + beacon.getProximityUUID());
	holder.majorTextView.setText("Major: " + beacon.getMajor());
	holder.minorTextView.setText("Minor: " + beacon.getMinor());
	holder.measuredPowerTextView.setText("MPower: "
			+ beacon.getMeasuredPower());
	holder.rssiTextView.setText("RSSI: " + beacon.getRssi());

	switch (beacon.getProximity()) {
	case 0:
		holder.proximityView.setText("proximity: " + "unknow");
		break;
	case 1:
		holder.proximityView.setText("proximity: " + "immediate");
		break;
	case 2:
		holder.proximityView.setText("proximity: " + "near");
		break;
	case 3:
		holder.proximityView.setText("proximity: " + "far");
		bt.write("f");
		break;
	default:
		break;
	}

}
//

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 uuidTextView;
	final TextView majorTextView;
	final TextView minorTextView;
	final TextView measuredPowerTextView;
	final TextView rssiTextView;
	final TextView proximityView;

	ViewHolder(View view) {
		macTextView = (TextView) view.findViewWithTag("mac");
		uuidTextView = (TextView) view.findViewWithTag("uuid");
		majorTextView = (TextView) view.findViewWithTag("major");
		minorTextView = (TextView) view.findViewWithTag("minor");
		measuredPowerTextView = (TextView) view.findViewWithTag("mpower");
		rssiTextView = (TextView) view.findViewWithTag("rssi");
		proximityView = (TextView) view.findViewWithTag("proximity");
	}
}

}


#2

你可以尝试在BeaconList中添加个条目点击事件 点击条目时进行你的连接操作