2016-04-26 2 views
0

Как сохранить список обнаруженных устройств BLE в Android?Сохраните список доступных устройств Bluetooth bluetooth?

Следуй за мной реализацию startDeviceScan() метод:

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; 
} 

Это обратный вызовустройство сканирования:

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

Другой вопрос. Как подождать завершения сканирования?

+0

Вы можете список устройств от mNearestDevices и сохранить его. –

ответ

0

Вы должны изменить способ onLeScan так:

@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++; 
} 
Смежные вопросы