2016-08-03 136 views
0

我目前正在使用BLE將我的android設備與另一個藍牙設備連接。Android BLE startLeScan掃描已連接的設備

問題是,如何掃描已連接的設備?

在我的第一種方法中,我沒有在連接方法之前調用stopLeScan。

這對上述問題沒有任何問題,但它導致ui中斷(太短的ui更新間隔),有時連接時間非常慢。

在連接之前,我讓我的應用程序調用stopLeDevice後,每個問題都已解決,但出現了一個新問題。新問題是我無法在scanResult上看到連接的設備。它只顯示斷開的設備。我仍然想監視我的連接設備。我該如何實現這個目標?

回答

0

使用此類自動啓動新的BLE設備。

BLEScanner服務

public class BLEScanner extends Service { 
    private BluetoothAdapter mBluetoothAdapter; 
    private ArrayList<BluetoothDevice> mLeDevices; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     mLeDevices = new ArrayList<BluetoothDevice>(); 
     if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { 
      BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 
      mBluetoothAdapter = bluetoothManager.getAdapter(); 
     } 
     startLeScan(); 
     return Service.START_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    private void startLeScan() { 
     scanLeDevice(true); 
    } 

    private void stopLeScan() { 
     scanLeDevice(false); 
    } 

    private void scanLeDevice(boolean enable) { 
     if (enable) { 
      mBluetoothAdapter.startLeScan(mLeScanCallback); 
     } else { 
      mBluetoothAdapter.stopLeScan(mLeScanCallback); 
     } 
    } 

    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { 

     @Override 
     public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { 
      if (!mLeDevices.contains(device)) { 
       mLeDevices.add(device); 
       connectToDevice(device); 
      } 
     } 
    }; 

    private void connectToDevice(final BluetoothDevice device) { 
     if (device != null) { 
      Log.i("Tag", "Name: " + device.getAddress() + " Connecting"); 
      if (device.getName() != null) 
       device.connectGatt(this.getApplicationContext(), false, new BluetoothCallBack(this.getApplicationContext(), BLEScanner.this)); 
     } 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     stopLeScan(); 
     mLeDevices.clear(); 
    } 

    public void removeDevice(BluetoothDevice mDevice) { 
     try { 
      if (mLeDevices != null && mLeDevices.size() > 0) 
       mLeDevices.remove(mDevice); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

現在回調類檢查BLE設備連接或不

public class BluetoothCallBack extends BluetoothGattCallback { 
    private BLEScanner mServiceObject; 

    public BluetoothCallBack(Context mContext, BLEScanner mServiceObject) { 
     this.mServiceObject = mServiceObject; 
    } 

    @Override 
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 
     if (newState == BluetoothProfile.STATE_CONNECTED) { 
      Log.i("Tag", "CONNECTED DEVICE: " + gatt.getDevice().getAddress()); 
      gatt.discoverServices(); 
     } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { 
      Log.e("Tag", "DISCONNECTED DEVICE: " + gatt.getDevice().getAddress()); 
      gatt.disconnect(); 
      gatt.close(); 
      mServiceObject.removeDevice(gatt.getDevice()); 
     } 
    } 
}