Ejemplo n.º 1
0
    void Scan(DeviceName[] _supportedDevices)
    {
        Debug.Log("Start scanning...");
        BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null, null, (deviceID, name, rssi, adInfo) =>
        {
            for (int i = 0; i < _supportedDevices.Length; ++i)
            {
                for (int j = 0; j < m_deviceNames[_supportedDevices[i]].Length; ++j)
                {
                    string targetName = m_deviceNames[_supportedDevices[i]][j];
                    //  deviceID:   安卓上是 MAC 地址,iOS 上是某一串不明代码
                    //  adInfo:     广播数据,第 2-7 字节是 MAC 地址
                    if (name.Equals(targetName))
                    {
                        Debug.Log("Found: " + targetName);
                        BluetoothLEHardwareInterface.StopScan();

                        m_device = GetDeviceScript(_supportedDevices[i]);
                        m_device.SetAppInfo(m_userID, m_appID, m_appName, m_appDescription);
                        m_device.m_deviceID = deviceID;
#if UNITY_IPHONE
                        m_device.m_MAC = GeekplayCommon.BytesToHexString(adInfo, "").Substring(4, 12);
#elif UNITY_ANDROID
                        m_device.m_MAC = deviceID.Replace(":", "");
#endif

                        return;
                    }
                }
            }
        });
    }
Ejemplo n.º 2
0
    string GetHardwareVersion(byte[] _data)
    {
        byte[] temp = new byte[2];

        for (int i = 0; i < temp.Length; ++i)
        {
            temp[i] = _data[i + 8];
        }

        return(GeekplayCommon.BytesToHexString(temp, "."));
    }
Ejemplo n.º 3
0
    void ParseSign4Local(byte[] _data)
    {
        signPackCount4Local++;
        signData4Local += GeekplayCommon.BytesToHexString(_data, "");
        Debug.Log("sign Data " + signPackCount4Local + " : " + GeekplayCommon.BytesToHexString(_data, ""));

        if (4 == signPackCount4Local)
        {
            signPackCount4Local = 0;
            signPack            = signData4Local;
            signData4Local      = "";
        }
    }
Ejemplo n.º 4
0
    void ParseSign4Remote(byte[] _data)
    {
        signPackCount4Remote++;
        signData4Remote += GeekplayCommon.BytesToHexString(_data, "");
        Debug.Log("sign Data " + signPackCount4Remote + " : " + GeekplayCommon.BytesToHexString(_data, ""));

        if (4 == signPackCount4Remote)
        {
            signPackCount4Remote = 0;
            sign1           = signData4Remote.Substring(0, 64);
            sign2           = signData4Remote.Substring(64, 64);
            signData4Remote = "";
        }
    }
Ejemplo n.º 5
0
    protected IEnumerator CoVerifyDevice(Action _complete = null)
    {
        //  读取固件和硬件版本号
        GetHardwareInfo(m_deviceID);
        yield return(new WaitUntil(() => { return ((null != firmwareVersion) && (null != hardwareVersion)); }));

        //  订阅签名通道
        yield return(StartCoroutine(Subscribe(m_deviceID, "FFF0", "FFF9", ParseSign4Local)));

        //  将 token 发给硬件
        byte[] tokenPack1 = GenerateTokenPack();
        byte[] tokenPack2 = GenerateTokenPack();
        WriteCharacteristic("FFF0", "FFFA", tokenPack1);
        WriteCharacteristic("FFF0", "FFFA", tokenPack2);

        //  等待硬件返回签名包
        yield return(new WaitUntil(() => { return (null != signPack); }));

        Debug.Log("Sign Received.");
        yield return(StartCoroutine(UnSubscribe("FFF0", "FFF9")));

        Debug.Log("signPack: " + signPack);
        string dataToSign = m_MAC + firmwareVersion.Replace(".", "") + hardwareVersion.Replace(".", "")
                            + GeekplayCommon.BytesToHexString(tokenPack1, "").Substring(6, 20)
                            + GeekplayCommon.BytesToHexString(tokenPack2, "").Substring(6, 20);

        Debug.Log("Data to sign: " + dataToSign);

        if (GeekplayCommon.VerifySign(dataToSign, signPack, pubKeyX, pubKeyY))
        {
            Debug.Log("Legal Hardware!");
            isLegal = true;
        }
        else
        {
            Debug.Log("Illegal Hardware!");
            isLegal = false;
        }

        if (null != _complete)
        {
            _complete();
        }
    }