Przeglądaj źródła

背夹式扫码枪

chenzixuan 2 miesięcy temu
commit
cf1ec653d0

+ 263 - 0
android示例代码/BluetoothManager.java

@@ -0,0 +1,263 @@
+package com.nuoda.uniapp.Application;
+
+import android.annotation.SuppressLint;
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.bluetooth.BluetoothGatt;
+import android.bluetooth.BluetoothGattCallback;
+import android.bluetooth.BluetoothGattCharacteristic;
+import android.bluetooth.BluetoothGattDescriptor;
+import android.bluetooth.BluetoothGattService;
+import android.bluetooth.BluetoothProfile;
+import android.content.Context;
+import android.util.Log;
+
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+
+/**
+ *     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
+ *     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
+ *     <uses-permission android:name="android.permission.BLUETOOTH" />
+ *     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
+ * */
+public class BluetoothManager {
+
+    private BluetoothManager(){ }
+
+    private static BluetoothManager mBluetoothManager;
+
+    public static BluetoothManager getBluetoothManager(){
+
+        if (mBluetoothManager==null)mBluetoothManager = new BluetoothManager();
+        return mBluetoothManager;
+    }
+
+    private BluetoothGatt bluetoothGatt = null;
+    private BluetoothGattCharacteristic myCharacteristic = null;
+    private String bleValue = "....";
+
+    private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
+
+    private final BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {
+
+        private UUID YOUR_SERVICE_UUID = UUID.fromString("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
+        private UUID YOUR_CHARACTERISTIC_UUID = UUID.fromString("beb5483e-36e1-4688-b7f5-ea07361b26a8");
+
+        @Override
+        public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
+            super.onMtuChanged(gatt, mtu, status);
+
+            openNotification();
+        }
+
+        @SuppressLint("MissingPermission")
+        @Override
+        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
+
+            //连接成功开启服务
+            if (newState == BluetoothProfile.STATE_CONNECTED) {
+
+                gatt.discoverServices();
+
+            }else {
+
+                disconnect();
+            }
+        }
+
+        //当 BLE 设备的服务被发现时调用。这是在连接成功后进行服务发现时触发的回调
+        @SuppressLint("MissingPermission")
+        @Override
+        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
+
+            if (status == BluetoothGatt.GATT_SUCCESS) {
+
+                gatt.requestMtu(300);
+                BluetoothGattService service = gatt.getService(YOUR_SERVICE_UUID);
+
+                if (service!=null){
+
+                    myCharacteristic = service.getCharacteristic(YOUR_CHARACTERISTIC_UUID);
+                    bluetoothGatt = gatt;
+                }
+            }
+        }
+
+        //当特性值发生变化时调用。这通常是因为设备向手机推送了新的数据
+        @Override
+        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){
+
+            bluetoothGatt = gatt;
+            myCharacteristic = characteristic;
+            getString();
+
+        }
+
+        //当读取特性值的操作完成时调用。此时可以获取特性的值
+        @Override
+        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
+
+            if (status == BluetoothGatt.GATT_SUCCESS){
+
+                bluetoothGatt = gatt;
+                myCharacteristic = characteristic;
+                getString();
+            }
+        }
+
+        //当写入特性值的操作完成时调用
+        @Override
+        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
+            // 检查写入操作是否成功
+            if (status == BluetoothGatt.GATT_SUCCESS) {
+
+                bluetoothGatt = gatt;
+                myCharacteristic = characteristic;
+                getString();
+
+            }
+        }
+    };
+
+    @SuppressLint("MissingPermission")
+    private void openNotification(){
+
+        if(bluetoothGatt==null || myCharacteristic==null)return;
+
+        try{
+            //开启通知
+            bluetoothGatt.setCharacteristicNotification(myCharacteristic, true);
+            BluetoothGattDescriptor descriptor = myCharacteristic.getDescriptors().get(0);
+            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
+            bluetoothGatt.writeDescriptor(descriptor);
+
+        }catch (Exception e){
+
+            Log.d("bleerror:",e.getMessage());
+        }
+
+        //主动获取特征值
+        read();
+    }
+
+    @SuppressLint("MissingPermission")
+    public List<BluetoothDevice> getDivList(){
+
+        List<BluetoothDevice> listData = new ArrayList<>();
+
+        if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
+            // 蓝牙不可用
+        }else {
+
+            Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
+            for (BluetoothDevice bean:pairedDevices)listData.add(bean);
+
+        }
+
+        return listData;
+    }
+
+    @SuppressLint("MissingPermission")
+    public void connect(Context context, BluetoothDevice device) {
+
+        if (bluetoothGatt!=null)return;
+
+        bluetoothGatt = device.connectGatt(context, false, bluetoothGattCallback);
+    }
+
+    //读取
+    @SuppressLint("MissingPermission")
+    public void read(){
+
+        if (bluetoothGatt==null || myCharacteristic==null)return;
+
+        bluetoothGatt.readCharacteristic(myCharacteristic);
+    }
+
+    //写入
+    @SuppressLint("MissingPermission")
+    public void write(String value){
+
+        if (bluetoothGatt==null || myCharacteristic==null)return;
+
+        myCharacteristic.setValue(value.getBytes());
+        bluetoothGatt.writeCharacteristic(myCharacteristic);
+    }
+
+    @SuppressLint("MissingPermission")
+    public void disconnect() {
+
+        try {
+
+            if (bluetoothGatt != null) {
+                bluetoothGatt.disconnect();
+                bluetoothGatt.close();
+                bluetoothGatt = null;
+                myCharacteristic=null;
+            }
+        } catch (Exception e) {
+            // 处理异常关闭
+        }
+    }
+
+    public boolean isble(){
+
+        return bluetoothGatt!=null;
+    }
+
+    public String getValue(){
+
+        return bleValue;
+    }
+
+    public void getString(){
+
+        if (myCharacteristic==null)return;
+
+        byte[] data = myCharacteristic.getValue();
+
+        if (data==null)return;
+
+        StringBuilder sb = new StringBuilder();
+        for (byte b:data) sb.append((char) (b & 0xFF));
+        bleValue = sb.toString();
+
+        if (bleValue==null)bleValue="";
+
+        if("r:not rfid".equals(bleValue)){
+
+            bleValue = "未识别到ic卡";
+
+        }else if ("r:rfid error".equals(bleValue)){
+
+            bleValue = "操作ic卡异常";
+
+        }else if ("r:write ok".equals(bleValue)){
+
+            bleValue = "写入成功";
+
+        }else if (bleValue.indexOf("r:")==0){
+
+            bleValue = "ic卡读取数据:" + bleValue.substring(2);
+
+        }else if ("c:...".equals(bleValue)){
+
+            bleValue = "扫码头已打开";
+
+        }else if ("c:end".equals(bleValue)){
+
+            bleValue = "扫码头已关闭";
+
+        }else if (bleValue.indexOf("c:")==0){
+
+            bleValue = "扫码数据:" + bleValue.substring(2);
+
+        }
+
+        Log.d("bledata:",bleValue);
+    }
+}

