Пример #1
0
 /** Search for the TrueTouch device. */
 private void handleBleScanning()
 {
     BleApi.DeviceUpdate device = new BleApi.DeviceUpdate();
     BleApi.ScanStatus   status;
     do
     {
         status = BleApi.PollDevice(ref device, false);
         if (status == BleApi.ScanStatus.AVAILABLE)
         {
             Debug.Log("Found device: " + device.name + " | " + device.id);
             if (device.name.Equals(DEVICE_NAME, StringComparison.OrdinalIgnoreCase))
             {
                 /* TrueTouch found, scan for the desired service UUID */
                 BleApi.StopDeviceScan();
                 truetouchDevice = device;
                 BleApi.ScanServices(truetouchDevice.id);
                 state = BleState.CONNECTING;
                 Debug.Log("Scanning TrueTouch's services");
                 return;
             }
         }
         else if (status == BleApi.ScanStatus.FINISHED)
         {
             /* Ran out of devices without finding target device */
             errorString = "Could not find TrueTouch";
             state       = BleState.ERROR;
         }
     } while (status == BleApi.ScanStatus.AVAILABLE);
 }
Пример #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;
         }
     }
 }