2016-04-26 10 views
0

Wie kann ich eine Liste erkennbarer BLE-Geräte in Android speichern?Speichern Sie eine Liste von erkennbaren Bluetooth LE-Geräten?

Folgen Sie meine Implementierung von startDeviceScan() Methode:

private String[][] mNearestDevices; 
// ... 

public String[][] startDeviceScan() { 
    Log.i(TAG, "Start device scan"); 

    mNearestDevices = new String[n][2]; 
    mCounter = 0; 

    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      mScanning = false; 
      mBluetoothAdapter.stopLeScan(mBleScanCallback); 
     } 
    }, SCAN_PERIOD); 

    mScanning = true; 
    mBluetoothAdapter.startLeScan(mBleScanCallback); 

    return mNearestDevices; 
} 

Dies ist die Gerät Scan Rückruf:

private BluetoothAdapter.LeScanCallback mBleScanCallback = new BluetoothAdapter.LeScanCallback() { 
      @Override 
      public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { 
       Runnable r = new Runnable() { 
        @Override 
        public void run() { 
         Log.i(TAG, "Name: " + device.getName() + " (" + device.getAddress() + ")"); 
         mNearestDevices[mCounter][0] = device.getName(); 
         mNearestDevices[mCounter][1] = device.getAddress(); 
        } 
       }; 
       r.run(); 
      } 
     }; 

Eine weitere Frage. Wie kann ich den Abschluss des Scans abwarten?

+0

Sie können Geräteliste speichern. –

Antwort

0

Sie haben die onLeScan Verfahren wie das ändern: von mNearestDevices und es

@Override 
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) 
{ 

    Log.i(TAG, "Name: " + device.getName() + " (" + device.getAddress() + ")"); 
    mNearestDevices[mCounter][0] = device.getName(); 
    mNearestDevices[mCounter][1] = device.getAddress(); 
    mCounter++; 
}