BIN
blander/qr5.blend


BIN
blander/qr5.blend1


BIN
blander/历史记录/qr.blend


BIN
blander/历史记录/qr.blend1


BIN
blander/历史记录/qr1.blend


BIN
blander/历史记录/qr1.blend1


BIN
blander/历史记录/qr2.blend


BIN
blander/历史记录/qr2.blend1


BIN
blander/历史记录/qr3.blend


BIN
blander/历史记录/qr3.blend1


BIN
blander/历史记录/qr4.blend


BIN
blander/历史记录/qr4.blend1


BIN
blander/历史记录/qr5.blend


BIN
blander/历史记录/qr5.blend1


+ 23 - 0
bledome/bledome.ino

@@ -0,0 +1,23 @@
+#include "blemanager.h"
+#include "rfid.h"
+#include "qr.h"
+
+ void setup() {
+  
+  Serial.begin(19200);
+  
+  bleSetup();
+  rfidSetup();
+  qrSetup();
+
+  Serial.println("lllalalal");
+
+ }
+
+ void loop(){
+
+  bleLoop();
+  rfidLoop();
+  qrLoop();
+
+ }

+ 105 - 0
bledome/blemanager.h

@@ -0,0 +1,105 @@
+//https://blog.csdn.net/Naisu_kun/article/details/115958024
+
+#include <BLEDevice.h> // 引入相关库
+#include <BLE2902.h>
+
+#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" // 服务 自定义UUID
+
+#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" // 特征UUID
+
+bool deviceConnected = false;
+
+BLECharacteristic *pCharacteristic;
+
+int ledPin = 8;
+
+ 
+// Server回调函数声明
+class MyServerCallbacks: public BLEServerCallbacks {
+    void onConnect(BLEServer* pServer) {
+
+      //有设备接入
+      Serial.println("device open~");
+      digitalWrite(ledPin, LOW);
+      deviceConnected = true;
+    };
+
+    void onDisconnect(BLEServer* pServer) {
+
+      //设备断开
+      Serial.println("device break~");
+      // 在有设备接入后Advertising广播会被停止,所以要在设备断开连接时重新开启广播
+      // 不然的话只有重启ESP32后才能重新搜索到
+      pServer->startAdvertising(); //该行效果同 BLEDevice::startAdvertising();
+      digitalWrite(ledPin, HIGH);
+      deviceConnected = false;
+    }
+};
+
+class MyCallbacks: public BLECharacteristicCallbacks {
+    void onRead(BLECharacteristic* pCharacteristic) { // 客户端读取事件回调函数
+    
+      Serial.println("value read");
+    }
+
+    void onWrite(BLECharacteristic *pCharacteristic) { // 客户端写入事件回调函数
+      Serial.println("value write");
+    }
+};
+
+void bleSetup() {
+
+  pinMode(ledPin, OUTPUT);
+  digitalWrite(ledPin, HIGH);
+ 
+  BLEDevice::init("APTBLE"); // 填写自身对外显示的蓝牙设备名称,并初始化蓝牙功能
+
+  BLEServer *pServer = BLEDevice::createServer(); // 创建服务器
+
+  pServer->setCallbacks(new MyServerCallbacks()); // 绑定回调函数
+
+  BLEService *pService = pServer->createService(SERVICE_UUID); // 创建服务
+
+  pCharacteristic = pService->createCharacteristic( // 创建特征
+                                                  CHARACTERISTIC_UUID, 
+                                                  BLECharacteristic::PROPERTY_READ |  // 启用读取
+                                                  BLECharacteristic::PROPERTY_WRITE |  // 启用写入
+                                                  BLECharacteristic::PROPERTY_NOTIFY | // 启用通知
+                                                  BLECharacteristic::PROPERTY_INDICATE // 启用通知
+                                                  );
+
+  pCharacteristic->setCallbacks(new MyCallbacks());
+
+  pCharacteristic->addDescriptor(new BLE2902()); // 不加这行可能无法使用通知
+
+  pCharacteristic->setValue("this is apt"); // 设置该Characteristic的Value值
+
+  pService->start(); // 启动服务
+  BLEDevice::startAdvertising(); // 开启Advertising广播
+}
+
+//消息通知
+void bleNotify(){
+
+      pCharacteristic->notify();
+      Serial.println("notify...");
+}
+
+int loopBleSize=1000; //loop间隔
+int bleMillisTime;
+void bleLoop() {
+  
+  //1秒发送1次通知
+  if (!deviceConnected)return;
+
+  int bleTimeCurrent = millis();
+  
+  if(bleTimeCurrent<=0)bleMillisTime = bleTimeCurrent;
+
+  if((bleTimeCurrent - bleMillisTime)>loopBleSize){
+
+    bleMillisTime = bleTimeCurrent;
+    pCharacteristic->notify();
+    Serial.println("notify...");
+  }
+}

