2016-05-11 12 views
0

Wenn ich von meinem Homescreen auf eine andere Aktivität umschalte, wird mein BLE-Gerät getrennt. Hier ist meine Haupttätigkeit:Mein BLE-Gerät wird getrennt, wenn ich die Aktivität aktiviere

So habe ich ein Popup-Menü, das mich zu einer anderen Aktivität umleitet. Wenn ich darauf klicke, bekomme ich ein Protokoll von Bluetooth Gatt, dass es geschlossen ist. Ich habe auch meine API untersucht und kein Problem gefunden. Hier ist meine API für den Fall, dass es hilft:

public class FlipApi { 

Intent iBluetoothLeService; 
static BluetoothDevice mBluetoothDevice; 
BluetoothAdapter mBluetoothAdapter; 
private ProgressDialog mProgressDialog; 
float x_p=0,x_e=60,P_p=1,P_e=1,Q=0.01f,RM=0.5f,K=0; 

Context activitycontext ; 
private OnDataReceivedListener onDataReceivedListener; 
private OnStateChangedListener onStateChangedListener; 

public static final int STATE_CONNECTED = 0x45; 
public static final int STATE_DISCONNECTED = 0x46; 

boolean isBond = false; 

public FlipApi(Context context) 
{ 
    this.activitycontext = context; 
} 
ScanCallback mLeScanCallback; 
public void initApi() 
{ 
    mProgressDialog = new ProgressDialog(activitycontext); 
    mProgressDialog.setCanceledOnTouchOutside(false); 
    mProgressDialog.setMessage("finding device"); 

    IntentFilter intentFilter= new IntentFilter(); 
    intentFilter.addAction("com.flip.data"); 
    activitycontext.registerReceiver(bluetoothDataReceiver,intentFilter); 


    final BluetoothManager bluetoothManager = 
      (BluetoothManager) activitycontext.getSystemService(Context.BLUETOOTH_SERVICE); 

    mBluetoothAdapter = bluetoothManager.getAdapter(); 

    if(mBluetoothAdapter==null) { 
     Toast.makeText(activitycontext, "Bluetooth not found", Toast.LENGTH_LONG).show(); 
    } 
    else if(!mBluetoothAdapter.isEnabled()){ 
     Intent enableBluetooth= new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     activitycontext.startActivity(enableBluetooth); 
    } 
    if(Build.VERSION.SDK_INT >=21) { 
     mLeScanCallback = 
       new ScanCallback() { 
        @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
        @Override 
        public void onScanResult(int callbackType, ScanResult result) { 
         super.onScanResult(callbackType, result); 


         try { 
          if (result.getDevice().getName().contains("FLIP9") || result.getDevice().getName().contains("FLIP8")) { 

           mProgressDialog.dismiss(); 

           mBluetoothDevice = result.getDevice(); 

           iBluetoothLeService = new Intent(activitycontext, BluetoothLeService.class); 

           isBond = activitycontext.bindService(iBluetoothLeService, mServiceConnection, Context.BIND_AUTO_CREATE); 


           Log.d("flip", "device found"); 

           Toast.makeText(activitycontext, result.getDevice().getName() + " found", Toast.LENGTH_SHORT).show(); 

           mBluetoothAdapter.getBluetoothLeScanner().stopScan(mLeScanCallback); 
          } 
         } catch (Exception ex) { 
          ex.printStackTrace(); 
         } 
        } 

        @Override 
        public void onScanFailed(int errorCode) { 
         super.onScanFailed(errorCode); 

        } 

       }; 
    } 
} 

public void startScan() 
{ 
    if(mBluetoothAdapter.isEnabled()) { 
     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      if(mLeScanCallback!=null) { 
       mBluetoothAdapter.getBluetoothLeScanner().startScan(mLeScanCallback); 
       mProgressDialog.show(); 
      } 
     } 
     else 
     { 
      // mBluetoothAdapter.startLeScan(mLeScanCallbackFucksSamsung); 
      if(mBluetoothDevice == null) { 
       mBluetoothAdapter.startLeScan(mLeScanCallbackFucksSamsung); 
       mProgressDialog.show(); 
      } 
      else 
      { 
       iBluetoothLeService = new Intent(activitycontext,BluetoothLeService.class); 
       isBond = activitycontext.bindService(iBluetoothLeService, mServiceConnection, Context.BIND_AUTO_CREATE); 
      } 
     } 

    } 
    else 
    { 
     Toast.makeText(activitycontext, "Please switch on bluetooth", Toast.LENGTH_LONG).show(); 
    } 
} 

