我正在努力解决一个奇怪的问题,即从Android设备发送到BLE加密狗的命令。这里我有两个按钮 Button1 和 Button2 。点击每个按钮我试图发送到BLE加密狗的不同命令。问题的详细说明如下:
*
震荡 - 仅在第一次按下第一个按钮时发送命令。
* 现在这里是我使用的代码 下面是我从Fragment的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);
}
}
}
}
提前感谢所有人。
答案 0 :(得分:1)
如果您点击两次相同的按钮(如第一个按钮)会发生什么?发送命令两次或只发送一次?