BIN
bledome/build/esp32.esp32.esp32c3/bledome.ino.bin


BIN
bledome/build/esp32.esp32.esp32c3/bledome.ino.bootloader.bin


BIN
bledome/build/esp32.esp32.esp32c3/bledome.ino.elf


Plik diff jest za duży
+ 96684 - 0
bledome/build/esp32.esp32.esp32c3/bledome.ino.map


BIN
bledome/build/esp32.esp32.esp32c3/bledome.ino.partitions.bin


+ 87 - 0
bledome/qr.h

@@ -0,0 +1,87 @@
+//https://blog.csdn.net/luyang14/article/details/115796909
+
+#include <HardwareSerial.h>
+
+// 定义串口对象Serial2
+HardwareSerial Serial2(1);
+#define PIN_RX 1
+#define PIN_TX 0
+#define PIN_BUTTON 2
+
+String qrData = "";
+int qrType=0;
+
+String qrOpen = "c:...";
+String qrEnd = "c:end";
+
+// 串口2接收中断服务程序
+void serialEvent() {
+
+    while (Serial2.available() > 0) {
+
+      if(qrType==0)qrData = "";
+
+      qrType = 1;
+      
+      char incomingByte = Serial2.read();
+      qrData += (char)incomingByte;
+
+    }
+
+    if(qrType==1){
+
+      Serial.println("qrData: " + qrData);
+      String str = "c:" + qrData;
+      pCharacteristic->setValue(str.c_str());
+      digitalWrite(PIN_BUTTON,HIGH); //这里扫码成功后需要你自己拉高电频 不然扫码成功后返回数据你再次拉低失败的
+      qrType = 0;
+    }
+
+}
+
+void qrCommand(){
+
+    String command = pCharacteristic->getValue().c_str();
+
+    if (command.startsWith("2:0")) {
+
+      pCharacteristic->setValue(qrOpen.c_str());
+
+      digitalWrite(PIN_BUTTON,LOW);
+
+    } else if (command.startsWith("2:1")) {
+
+      pCharacteristic->setValue(qrEnd.c_str());
+
+      digitalWrite(PIN_BUTTON,HIGH);
+    } 
+    
+}
+
+void qrSetup(){
+
+  Serial2.begin(9600, SERIAL_8N1,PIN_RX,PIN_TX); // 设置串口的波特率、1数据位、奇偶校验位和停止位
+
+  pinMode(PIN_BUTTON,OUTPUT);//设置引脚为输出
+  digitalWrite(PIN_BUTTON,HIGH);//高电平关
+  // digitalWrite(PIN_BUTTON,LOW);//低电平开
+  
+}
+
+int loopQrSize=1000; //loop间隔
+int qrMillisTime;
+void qrLoop(){
+
+    //1秒读取一次指令
+  int qrTimeCurrent = millis();
+  
+  if(qrTimeCurrent<=0)qrMillisTime = qrTimeCurrent;
+
+  if((qrTimeCurrent - qrMillisTime)>loopQrSize){
+
+    qrMillisTime = qrTimeCurrent;
+
+    qrCommand();
+    serialEvent();
+  }
+}

