2017-07-02 19 views
-1

私は、Bluetooth Leデバイスの起動時にユーザがBluetooth Leデバイスの状態を知ることができ、接続状態が次のように更新されるアプリケーションを開発しようとしています。私は、BleサービスからBroadcastを送信し、それをonResume of Homeフラグメントにキャッチし、その後ステータステキストビューでそれを更新することによって、それを実装しようとしました。ステータスは更新されますが、フラグメントを変更してホームフラグメントに戻った場合、Bluetooth Leデバイスが接続されているにもかかわらず、ステータスを表示するためのテキストビューが空白になります。この問題を解決して、デバイスが接続されていて切断されている場合は切断されていますか?Bluetooth Leデバイスの接続/切断状態を常に表示する方法

どのような指導も高く評価されます。ここ

は、私はあなたのBleServiceにBleservice.java HomeFragment.java

public class HomeFragment extends Fragment 
{ 
    private BroadcastReceiver mReceiver; 
@Override 
protected void onResume() { 
    super.onResume(); 
mReceiver = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      final String action = intent.getAction(); 

      if (Bleservice.ACTION_GATT_CONNECTED.equals(action)) { 
       mConnected = true; 
       updateConnectionState(R.string.connected); 


      } else if (Bleservice.ACTION_GATT_DISCONNECTED.equals(action)) { 
       mConnected = false; 
       updateConnectionState(R.string.disconnected); 


      } 
     } 


    }; 
    getActivity().registerReceiver(mReceiver,makeGattUpdateIntentFilter()); 

} 

private void updateConnectionState(final int resourceId) { 
    getActivity().runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 

      tv_connected_disconnected.setText(resourceId); 

     } 
    }); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    getActivity().unregisterReceiver(mReceiver); 
} 

private static IntentFilter makeGattUpdateIntentFilter() { 
    final IntentFilter intentFilter = new IntentFilter(); 
    intentFilter.addAction(Bleservice.ACTION_GATT_CONNECTED); 
    intentFilter.addAction(Bleservice.ACTION_GATT_DISCONNECTED); 
    return intentFilter; 

} 

答えて

0

接続で

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { 
    @Override 
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 
     String intentAction; 

     if (newState == BluetoothProfile.STATE_CONNECTED) { 
      intentAction = ACTION_GATT_CONNECTED; 
      mConnectionState = STATE_CONNECTED; 


      broadcastUpdate(intentAction); 
      Log.i(TAG, "Connected to GATT server."); 

      // Attempts to discover services after successful connection. 
      Log.i(TAG, "Attempting to start service discovery:" + 
        mBluetoothGatt.discoverServices()); 

     } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { 
      intentAction = ACTION_GATT_DISCONNECTED; 
      mConnectionState = STATE_DISCONNECTED; 
      Log.i(TAG, "Disconnected from GATT server."); 
      broadcastUpdate(intentAction); 
     } 
    } 

private void broadcastUpdate(final String action) { 
    final Intent intent = new Intent(action); 
    sendBroadcast(intent); 
} 

では上記

を実装するために使用しているコードセグメントでありますバインドサービスによって。アクティビティとサービスの例を確認してください。

private BleService mBluetoothLeService; 
private boolean isConnected; 

@Override 
protected void onStart() { 
    super.onStart(); 
    Intent bindIntent = new Intent(this, BleService.class); 
    startService(bindIntent); 
    bindService(bindIntent, mServiceConnection, Context.BIND_AUTO_CREATE); 
    registerServiceReceiver(); //register here your mGattCallback that get actions from BleService 
} 

@Override 
protected void onStop() { 
    super.onStop(); 
    unbindService(mServiceConnection); 
    mBluetoothLeService = null; 
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mGattUpdateReceiver); 
} 

メソッドbindServiceでは、Service Lifecycleを管理するServiceConnectionを渡す必要があります。

// Code to manage Service lifecycle. 
private final ServiceConnection mServiceConnection = new ServiceConnection() { 

    @Override 
    public void onServiceConnected(ComponentName componentName, IBinder service) { 

     mBluetoothLeService = ((BleService.LocalBinder) service).getService(); 
     isConnected = (mBluetoothLeService.getConnectionState() != BleService.STATE_CONNECTED) 

    } 

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

サービスでは、バインダーを宣言する必要があります。

private final IBinder mBinder = new LocalBinder(); 

@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return mBinder; 
} 
public class LocalBinder extends Binder { 
    public BleService getService() { 
     return BleService.this; 
    } 
} 


public int getConnectionState() { 
    return mConnectionState; 
} 

これで、BleServiceにバインドした後、接続状態を取得できます。

関連する問題