Пример #1
0
    private void handleBleConnected()
    {
        /* If we get data it's likely ACKs for benchmarking. */
        BleApi.BLEData data = new BleApi.BLEData();
        while (BleApi.PollData(out data, false))
        {
            string log_str = string.Format(
                "Recieved from {0} | {1} | {2}\n\t{3} bytes:\n\t",
                data.deviceId, data.serviceUuid, data.characteristicUuid, data.size
                );
            Debug.Log(log_str);

#if BLE_BENCHMARK
            if (awaitingResponse)   /* Got response. */
            {
                awaitingResponse = false;
                var now      = DateTime.Now;
                var timediff = now - previousSendTime;
                Debug.Log(
                    string.Format("Received ACK in {0} ms", timediff.TotalMilliseconds)
                    );
            }
#endif
        }
    }
Пример #2
0
 // Update is called once per frame
 void Update()
 {
     BleApi.ScanStatus status;
     if (isScanningDevices)
     {
         BleApi.DeviceUpdate res = new BleApi.DeviceUpdate();
         do
         {
             status = BleApi.PollDevice(ref res, false);
             if (status == BleApi.ScanStatus.AVAILABLE)
             {
                 if (!devices.ContainsKey(res.id))
                 {
                     devices[res.id] = new Dictionary <string, string>()
                     {
                         { "name", "" },
                         { "isConnectable", "False" }
                     }
                 }
                 ;
                 if (res.nameUpdated)
                 {
                     devices[res.id]["name"] = res.name;
                 }
                 if (res.isConnectableUpdated)
                 {
                     devices[res.id]["isConnectable"] = res.isConnectable.ToString();
                 }
                 // consider only devices which have a name and which are connectable
                 if (devices[res.id]["name"] != "" && devices[res.id]["isConnectable"] == "True")
                 {
                     // add new device to list
                     GameObject g = Instantiate(deviceScanResultProto, scanResultRoot);
                     g.name = res.id;
                     g.transform.GetChild(0).GetComponent <Text>().text = devices[res.id]["name"];
                     g.transform.GetChild(1).GetComponent <Text>().text = res.id;
                 }
             }
             else if (status == BleApi.ScanStatus.FINISHED)
             {
                 isScanningDevices         = false;
                 deviceScanButtonText.text = "Scan devices";
                 deviceScanStatusText.text = "finished";
             }
         } while (status == BleApi.ScanStatus.AVAILABLE);
     }
     if (isScanningServices)
     {
         BleApi.Service res = new BleApi.Service();
         do
         {
             status = BleApi.PollService(out res, false);
             if (status == BleApi.ScanStatus.AVAILABLE)
             {
                 serviceDropdown.AddOptions(new List <string> {
                     res.uuid
                 });
                 // first option gets selected
                 if (serviceDropdown.options.Count == 1)
                 {
                     SelectService(serviceDropdown.gameObject);
                 }
             }
             else if (status == BleApi.ScanStatus.FINISHED)
             {
                 isScanningServices             = false;
                 serviceScanButton.interactable = true;
                 serviceScanStatusText.text     = "finished";
             }
         } while (status == BleApi.ScanStatus.AVAILABLE);
     }
     if (isScanningCharacteristics)
     {
         BleApi.Characteristic res = new BleApi.Characteristic();
         do
         {
             status = BleApi.PollCharacteristic(out res, false);
             if (status == BleApi.ScanStatus.AVAILABLE)
             {
                 string name = res.userDescription != "no description available" ? res.userDescription : res.uuid;
                 characteristicNames[name] = res.uuid;
                 characteristicDropdown.AddOptions(new List <string> {
                     name
                 });
                 // first option gets selected
                 if (characteristicDropdown.options.Count == 1)
                 {
                     SelectCharacteristic(characteristicDropdown.gameObject);
                 }
             }
             else if (status == BleApi.ScanStatus.FINISHED)
             {
                 isScanningCharacteristics             = false;
                 characteristicScanButton.interactable = true;
                 characteristicScanStatusText.text     = "finished";
             }
         } while (status == BleApi.ScanStatus.AVAILABLE);
     }
     if (isSubscribed)
     {
         BleApi.BLEData res = new BleApi.BLEData();
         while (BleApi.PollData(out res, false))
         {
             subcribeText.text = BitConverter.ToString(res.buf, 0, res.size);
             // subcribeText.text = Encoding.ASCII.GetString(res.buf, 0, res.size);
         }
     }
     {
         // log potential errors
         BleApi.ErrorMessage res = new BleApi.ErrorMessage();
         BleApi.GetError(out res);
         if (lastError != res.msg)
         {
             Debug.LogError(res.msg);
             errorText.text = res.msg;
             lastError      = res.msg;
         }
     }
 }