+ 228 - 0
bledome/rfid.h

@@ -0,0 +1,228 @@
+//https://blog.csdn.net/Y_xianlin/article/details/141708176
+
+#include <SPI.h>
+#include <MFRC522.h>
+
+// 定义RC522模块的引脚
+#define SS_PIN    7 // SDA
+#define RST_PIN   3 // RST
+
+// 创建MFRC522对象
+MFRC522 mfrc522(SS_PIN, RST_PIN);
+
+
+MFRC522::MIFARE_Key key; // 密钥
+
+int blockAddrs[2] = {1,2};// 数据块
+
+String errorValue = "r:rfid error";
+String notRfid = "r:not rfid";
+String writeValue = "r:write ok";
+
+void rfidSetup(){
+
+  SPI.begin(); // 初始化SPI总线
+  mfrc522.PCD_Init(); // 初始化MFRC522模块
+
+  // 初始化密钥(全为0xFF)
+  for (byte i = 0; i < 6; i++) {
+    key.keyByte[i] = 0xFF;
+  }
+  
+  Serial.println("RFID init ok...");
+
+}
+
+
+String readRFID(int index){
+
+ int blockAddr = blockAddrs[index];
+
+  // 认证指定扇区
+  MFRC522::StatusCode status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockAddr, &key, &(mfrc522.uid));
+
+  if (status != MFRC522::STATUS_OK) {
+    Serial.println("rfid error"); //认证失败
+    return errorValue;
+  }
+ 
+  // 读取扇区数据
+  byte buffer[18];
+  byte size = sizeof(buffer);
+  status = mfrc522.MIFARE_Read(blockAddr, buffer, &size);
+  if (status != MFRC522::STATUS_OK) {
+    Serial.println("rfid error"); //认证失败
+    return errorValue;
+  }
+ 
+
+  String str = "";
+  for (byte i = 0; i < 15; i++) {
+
+    str += (char)buffer[i];
+  }
+  
+  Serial.println("read:" + str);
+  return str;
+}
+
+void readRFID() {
+
+  // 检查是否有新的RFID标签
+  if (!mfrc522.PICC_IsNewCardPresent()) {
+    Serial.println("not rfid"); //没rfid
+    pCharacteristic->setValue(notRfid.c_str());
+    return;
+  }
+ 
+  // 选择其中一个RFID标签
+  if (!mfrc522.PICC_ReadCardSerial()) {
+    Serial.println("rfid fail"); //失败
+    pCharacteristic->setValue(errorValue.c_str());
+    return;
+  }
+
+  String str = "";
+
+  for(int i=0;i<2;i++){
+
+    String strData = readRFID(i);
+
+    if(strData==errorValue){
+
+      pCharacteristic->setValue(errorValue.c_str());
+
+      // 停止与标签的通信
+      mfrc522.PICC_HaltA();
+      mfrc522.PCD_StopCrypto1();
+      return;
+
+    }else{
+
+      str = str + strData;
+    }
+    
+  }
+
+  str = "r:" + str;
+  Serial.println(str);
+  pCharacteristic->setValue(str.c_str());
+ 
+  // 停止与标签的通信
+  mfrc522.PICC_HaltA();
+  mfrc522.PCD_StopCrypto1();
+}
+
+String writeRFID(String data,int index){
+
+   if(index>=2)return writeValue;
+
+   int blockAddr = blockAddrs[index];
+
+   int strLen = data.length() - index*15;
+
+   if(strLen<=0)return writeValue;
+
+   if(strLen>15)strLen=15;
+
+   String strData = data.substring(index*15, index*15 + strLen);
+
+   Serial.print("write data: ");
+   Serial.println(strData);
+
+  // 将数据转化为字节数组(最多16字节)
+  byte dataBlock[16];
+  strData.getBytes(dataBlock, 16);
+ 
+  // 认证指定扇区
+  MFRC522::StatusCode status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockAddr, &key, &(mfrc522.uid));
+
+  if (status != MFRC522::STATUS_OK) {
+    Serial.println("rfid error"); //认证失败
+    return errorValue;
+  }
+ 
+  // 写入数据到指定块
+  status = mfrc522.MIFARE_Write(blockAddr, dataBlock, 16);
+
+  if (status != MFRC522::STATUS_OK) {
+    Serial.println("rfid error"); //写入失败
+    return errorValue;
+  } else {
+    Serial.println("rfid ok"); //写入成功
+    return writeValue;
+  }
+  
+
+}
+ 
+void writeRFID(String data) {
+
+  // 检查是否有新的RFID标签
+  if (!mfrc522.PICC_IsNewCardPresent()) {
+    Serial.println("not rfid"); //没rfid
+    pCharacteristic->setValue(notRfid.c_str());
+    return;
+  }
+ 
+  // 选择其中一个RFID标签
+  if (!mfrc522.PICC_ReadCardSerial()) {
+    Serial.println("rfid fail"); //失败
+    pCharacteristic->setValue(errorValue.c_str());
+    return;
+  }
+  
+ 
+  String strData = "";
+
+  int len = data.length()/15;
+  if(data.length()%15>0)len=len+1;
+
+  for(int i=0;i<len;i++){
+
+    strData = writeRFID(data,i);
+  }
+
+  pCharacteristic->setValue(strData.c_str());
+ 
+  // 停止与标签的通信
+  mfrc522.PICC_HaltA();
+  mfrc522.PCD_StopCrypto1();
+}
+
+//0:开头字符串读取 1:开头字符串写入
+void rifdCommand(){
+
+    String command = pCharacteristic->getValue().c_str();
+
+    if (command.startsWith("0:")) {
+
+      readRFID();
+
+    } else if (command.startsWith("1:")) {
+
+      String data = command.substring(2);
+
+      if(data.length()<=30){
+        writeRFID(data);
+      }
+      
+    }
+}
+
+int loopRfidSize=1000; //loop间隔
+int rfidMillisTime;
+void rfidLoop(){
+
+  //1秒读取一次指令
+  int rfidTimeCurrent = millis();
+  
+  if(rfidTimeCurrent<=0)rfidMillisTime = rfidTimeCurrent;
+
+  if((rfidTimeCurrent - rfidMillisTime)>loopRfidSize){
+
+    rfidMillisTime = rfidTimeCurrent;
+    rifdCommand();
+  }
+
+}

BIN
扫码头/扫码模块用户设置手册详解.pdf


BIN
扫码头/扫码模块硬件参数简介.pdf


BIN
扫码枪文档.doc