java - 是否可以从使用意图过滤器和广播的类中创建多个实例?
问题描述
我正在尝试扩展用于连接单个 BLE 设备的解决方案。我这样做是通过创建 2 个类的多个实例来实现的,这些类大量使用广播来耦合它们的执行顺序。
我有 3 个名为Device
、Sequence
和的类Bluetooth
。
Sequence
并且Bluetooth
是使用广播和过滤意图的人。对于每个 BLE 设备,将从 、 和 中创建 3 个Device
新Sequence
实例Bluetooth
。
我在 中看到奇怪的行为,Sequence
所以Bluetooth
我想确认是否需要为每个创建的实例使用唯一的意图过滤器Sequence
和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);
}
}
我在下面的类中运行我的操作序列:
public class BandT1sSequence implements UiThreadCallback {
private final static String TAG = BandT1sSequence.class.getSimpleName();
private static final String ACTION_GATT_CONNECTED =
"com.world.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";
private LocationManager locationManager;
private String mExtraFilter;
private Boolean closeCompletedFlag = false;
private Boolean clearQueuesFlag = false;
/Constructor
public BandT1sSequence(Context context){
if (context!=null && mContext ==null)
mContext = context;
Log.d(TAG, "BandT1sSequence constructor");
//AE - Malaysia
mCommandManager = CommandManager.getInstance(mContext);
locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
intThread();
mExtraFilter = Integer.toString(System.identityHashCode(mHandlerThread));
//System.identityHashCode(mBluetoothLeClass)
//Create bluetooth class
mBluetoothLeClass = new BluetoothBle(context,mExtraFilter);
//confirm the object identity
dataHelper = DataHelper.getsInstance(context);
//Register broadcast receivers
if (mBluetoothLeClass != null)
mContext.registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
}
private IntentFilter makeGattUpdateIntentFilter() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_GATT_CONNECTED);
intentFilter.addAction(ACTION_GATT_DISCONNECTED);
intentFilter.addAction(ACTION_GATT_SERVICES_DISCOVERED);
intentFilter.addAction(ACTION_DATA_AVAILABLE);
intentFilter.addAction(CLASS_CONSTRUCTED);
return intentFilter;
}
public void connectToDevice (){
if (mBluetoothLeClassConstructed && mBluetoothLeClass!=null && bandConnectionFlag==false ) {
mDeviceConnectedFlag= mBluetoothLeClass.connect(mDeviceAddress);
int obj_id = System.identityHashCode(mBluetoothLeClass);
Log.d(TAG, "BandT1sSequence connect to device mBluetoothLeClass_obj_id:" + obj_id +" - "+ getBandDetails());
//device1ConnectionStarted=true;
}else
Log.d(TAG, "connectToDevice: can't connect " + mBluetoothLeClassConstructed);
}
private BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
bluetoothIntentAction = intent.getAction();
if (ACTION_GATT_CONNECTED.equals(bluetoothIntentAction)) {
if (!mSequence.settingModeFlag){
sendRunnableToHandlerThread(syncRunnable);
}
Log.d(TAG, "onConnectionStateChange: threads count " + java.lang.Thread.activeCount() +" - current thread " + Thread.currentThread().getId());
Log.d(TAG, "onReceive: BluetoothLeService.ACTION_GATT_CONNECTED");
} else if (ACTION_GATT_DISCONNECTED.equals(bluetoothIntentAction)) {
Log.d(TAG, "onReceive: BluetoothLeService.ACTION_GATT_DISCONNECTED B: " + mDeviceName +"-"+mDeviceAddress );
mBluetoothLeClass.close();
bandConnectionFlag = false;
} else if (ACTION_GATT_SERVICES_DISCOVERED.equals(bluetoothIntentAction)) {
}
}
};
}
解决方案
推荐阅读
- assembly - C64 Assembly - 循环通过位图作为我的游戏的图形介绍
- excel - VBA for Excel,按下回车键时,在特定方向上移动列中的一组单元格
- go-gorm - 如何在 go-gorm 连接表中设置 CreatedAt 和 UpdatedAt?
- python - 根据具有不同形状的另一个 Dataframe 在 Dataframe 中插入列
- docker - 无法在主机模式下连接到 docker 容器(redis)
- node.js - 在 npm install webdriverio ./node_modules/.bin/wdio 之后:没有这样的文件或目录
- firebase - NoSuchMethodError:在 null 上调用了方法“[]”。(扑)
- css - 我可以使用文档内 SVG 来屏蔽 div 吗?
- .htaccess - Htaccess - 如果设置了 cookie
- angular - 无法加载 TestbedHarnessEnvironment 或如何单击 MatRadioButton