我正在開發一個應用程序,我必須連接到Android 4.3上的Bluetooth Low Energy設備。 我想這在我的應用程序,以顯示RSSI,但它不工作藍牙低功耗Rssi Android
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
//final LeDeviceListAdapter deviceLe = new LeDeviceListAdapter(device, rssi, scanRecord);
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device,rssi);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
我的名單適配器代碼是:
private class LeDeviceListAdapter extends BaseAdapter {
private ArrayList<BluetoothDevice> mLeDevices;
private LayoutInflater mInflator;
public LeDeviceListAdapter() {
super();
mLeDevices = new ArrayList<>();
mInflator = MainActivity.this.getLayoutInflater();
}
public void addDevice(BluetoothDevice device) {
if (!mLeDevices.contains(device)) {
mLeDevices.add(device);
}
}
public BluetoothDevice getDevice(int position) {
return mLeDevices.get(position);
}
public void clear() {
mLeDevices.clear();
// notifyDataSetChanged();
}
@Override
public int getCount() {
return mLeDevices.size();
}
@Override
public Object getItem(int i) {
return mLeDevices.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
// General ListView optimization code.
if (view == null) {
view = mInflator.inflate(R.layout.listitem_device,viewGroup, false);
viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
// viewHolder.signal = (TextView) view.findViewById(R.id.device_signal);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
BluetoothDevice device = mLeDevices.get(i);
//final String rssi_s=device.getRssi();
final String deviceName = device.getName();
/* ScanResult rssi=null;
final double rssi_s = rssi.getRssi();
String rssiString = String.valueOf(rssi_s);*/
//final String rssiString =getString(valueOf(rssi_s));
if
(deviceName != null && deviceName.length() > 0){
viewHolder.deviceName.setText(deviceName);
// viewHolder.signal.setText(rssiString);
}
else
viewHolder.deviceName.setText(R.string.unknown_device);
viewHolder.deviceAddress.setText(device.getAddress());
return view;
}
}
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
//final LeDeviceListAdapter deviceLe = new LeDeviceListAdapter(device, rssi, scanRecord);
runOnUiThread(new Runnable() {
@Override
public void run() {
// String rssi_signal=String.valueOf(rssi);
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
};
static class ViewHolder {
TextView deviceName;
TextView deviceAddress;
//TextView signal;
}
你有任何想法如何,我可以顯示這個? 我還需要在我的oncreate方法中輸入這個嗎?
mLeDeviceListAdapter=new LeDeviceListAdapter();
什麼是不工作?你沒有得到任何掃描結果,或設備列表適配器沒有顯示正確的東西?請發佈列表適配器的代碼。 – curob
我的應用可以顯示信標的名稱和Mac地址,但不是rssi,我敢肯定,我的代碼中缺少某些東西,這就是爲什麼我希望以前有人做過這個操作,請指導我 – geniusme
顯示您的適配器代碼請。 – curob