我试图开发能够检测BLE信号的应用程序,并显示其RSSI值(除了其他参数)。从现在开始,我能够检测BLE,并在的ListView
列出,但我坚持就当它已经检测到 BaseAdapter ,我已为的ListView开发
。
I am trying to develop an app capable of detecting BLE signals and show its RSSI value (apart from other parameters). From now I am capable of detecting BLE and list it in a ListView
but I am stuck on how to pass the parameter RSSI when it has been detected to the BaseAdapter
that I have developed for the ListView
.
这是code口用于检测BLE设备:
This is the code I used to detect the BLE device:
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
addDevice(device);
}
};
这是另一个类的地方是code以上实行的AddDevice code:
Here it is the addDevice code that is implemented in another class where it is the code above:
protected synchronized void addDevice(final BluetoothDevice device) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
和决赛,在这里它是 BaseAdapter
我使用的的ListView
:
And final, here it is the BaseAdapter
I use for the ListView
:
public class LeDeviceListAdapter extends BaseAdapter {
private List<BluetoothDevice> data;
private Activity context;
public LeDeviceListAdapter(Activity context, List<BluetoothDevice> data) {
this.data = data;
this.context = context;
}
public synchronized void addDevice(BluetoothDevice device) {
if(!data.contains(device) ){
data.add(device);
}
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
LayoutInflater mInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.leaf_devices_list_item, null);
convertView.setTag(new DeviceView(convertView));
}
DeviceView view = (DeviceView) convertView.getTag();
view.init((BluetoothDevice) getItem(position), null);
return convertView;
}
public class DeviceView {
private TextView title;
private TextView status;
private TextView type;
private TextView address;
private TextView rssivalue;
public DeviceView(View view) {
title = (TextView) view.findViewById(R.id.device_name);
status = (TextView) view.findViewById(R.id.device_status_txt);
type = (TextView) view.findViewById(R.id.device_type_txt);
address = (TextView) view.findViewById(R.id.device_address_txt);
rssivalue = (TextView) view.findViewById(id.signal_intensity_txt);
}
public void init(BluetoothDevice device, Intent intent) {
title.setText(device.getName());
address.setText(device.getAddress());
setType(device.getType());
setStatus(device.getBondState());
}
}
在rssivalue是的TextView
我要使用显示RSSI。
The "rssivalue" is the TextView
I am going to use to show the RSSI.
我已经从我认为这是有关这一每一类采取了codeS的一部分。如果需要,我会提供code作为此case.Please重要的,有人帮我解决这个问题!
I have taken parts of the codes from each class that I thought it was relevant for this. If it is needed I will provide the code that are important for this case.Please, somebody help me to solve this problem!!!
从添加RSSI值 LeScanCallback
:
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
yourBleAdapter.addDevice(device, rssi);
}
};
在您的适配器,这将成为:
In your adapter, this becomes:
public void addDevice(BluetoothDevice device, int rssi) {
//get device and its rssi locally here
}
,你得到的RSSI值多数民众赞成。它是从样品code,我使用。作品。
thats where you get the rssi value from. It is from the sample code that i use. Works.