yy1717
2020-11-25 1baa8f7baa336ce7e78d4b8389a351526cb8c673
蓝牙密码
10个文件已修改
211 ■■■■■ 已修改文件
lib/src/main/cpp/master/comm_if.cpp 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/src/main/cpp/master/comm_if.h 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/src/main/cpp/native-lib.cpp 64 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/src/main/cpp/native-lib.h 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/src/main/cpp/rtk_platform/platform.cpp 61 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/src/main/cpp/rtk_platform/platform.h 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/src/main/java/com/anyun/exam/lib/RemoteService.java 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/src/main/java/com/anyun/exam/lib/util/Bluetooth.java 28 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/src/main/java/com/anyun/exam/lib/util/BluetoothChatService.java 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/src/main/java/com/anyun/exam/lib/util/Constants.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lib/src/main/cpp/master/comm_if.cpp
@@ -1988,6 +1988,21 @@
            MasterInqRoadStatus();
            break;
        }
        case ID_MS_BLUETOOTH_NAME: {
            Document doc;
            doc.Parse(value);
            if (!doc.HasParseError()) {
                if (doc.HasMember("bluetooth_name") && doc.HasMember("bluetooth_addr")) {
                    const Value& s1 = doc["bluetooth_name"];
                    const Value& s2 = doc["bluetooth_addr"];
                    SetRemoteBluetooth(s1.GetString(), s2.GetString());
                } else if (doc.HasMember("bluetooth_addr")) {
                    const Value& s = doc["bluetooth_addr"];
                    SetRemoteBluetooth(NULL, s.GetString());
                }
            }
            break;
        }
        default:break;
    }
}
@@ -2026,12 +2041,22 @@
    SendMsgToMainProcIndep(ID_SM_LIGHT_EXAM_REQ, NULL);
}
void MA_SendBlueStatus(int status)
void MA_SendBlueStatus(const char *name, const char *addr, int status)
{
    StringBuffer sb;
    Writer<StringBuffer> writer(sb);
    writer.StartObject();
    if (name != NULL) {
        writer.Key("bluetooth_name");
        writer.String(name);
    }
    if (addr != NULL) {
        writer.Key("bluetooth_addr");
        writer.String(addr);
    }
    writer.Key("bluetooth_status");
    writer.Int(status);
    writer.EndObject();
lib/src/main/cpp/master/comm_if.h
@@ -153,6 +153,6 @@
void MA_SendCrossingStatus(const struct crossingStatusBrief *brief);
void MA_SendCanStatus(const struct canBrief *brief);
void MA_SendBlueStatus(int status);
void MA_SendBlueStatus(const char *name, const char *addr, int status);
#endif //MYAPPLICATION2_COMM_IF_H
lib/src/main/cpp/native-lib.cpp
@@ -340,7 +340,37 @@
    jclass cls = env->GetObjectClass(sg_obj);
    jmethodID fun = env->GetMethodID(cls, "ConnectBluetooth", "(Ljava/lang/String;Ljava/lang/String;)V");
    env->CallVoidMethod(sg_obj, fun, env->NewStringUTF(addr), env->NewStringUTF(pin));
    env->CallVoidMethod(sg_obj, fun, env->NewStringUTF(addr), (pin == NULL) ? NULL : env->NewStringUTF(pin));
    env->DeleteLocalRef(cls);
    if (!ready_in_java_env) {
        //Detach主线程
        if (sg_jvm->DetachCurrentThread() != JNI_OK) {
            LOGE("%s: DetachCurrentThread() failed", __FUNCTION__);
        }
    }
}
void DisconnectBluetooth(void)
{
    JNIEnv *env;
    bool ready_in_java_env = false;
    if (sg_jvm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
        // Attach主线程
        if (sg_jvm->AttachCurrentThread(&env, NULL) != JNI_OK) {
            LOGE("%s: AttachCurrentThread() failed", __FUNCTION__);
            return;
        }
    } else {
        ready_in_java_env = true;
    }
    jclass cls = env->GetObjectClass(sg_obj);
    jmethodID fun = env->GetMethodID(cls, "DisconnectBluetooth", "()V");
    env->CallVoidMethod(sg_obj, fun);
    env->DeleteLocalRef(cls);
@@ -493,3 +523,35 @@
    PlatformStatusChanged(BLUETOOTH_STATUS_EVT, &sta, 1);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_anyun_exam_lib_RemoteService_BluetoothConnected(JNIEnv *env, jobject thiz, jstring name,
                                                         jstring addr) {
    // TODO: implement BluetoothConnected()
    if (name != NULL && addr != NULL) {
        const char *strname = env->GetStringUTFChars(name, 0);
        const char *straddr = env->GetStringUTFChars(addr, 0);
        uint8_t data[128];
        strcpy((char *)data, strname);
        strcpy((char *)data + 64, straddr);
        env->ReleaseStringUTFChars(name, strname);
        env->ReleaseStringUTFChars(addr, straddr);
        PlatformStatusChanged(BLUETOOTH_STATUS_EVT, data, 128);
    } else if (addr != NULL) {
        const char *straddr = env->GetStringUTFChars(addr, 0);
        uint8_t data[64];
        strcpy((char *)data, straddr);
        env->ReleaseStringUTFChars(addr, straddr);
        PlatformStatusChanged(BLUETOOTH_STATUS_EVT, data, 64);
    } else {
        uint8_t sta = 3;
        PlatformStatusChanged(BLUETOOTH_STATUS_EVT, &sta, 1);
    }
}
lib/src/main/cpp/native-lib.h
@@ -30,5 +30,6 @@
void PlayRing(void);
void SendToBluetooth(const uint8_t *data, int length);
void ConnectToBluetooth(const char *addr, const char *pin);
void DisconnectBluetooth(void);
#endif //RTKBASESTATION_NATIVE_LIB_H
lib/src/main/cpp/rtk_platform/platform.cpp
@@ -30,6 +30,8 @@
#define DEBUG(fmt, args...)     LOGD("<platform> <%s>: " fmt, __func__, ##args)
using namespace std;
struct platformSocket {
    char domain_name[32];
    int port;
@@ -86,6 +88,10 @@
static struct gpsBrief gbf;
static struct rtkBrief rbf;
static bool btEnable = false, btConnected = false;
static char btAddr[64] = {0};
static char *btPin = NULL;
static void AddEvnet(uint32_t event, const uint8_t *data, int length);
static struct event_t * FetchEvent(void);
@@ -575,25 +581,47 @@
        }
    }
    if (events & BLUETOOTH_STATUS_EVT) {
        DEBUG("BLUETOOTH_STATUS_EVT %d", data[0]);
        DEBUG("BLUETOOTH_STATUS_EVT");
        uint8_t sta;
        if (data[0] == 3) {
        if (length == 128) {
            MA_SendBlueStatus((char *)data, (char *)data+64, 3);
            sta = 3;
        } else if (length == 64) {
            MA_SendBlueStatus(NULL, (char *)data, 3);
            sta = 3;
        } else {
            MA_SendBlueStatus(NULL, NULL, data[0]);
            sta = data[0];
        }
        if (sta == 3) {
            // Connected
            btConnected = true;
            ParseMcuInit();
            ConfigRTKModuleLater();
            PlayTTS("蓝牙连接", NULL);
        } else if (data[0] == 2) {
        } else if (sta == 2) {
            // Disconnect
            btConnected = false;
            PlayTTS("蓝牙断开", NULL);
        } else if (data[0] == 1) {
        } else if (sta == 1) {
            // Open
            ConnectToBluetooth("00:1B:35:16:20:4A", "3800");
            btEnable = true;
            if (strlen(btAddr) > 0) {
                ConnectToBluetooth(btAddr, NULL);
            }
//            ConnectToBluetooth("00:1B:35:16:20:4A", "5516");
//            ConnectToBluetooth("00:1B:35:16:20:4A", "3800");``
//            ConnectToBluetooth("00:1D:43:9A:E0:79", "1900");
//            ConnectToBluetooth("DESKTOP-IE9V7U8", "0000");
            PlayTTS("蓝牙启动", NULL);
        } else {
            // Close
            btEnable = false;
            btConnected = false;
            PlayTTS("蓝牙关闭", NULL);
        }
    }
@@ -815,3 +843,26 @@
        memcpy(defaultMcuRom.rom, rom, length);
    }
}
void SetRemoteBluetooth(const char *name, const char *addr)
{
    strcpy(btAddr, addr);
    if (btConnected) {
        DisconnectBluetooth();
    }
    /*string csname = name;
    if (csname != NULL) {
        if (csname.find("AKS3A_TEST") != string::npos) {
            btPin = "1111";
        } else if (csname.find("AKS3A_") != string::npos && csname.size() >= 8) {
            int a = stoi(csname.substr(csname.size()-8, 4));
            int b = stoi(csname.substr(csname.size()-4, 4));
        }
    }*/
    if (btEnable) {
        ConnectToBluetooth(addr, NULL);
    }
}
lib/src/main/cpp/rtk_platform/platform.h
@@ -49,5 +49,6 @@
void StopRtkDownload(void);
void LoadDefaultMcuRom(const char *verCode, const uint8_t *rom, int length);
void SetRemoteBluetooth(const char *name, const char *addr);
#endif //RTKDRIVERTEST_PLATFORM_H
lib/src/main/java/com/anyun/exam/lib/RemoteService.java
@@ -70,6 +70,8 @@
    private Bluetooth mBluetooth = null;
    private BluetoothChatService mChatService = null;
    private String mConnectedDeviceName = null;
    private String mConnectedDeviceAddr = null;
    private String mTargetBluetooth = null;
    private String mTargetBluetoothAddr = null;
    private boolean mDiscoveryBluetooth = false;
