Exemple #1
0
    public void connect(string addr)
    {
        if (isConnected)
        {
            return;
        }
        BTStatus    = false;
        linkData    = new BluetoothData();
        isConnected = false;
        readFound   = false;
        writeFound  = false;
        BTLog       = "Connecting to \n" + addr + "\n";
        BluetoothLEHardwareInterface.ConnectToPeripheral(addr, (address) => {; },
                                                         (address, serviceUUID) => {; },
                                                         (address, serviceUUID, characteristicUUID) =>
        {
            // discovered characteristic
            if (BluetoothData.isEqual(serviceUUID, peripheralList[address].service))
            {
                isConnected      = true;
                linkData.name    = peripheralList[address].name;
                linkData.address = address;
                linkData.service = peripheralList[address].service;
                if (BluetoothData.isEqual(characteristicUUID, peripheralList[address].rCharacteristic))
                {
                    readFound = true;
                    linkData.rCharacteristic = peripheralList[address].rCharacteristic;
                    BTLog += "readTrue \n";
                }
                if (BluetoothData.isEqual(characteristicUUID, peripheralList[address].wCharacteristic))
                {
                    writeFound = true;
                    linkData.wCharacteristic = peripheralList[address].wCharacteristic;
                    BTLog += "writeTrue \n";
                }

                if (readFound && writeFound)
                {
                    Invoke("delayConnect", 1f);
                    BTLog  = address + "\n";
                    BTLog += "Connected! \n";
                }
                BluetoothLEHardwareInterface.StopScan();
            }
        },
                                                         (address) =>
        {
            // this will get called when the device disconnects
            // be aware that this will also get called when the disconnect
            // is called above. both methods get call for the same action
            // this is for backwards compatibility
        });
    }