2016-03-01 259 views
0

我正在開發一個應用程序,我必須連接到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(); 
+0

什麼是不工作?你沒有得到任何掃描結果,或設備列表適配器沒有顯示正確的東西?請發佈列表適配器的代碼。 – curob

+0

我的應用可以顯示信標的名稱和Mac地址,但不是rssi,我敢肯定,我的代碼中缺少某些東西,這就是爲什麼我希望以前有人做過這個操作,請指導我 – geniusme

+0

顯示您的適配器代碼請。 – curob

回答

1

你的問題是:(1)該適配器本身並不具備跟蹤RSSI值和(2)您的適配器列表項視圖沒有辦法顯示RSSI值,並將其自身確實適配器的方法沒有跟蹤RSSI值的方法。下面是詳細問題:

問題1列表適配器不能跟蹤RSSI值。

您要添加的設備和RSSI值到適配器與此呼籲:

mLeDeviceListAdapter.addDevice(device,rssi); 

但適配器只有這樣的方法:

public void addDevice(BluetoothDevice device) { 
    if (!mLeDevices.contains(device)) { 
     mLeDevices.add(device); 

    } 
} 

正如你所看到的方法做不匹配。我建議執行以下操作: 首先創建一個新類,它將同時保存BluetoothDevice和RSSI值。事情是這樣的:

private class LeScanRecord { 
    public final BluetoothDevice device; 
    public final int rssi; 

    public LeScanRecord(BluetoothDevice device, int rssi) { 
     this.device = device; 
     this.rssi = rssi; 
    } 
} 

然後改變你的適配器來接受新類的跟蹤實例(注意,這在文本字段RSSI值填充處理問題2):

/** 
* Adapter for the scan record devices 
*/ 
private class LeDeviceListAdapter extends ArrayAdapter<LeScanRecord > 
{ 
    /** 
    * View inflater 
    */ 
    private final LayoutInflater inflater; 

    public LeDeviceListAdapter() 
    { 
     super(MainActivity.this, R.layout.listitem_device); 
     inflater = MainActivity.this.getLayoutInflater(); 
    } 
    @Override 
    public void add(LeScanRecord object) 
    { 
     for (int i = 0; i < getCount(); i++) 
     { 
      if (getItem(i).device.equals(object.device)) 
      { 
       return; // do not add duplicates 
      } 
     } 
     super.add(object); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     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.rssi= (TextView) view.findViewById(R.id.device_signal); 
      view.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) view.getTag(); 
     } 

     LeScanRecord record = getItem(position); 
     final String deviceName = record.device.getName(); 
     if (deviceName != null && deviceName.length() > 0){ 
      viewHolder.deviceName.setText(deviceName); 

     } 
     else { 
      viewHolder.deviceName.setText(R.string.unknown_device); 
     } 
     viewHolder.deviceAddress.setText(record.device.getAddress()); 
     viewHolder.rssi.setText(Integer.valueOf(record.rssi)); 


     return view; 
    } 
} 

static class ViewHolder { 
    public TextView deviceName; 
    public TextView deviceAddress; 
    public TextView rssi; 
} 

最後要做的是將掃描記錄的實例添加到適配器。所以改變:

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

     } 
    }); 
} 

要:

@Override 
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) { 
    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      mLeDeviceListAdapter.add(new LeScanRecord(device, rssi)); 
      mLeDeviceListAdapter.notifyDataSetChanged(); 

     } 
    }); 
} 
+0

設備的陣列列表如何? – geniusme

+0

BaseAdapter類維護自己的數組;不需要添加你自己的。 – curob

+0

我猜你忘了在addDevice方法的參數中添加rssi,但是當我這樣做時,它向我展示了在其中使用的「contains」和「add」中的錯誤,我認爲是因爲Rssi是整數而不是BluetoothDevice類型這就是爲什麼它會拒絕它,你是如何成功運行應用程序的?肯定缺少的東西 – geniusme