微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否可以从使用Intent过滤器和广播的类中创建多个实例?

如何解决是否可以从使用Intent过滤器和广播的类中创建多个实例?

我正在尝试扩展用于连接到单个BLE设备的解决方案。为此,我创建了2个类的多个实例,这些实例大量使用广播来耦合其执行顺序。

我有3个名为DeviceSequenceBluetooth的类。

SequenceBluetooth是使用广播和过滤器意图的。对于每个BLE设备,将从DeviceSequenceBluetooth创建3个新实例。

我在SequenceBluetooth中看到奇怪的行为,所以我想确认是否需要为Sequence和{{1}中创建的每个实例使用唯一的意图过滤器}?

构造类时是否可以动态更改意图过滤器?

我非常感谢您的帮助

Bluetooth

我在下面的类中运行我的操作序列:

public class Bluetooth {
   private final static String TAG = BluetoothBle.class.getSimpleName();
   private Context mContext;// I am using the Applicationcontext
   private Boolean mdisconnectScheduledFlag = false;//in case we issue disconnect while timer is working

   //Run send data in thread
   private HandlerThread mHandlerThread;
   private Handler mHandler;
   private  int mIntervalBetweenTwoPackag =100;
   private Boolean mdisconnectedFlag = false;

   private BluetoothManager mBluetoothManager;
   private BluetoothAdapter mBluetoothAdapter;
   private String mBluetoothDeviceAddress;
   private BluetoothGatt mBluetoothGatt;
   private int mConnectionState = STATE_disCONNECTED;

   private static final int STATE_disCONNECTED = 0;
   private static final int STATE_CONNECTING = 1;
   private static final int STATE_CONNECTED = 2;

//Intent Filters - Do i need to change . create them dynamically for every created instance of the class
   private static final  String ACTION_GATT_CONNECTED =
           "com.hworld.bluetooth.le.ACTION_GATT_CONNECTED";
   private static final  String ACTION_GATT_disCONNECTED =
           "com.world.bluetooth.le.ACTION_GATT_disCONNECTED";
   private static final  String ACTION_GATT_SERVICES_disCOVERED =
           "com.world.bluetooth.le.ACTION_GATT_SERVICES_disCOVERED";
   private static final  String ACTION_DATA_AVAILABLE =
           "com.world.bluetooth.le.ACTION_DATA_AVAILABLE";
   private static final  String EXTRA_DATA =
           "com.world.bluetooth.le.EXTRA_DATA";
   private static final  String CLASS_CONSTRUCTED =
           "com.world.bluetooth.le.CLASS_CONSTRUCTED";

   LocalbroadcastManager localbroadcastManager;

   private int ble_status = FREE;
   private int packet_counter = 0;
   private int send_data_pointer = 0;
   private byte[] send_data = null;
   private boolean first_packet = false;
   private boolean final_packet = false;
   private boolean packet_send = false;
   private Timer mTimer;
   private int time_out_counter = 0;
   private int TIMER_INTERVAL = 100;
   private int TIME_OUT_LIMIT = 100;
   private ArrayList<byte[]> data_queue = new ArrayList<>();
   boolean sendingStoredData = false;

   // Implements callback methods for GATT events that the app cares about.  For example,// connection change and services discovered.
   private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
       @Override
       public void onConnectionStateChange(BluetoothGatt gatt,int status,int newState) {
           String intentAction;
           if (newState == BluetoothProfile.STATE_CONNECTED) {
               Log.d(TAG,"CONNECTED");
               intentAction = ACTION_GATT_CONNECTED;
               mConnectionState = STATE_CONNECTED;
               broadcastUpdate(intentAction);

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

               int obj_id = System.identityHashCode(mGattCallback);
               Log.d(TAG,"onConnectionStateChange mGattCallback_obj_id:" + obj_id +" - A:"+ mBluetoothDeviceAddress);

               mdisconnectedFlag = false;

           } else if (newState == BluetoothProfile.STATE_disCONNECTED) {
               Log.d(TAG,"disCONNECTED");
               intentAction = ACTION_GATT_disCONNECTED;
               mConnectionState = STATE_disCONNECTED;
               broadcastUpdate(intentAction);
               /*App.mConnected = false;
               close(true);*/
               mdisconnectedFlag = true;
           }
       } //End of function onConnectionStateChange
      
   private SendDataToBleReceiver sendDataToBleReceiver;

   private void broadcastUpdate(final String action) {
       final Intent intent = new Intent(action);
       mContext.sendbroadcast(intent);
   }

 public BluetoothBle(Context context,String extraFilter){
   if (mContext == null && context != null)
       mContext = context;

/*    //Add unique number to filter
   if (!TextUtils.isEmpty(extraFilter)){
       ACTION_GATT_CONNECTED =
               "com.world.bluetooth.le.ACTION_GATT_CONNECTED." + extraFilter;
       ACTION_GATT_disCONNECTED =
               "com.world.bluetooth.le.ACTION_GATT_disCONNECTED." + extraFilter;
       ACTION_GATT_SERVICES_disCOVERED =
               "com.world.bluetooth.le.ACTION_GATT_SERVICES_disCOVERED";
       ACTION_DATA_AVAILABLE =
               "com.world.bluetooth.le.ACTION_DATA_AVAILABLE";
       EXTRA_DATA =
               "com.world.bluetooth.le.EXTRA_DATA";
       CLASS_CONSTRUCTED =
               "com.world.bluetooth.le.CLASS_CONSTRUCTED";
   }*/

   localbroadcastManager= LocalbroadcastManager.getInstance(context);

   sendDataToBleReceiver = new SendDataToBleReceiver();
   IntentFilter intentFilter = new IntentFilter();
   intentFilter.addAction(BleConstans.ACTION_SEND_DATA_TO_BLE);
  
   LocalbroadcastManager.getInstance(context)
.registerReceiver(sendDataToBleReceiver,intentFilter);
   broadcastUpdate(CLASS_CONSTRUCTED);
 }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。