@@ -415,6 +417,14 @@
        }
    }
    public void DisconnectBluetooth()
    {
        handlerConnectBluetooth.removeCallbacks(runnableConnectBluetooth);
        if (mChatService != null) {
            mChatService.stop();
        }
    }
    Handler handlerConnectBluetooth = new Handler();
    Runnable runnableConnectBluetooth = new Runnable() {
        @Override
@@ -473,7 +483,8 @@
                    switch (msg.arg1) {
                        case BluetoothChatService.STATE_CONNECTED:
                            Log.d(TAG, "蓝牙已连接");
                            BluetoothStatusChange(3);
//                            BluetoothStatusChange(3);
                            BluetoothConnected(mConnectedDeviceName, mConnectedDeviceAddr);
                            break;
                        case BluetoothChatService.STATE_CONNECTING:
                            Log.d(TAG, "蓝牙连接中...");
@@ -505,6 +516,10 @@
                    mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME);
                    Log.d(TAG, "MESSAGE_DEVICE_NAME: " + mConnectedDeviceName);
                    break;
                case Constants.MESSAGE_DEVICE_ADDRESS:
                    mConnectedDeviceAddr = msg.getData().getString(Constants.DEVICE_ADDRESS);
                    Log.d(TAG, "MESSAGE_DEVICE_ADDRESS: " + mConnectedDeviceAddr);
                    break;
                case Constants.MESSAGE_TOAST:
                    break;
                    default:break;
@@ -529,6 +544,7 @@
    public native void MainProcBinMsgEntry(int cmd, byte []data, int length);
    public native void UpgradeMcu(String vercode, byte []rom);
    public native void TextSpeakEnd(int id);
    public native void BluetoothConnected(String name, String addr);
    public native void BluetoothStatusChange(int status);
    public native void BluetoothDataComeIn(byte []data, int length);
}
lib/src/main/java/com/anyun/exam/lib/util/Bluetooth.java
@@ -15,13 +15,13 @@
public class Bluetooth extends BroadcastReceiver {
    private static final String TAG = Bluetooth.class.getCanonicalName();
    public static final String DEFAULT_BT_PASSWORD = "0000";
    public static final String DEFAULT_BT_PASSWORD = "1111";
    private static Bluetooth instance = null;
    private Context context;
    private BluetoothAdapter mBtAdapter = null;
    private Handler mHandler;
    private String pin = DEFAULT_BT_PASSWORD;
    private String pin = null;
    private String mName = null;
    public static Bluetooth getInstance(Context context, Handler handler) {
@@ -46,6 +46,9 @@
        filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
        filter.addAction("android.bluetooth.device.action.PAIRING_REQUEST");
        filter.setPriority(1000);
        context.registerReceiver(this, filter);
    }
@@ -129,6 +132,25 @@
            Log.d(TAG, "匹配请求");
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            String name = device.getName();
            if (pin == null && name != null && name.contains("AKS3A_") && name.length() >= 8) {
                char [] sn = new char[8];
                name.getChars(name.length() - 8, name.length(), sn, 0);
                int a = ((sn[0] - '0') * 10 + sn[1] - '0') * 100 + (sn[2] - '0') * 10 + sn[3] - '0';
                int b = ((sn[4] - '0') * 10 + sn[5] - '0') * 100 + (sn[6] - '0') * 10 + sn[7] - '0';
                a = (a * b * 1978) % 10000;
                pin = String.format("%02d", a/100) + String.format("%02d", a%100);
            }
            if (pin == null) {
                pin = DEFAULT_BT_PASSWORD;
            }
            Log.d(TAG, "匹配吗 " + pin);
            /*if (device.getName().equals(BLUETOOTH_OBD_NAME) || device.getName().equals(BLUETOOTH_TARGET2_NAME)) */{
                try {
                    ClsUtils.setPairingConfirmation(device.getClass(), device, true);
@@ -139,7 +161,7 @@
                    ClsUtils.setPin(device.getClass(), device, pin);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e(TAG, e.getMessage());
                }
            }
        } else if (BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
lib/src/main/java/com/anyun/exam/lib/util/BluetoothChatService.java
@@ -35,7 +35,7 @@
 * This class does all the work for setting up and managing Bluetooth
 * connections with other devices. It has a thread that listens for
 * incoming connections, a thread for connecting with a device, and a
 * thread for performing data transmissions when connected.
 * thread for amperforming data transmisspions when connected.
 */
public class BluetoothChatService {
    // Debugging
@@ -212,6 +212,12 @@
        msg.setData(bundle);
        mHandler.sendMessage(msg);
        msg = mHandler.obtainMessage(Constants.MESSAGE_DEVICE_ADDRESS);
        bundle = new Bundle();
        bundle.putString(Constants.DEVICE_ADDRESS, device.getAddress());
        msg.setData(bundle);
        mHandler.sendMessage(msg);
        setState(STATE_CONNECTED);
        // Start the thread to manage the connection and perform transmissions
lib/src/main/java/com/anyun/exam/lib/util/Constants.java
@@ -31,6 +31,7 @@
    public static final int MESSAGE_BLUETOOTH_STATUS = 6;
    public static final int MESSAGE_BLUETOOTH_FOUND = 7;
    public static final int MESSAGE_BLUETOOTH_DISCOVERY_FINISHED = 8;
    public static final int MESSAGE_DEVICE_ADDRESS = 9;
    // Key names received from the BluetoothChatService Handler
    public static final String DEVICE_NAME = "device_name";