Ejemplo n.º 1
0
 private void handleBleConnecting()
 {
     BleApi.Service    service = new BleApi.Service();
     BleApi.ScanStatus status;
     do
     {
         status = BleApi.PollService(out service, false);
         if (status == BleApi.ScanStatus.AVAILABLE)
         {
             Debug.Log("Found service: " + service.uuid);
             string uuid = service.uuid.Replace("{", null).Replace("}", null);
             if (uuid.Equals(SERVICE_UUID, StringComparison.OrdinalIgnoreCase))
             {
                 /* Service found, subscribe to updates from TX char */
                 BleApi.SubscribeCharacteristic(truetouchDevice.id, SERVICE_UUID, TX_CHAR_UUID, false);
                 state = BleState.CONNECTED;
                 Debug.Log("Connected to TrueTouch");
                 return;
             }
         }
         else if (status == BleApi.ScanStatus.FINISHED)
         {
             /* Couldn't find target service on the device */
             errorString = "Could not find NUS service on TrueTouch";
             state       = BleState.ERROR;
         }
     } while (status == BleApi.ScanStatus.AVAILABLE);
 }
Ejemplo n.º 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;
         }
     }
 }