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 } }
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); }
/** 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); }
private void handleBleIdle() { /* Entering here must mean an error happened; just try to restart */ BleApi.StartDeviceScan(); state = BleState.SCANNING; Debug.Log("Restart scanning for devices"); }
// Start is called before the first frame update void Start() { /* Start scanning */ BleApi.StartDeviceScan(); state = BleState.SCANNING; Debug.Log("Scanning for devices"); InvokeRepeating("executeHandUpdate", SendRateSeconds, SendRateSeconds); }
public void StartCharacteristicScan() { if (!isScanningCharacteristics) { // start new scan characteristicDropdown.ClearOptions(); BleApi.ScanCharacteristics(selectedDeviceId, selectedServiceId); isScanningCharacteristics = true; characteristicScanStatusText.text = "scanning"; characteristicScanButton.interactable = false; } }
public void StartServiceScan() { if (!isScanningServices) { // start new scan serviceDropdown.ClearOptions(); BleApi.ScanServices(selectedDeviceId); isScanningServices = true; serviceScanStatusText.text = "scanning"; serviceScanButton.interactable = false; } }
public void StartStopDeviceScan() { if (!isScanningDevices) { // start new scan for (int i = scanResultRoot.childCount - 1; i >= 0; i--) { Destroy(scanResultRoot.GetChild(i).gameObject); } BleApi.StartDeviceScan(); isScanningDevices = true; deviceScanButtonText.text = "Stop scan"; deviceScanStatusText.text = "scanning"; } else { // stop scan isScanningDevices = false; BleApi.StopDeviceScan(); deviceScanButtonText.text = "Start scan"; deviceScanStatusText.text = "stopped"; } }
// 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; } } }
public void Subscribe() { // no error code available in non-blocking mode BleApi.SubscribeCharacteristic(selectedDeviceId, selectedServiceId, selectedCharacteristicId, false); isSubscribed = true; }
private void OnApplicationQuit() { BleApi.Quit(); }