public void setOnDataReceivedListener(OnDataReceivedListener onDataREceivedListener) 
{ 
    this.onDataReceivedListener = onDataREceivedListener; 
} 

public void setOnStateChangeListener(OnStateChangedListener onStateChangeListener) 
{ 
    this.onStateChangedListener = onStateChangeListener; 
} 

private final BroadcastReceiver bluetoothDataReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     int id = intent.getIntExtra("id",-1); 
     final int value = intent.getIntExtra("value",-1); 
     Log.d("flip","id = "+id +" value = "+value); 
     switch(id) 
     { 
      case 0: 
       x_p = x_e; 
       P_p = P_e + Q; 

       K = P_p/(P_p+RM); 
       x_e = x_p + K*(value - x_p); 
       P_e = (1-K)*P_p; 
       int k_value = (int) x_e; 
       if(onDataReceivedListener!=null) 
       onDataReceivedListener.onHeartRateReceived(k_value); 

       break; 
      case 1: 
       if(onDataReceivedListener!=null) 
       onDataReceivedListener.onStepsReceived(value); 
       break; 
      case 2: 
       if(onStateChangedListener!=null) 
       onStateChangedListener.onStateChanged(STATE_CONNECTED); 
       Toast.makeText(activitycontext,"Device Connected",Toast.LENGTH_SHORT).show(); 
       break; 
      case 3: 
       if(onStateChangedListener!=null) 
       onStateChangedListener.onStateChanged(STATE_DISCONNECTED); 
       Toast.makeText(activitycontext,"Device Disconnected",Toast.LENGTH_SHORT).show(); 
       iBluetoothLeService = null; 
       activitycontext.unbindService(mServiceConnection); 
       break; 
      case 4: 
       if(onDataReceivedListener!=null) 
       onDataReceivedListener.onBatteryReceived(value); 
       break; 
     } 
    } 
}; 

private BluetoothAdapter.LeScanCallback mLeScanCallbackFucksSamsung= new BluetoothAdapter.LeScanCallback(){ 
    @Override 
    public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) 
    { 

     try { 
      if (device.getName().contains("FLIP9") || device.getName().contains("FLIP8")){ 

       mProgressDialog.dismiss(); 

       mBluetoothDevice = device; 

       iBluetoothLeService = new Intent(activitycontext,BluetoothLeService.class); 

       isBond = activitycontext.bindService(iBluetoothLeService, mServiceConnection, Context.BIND_AUTO_CREATE); 



       Log.d("flip", "device found"); 

       mBluetoothAdapter.stopLeScan(mLeScanCallbackFucksSamsung); 
      } 
     } 
     catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 
}; 



private final ServiceConnection mServiceConnection = new ServiceConnection() { 

    BluetoothLeService mBluetoothLeService; 
    @Override 
    public void onServiceConnected(ComponentName componentName, IBinder service) { 
     mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService(); 
     mBluetoothLeService.initialize(); 
     mBluetoothLeService.connect(mBluetoothDevice.getAddress()); 

    } 

    @Override 
    public void onServiceDisconnected(ComponentName componentName) { 
     mBluetoothLeService = null; 
    } 
}; 

public void disconnect() 
{ 
    if(mServiceConnection!=null && isBond) 
    { 
     activitycontext.unbindService(mServiceConnection); 

    } 

    activitycontext.unregisterReceiver(bluetoothDataReceiver); 
} 

Antwort

0

Dies ist, weil Sie von Ihrem API trennen, wenn die Aktivität zerstört wird.

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    flipApi.disconnect(); 
    Log.d("On destroy",""); 
} 

Wenn Sie Ihren api möchten über alle Aktivitäten arbeiten könnten Sie wahrscheinlich an die Anwendungsklasse bewegen

siehe Why extend an Application class?

+0

ich dort ein Protokoll setzen, es ist nicht aktiv. Was nur bedeuten könnte, dass mein ondestroy() NICHT aufgerufen wird. –