安卓BLE命令发送不起作用



我正在努力解决一个奇怪的问题,即命令从安卓设备发送到BLE加密狗。在这里,我有两个按钮按钮1按钮2。单击每个按钮时,我正在尝试发送到BLE加密狗的不同命令。问题详述如下:

  1. 使用两个按钮按钮 1和按钮2启动片段
  2. 点击按钮1
  3. 命令成功发送到BLE加密狗
  4. 然后点击Button2命令不发送到 BLE.
  5. 再次重新运行和片段启动现在我单击了按钮2
  6. 命令发送成功
  7. 他们点击了Button1命令不发送。

*

脑震荡 - 命令仅在第一次单击第一个按钮时发送。

* 现在这是我使用的代码 下面是我从片段的onCreateView()调用的代码,用于初始化服务和广播接收器。

if(SingleTon.getInstance().getDeviceAddress() !=null && SingleTon.getInstance().getDeviceName() != null) {
if(!SingleTon.getInstance().isLEServiceGattInit())
settingObj.deviceControl(getActivity(), SingleTon.getInstance().getDeviceAddress(), SingleTon.getInstance().getDeviceName());
}

现在将获得代码上方调用形式的方法-

public  void deviceControl(Context context,String deviceAddress, String devicename ){
progressServices = new ProgressDialog(context);
progressServices = ProgressDialog.show(context, "",
"Looking for Services....", true);
this.mcontext = context;
this.mDeviceAddress = deviceAddress;
this.mDeviceName = devicename;
initServices();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
if(progressServices.isShowing()) {
progressServices.dismiss();
Toast.makeText(mcontext,"Issue in BLE, not able to advertise Services/UUIDs.",Toast.LENGTH_LONG).show();
}
}
}, 6000);
}
private void initServices(){
Intent gattServiceIntent = new Intent(mcontext, BluetoothLeService.class);
mcontext.bindService(gattServiceIntent, mServiceConnection, getActivity().BIND_AUTO_CREATE);
mcontext.registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
}

private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
if (!mBluetoothLeService.initialize()) {
Log.e(TAG, "Unable to initialize");
}
// Automatically connects to the device upon successful start-up initialization.
mBluetoothLeService.connect(mDeviceAddress);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBluetoothLeService = null;
}
};
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
mConnected = true;
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
mConnected = false;
SingleTon.getInstance().setLEServiceGattInit(false);
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
// Show all the supported services and characteristics on the user interface.
displayGattServices(mBluetoothLeService.getSupportedGattServices());
SingleTon.getInstance().setmBluetoothLeService(mBluetoothLeService);
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
}
}
};

private void displayGattServices(List<BluetoothGattService> gattServices) {
if (gattServices == null) return;
String uuid;
String unknownServiceString = "Unknown Services";
String unknownCharaString = "Unknown Characteristic";
ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<>();
ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData
= new ArrayList<>();
mGattCharacteristics = new ArrayList<>();
// Loops through available GATT Services.
String LIST_NAME = "NAME";
String LIST_UUID = "UUID";
for (BluetoothGattService gattService : gattServices) {
uuid = gattService.getUuid().toString();
if(uuid.contains("ff02")){
List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
// Loops through available Characteristics.
for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
if (gattCharacteristic.getUuid().equals(UUID.fromString("0000c002-0000-1000-8000-00805f9b34fb"))) {
SingleTon.getInstance().setListWriteCharecteristic(gattCharacteristic);
progressServices.dismiss();
SingleTon.getInstance().setLEServiceGattInit(true);
}
}
}
}
}
private static IntentFilter makeGattUpdateIntentFilter() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
return intentFilter;
}

现在下面是我们在每次单击按钮时调用命令发送的代码。

btn_head_up.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
sendHexCommand(CommandListHex.Hex20BIT_ACT_HEAD_UP);
return  true;
}
});
private void sendHexCommand(String cmd){
if(SingleTon.getInstance().getDeviceAddress() !=null && SingleTon.getInstance().getDeviceName() != null) {
settingObj.sendCommand(cmd, SingleTon.getInstance().getListWriteCharecteristic(), SingleTon.getInstance().getmBluetoothLeService());
} else{
showDialog(getActivity(), getResources().getString(R.string.dialogtitle), "First do configuration for Bluetooth");
}
}

现在终于代码发送命令

public void sendCommand(String Control_INST, ArrayList<BluetoothGattCharacteristic> charFromSinglonton ,
BluetoothLeService bleGATTService){
for(BluetoothGattCharacteristic gattChar :charFromSinglonton) {
if (charFromSinglonton != null) {
final BluetoothGattCharacteristic characteristic = gattChar;
boolean status = bleGATTService.writeCharacteristic(characteristic, hexStringToByteArray(Control_INST));
if (status) {
SingleTon.getInstance().setCorrectWriteGattCharecteristic(gattChar);
}
}
}
}

提前感谢大家。

如果您单击同一按钮两次(如第一个按钮),会发生什么?发送命令两次还是只发送一次?

最新更新