MainWindow.xaml.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /* MainWindow.xaml.cs/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro). */
  2. /* This copyright was auto-generated on Wed, Sep 1, 2021 5:05:38 PM */
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.ComponentModel;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Navigation;
  20. using System.Windows.Shapes;
  21. using Windows.Devices.Bluetooth;
  22. using Windows.Devices.Bluetooth.GenericAttributeProfile;
  23. using Windows.Devices.Enumeration;
  24. using Windows.Media.MediaProperties;
  25. using Windows.Storage.Streams;
  26. namespace GoProCSharpSample
  27. {
  28. /// <summary>
  29. /// Interaction logic for MainWindow.xaml
  30. /// </summary>
  31. public partial class MainWindow : Window, INotifyPropertyChanged
  32. {
  33. public class GDeviceInformation
  34. {
  35. public GDeviceInformation(DeviceInformation inDeviceInformation, bool inPresent, bool inConnected)
  36. {
  37. DeviceInfo = inDeviceInformation;
  38. IsPresent = inPresent;
  39. IsConnected = inConnected;
  40. }
  41. public DeviceInformation DeviceInfo { get; set; } = null;
  42. public bool IsPresent { get; set; } = false;
  43. public bool IsConnected { get; set; } = false;
  44. public bool IsVisible { get { return IsPresent || IsConnected; } }
  45. private GDeviceInformation() { }
  46. }
  47. #region Binded Properties
  48. public ObservableCollection<GDeviceInformation> Devices
  49. {
  50. get; set;
  51. } = new ObservableCollection<GDeviceInformation>();
  52. private bool mEncoding = false;
  53. public bool Encoding
  54. {
  55. get
  56. {
  57. return mEncoding;
  58. }
  59. set
  60. {
  61. mEncoding = value;
  62. if (this.PropertyChanged != null)
  63. {
  64. PropertyChanged(this, new PropertyChangedEventArgs("Encoding"));
  65. }
  66. }
  67. }
  68. private int mBatterylevel = 0;
  69. public int BatteryLevel
  70. {
  71. get
  72. {
  73. return mBatterylevel;
  74. }
  75. set
  76. {
  77. mBatterylevel = value;
  78. if (this.PropertyChanged != null)
  79. {
  80. PropertyChanged(this, new PropertyChangedEventArgs("BatteryLevel"));
  81. }
  82. }
  83. }
  84. private bool mWifiOn = false;
  85. public bool WifiOn
  86. {
  87. get
  88. {
  89. return mWifiOn;
  90. }
  91. set
  92. {
  93. mWifiOn = value;
  94. if (this.PropertyChanged != null)
  95. {
  96. PropertyChanged(this, new PropertyChangedEventArgs("WifiOn"));
  97. }
  98. }
  99. }
  100. #endregion
  101. #region Bluetooth Device Members
  102. private BluetoothLEDevice mBLED = null;
  103. public GattCharacteristic mNotifyCmds = null;
  104. public GattCharacteristic mSendCmds = null;
  105. public GattCharacteristic mSetSettings = null;
  106. public GattCharacteristic mNotifySettings = null;
  107. public GattCharacteristic mSendQueries = null;
  108. public GattCharacteristic mNotifyQueryResp = null;
  109. public GattCharacteristic mReadAPName = null;
  110. public GattCharacteristic mReadAPPass = null;
  111. #endregion
  112. public event PropertyChangedEventHandler PropertyChanged;
  113. DeviceWatcher mDeviceWatcher = null;
  114. private readonly Dictionary<string, DeviceInformation> mAllDevices = new Dictionary<string, DeviceInformation>();
  115. public MainWindow()
  116. {
  117. InitializeComponent();
  118. WindowStartupLocation = WindowStartupLocation.CenterScreen;
  119. }
  120. #region Button Click Handlers
  121. private void BtnScanBLE_Click(object sender, RoutedEventArgs e)
  122. {
  123. string BLESelector = "System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\"";
  124. DeviceInformationKind deviceInformationKind = DeviceInformationKind.AssociationEndpoint;
  125. string[] requiredProperties = { "System.Devices.Aep.Bluetooth.Le.IsConnectable", "System.Devices.Aep.IsConnected" };
  126. mDeviceWatcher = DeviceInformation.CreateWatcher(BLESelector, requiredProperties, deviceInformationKind);
  127. mDeviceWatcher.Added += MDeviceWatcher_Added; ;
  128. mDeviceWatcher.Updated += MDeviceWatcher_Updated; ;
  129. mDeviceWatcher.Removed += MDeviceWatcher_Removed; ;
  130. mDeviceWatcher.EnumerationCompleted += MDeviceWatcher_EnumerationCompleted; ;
  131. mDeviceWatcher.Stopped += MDeviceWatcher_Stopped; ;
  132. this.txtStatusBar.Text = "Scanning for devices...";
  133. mDeviceWatcher.Start();
  134. }
  135. private async void BtnPair_Click(object sender, RoutedEventArgs e)
  136. {
  137. GDeviceInformation lDevice = (GDeviceInformation)lbDevices.SelectedItem;
  138. if (lDevice != null)
  139. {
  140. StatusOutput("Pairing started");
  141. mBLED = await BluetoothLEDevice.FromIdAsync(lDevice.DeviceInfo.Id);
  142. mBLED.DeviceInformation.Pairing.Custom.PairingRequested += Custom_PairingRequested;
  143. if (mBLED.DeviceInformation.Pairing.CanPair)
  144. {
  145. DevicePairingProtectionLevel dppl = mBLED.DeviceInformation.Pairing.ProtectionLevel;
  146. DevicePairingResult dpr = await mBLED.DeviceInformation.Pairing.Custom.PairAsync(DevicePairingKinds.ConfirmOnly, dppl);
  147. StatusOutput("Pairing result = " + dpr.Status.ToString());
  148. }
  149. else
  150. {
  151. StatusOutput("Pairing failed");
  152. }
  153. }
  154. else
  155. {
  156. StatusOutput("Select a device");
  157. }
  158. }
  159. private async void BtnConnect_Click(object sender, RoutedEventArgs e)
  160. {
  161. StatusOutput("Connecting...");
  162. GDeviceInformation mDI = (GDeviceInformation)lbDevices.SelectedItem;
  163. if (mDI == null)
  164. {
  165. StatusOutput("No device selected");
  166. return;
  167. }
  168. mBLED = await BluetoothLEDevice.FromIdAsync(mDI.DeviceInfo.Id);
  169. if(!mBLED.DeviceInformation.Pairing.IsPaired)
  170. {
  171. StatusOutput("Device not paired");
  172. return;
  173. }
  174. GattDeviceServicesResult result = await mBLED.GetGattServicesAsync();
  175. mBLED.ConnectionStatusChanged += MBLED_ConnectionStatusChanged;
  176. if (result.Status == GattCommunicationStatus.Success)
  177. {
  178. IReadOnlyList<GattDeviceService> services = result.Services;
  179. foreach (GattDeviceService gatt in services)
  180. {
  181. GattCharacteristicsResult res = await gatt.GetCharacteristicsAsync();
  182. if (res.Status == GattCommunicationStatus.Success)
  183. {
  184. IReadOnlyList<GattCharacteristic> characteristics = res.Characteristics;
  185. foreach (GattCharacteristic characteristic in characteristics)
  186. {
  187. GattCharacteristicProperties properties = characteristic.CharacteristicProperties;
  188. if (properties.HasFlag(GattCharacteristicProperties.Read))
  189. {
  190. // This characteristic supports reading from it.
  191. }
  192. if (properties.HasFlag(GattCharacteristicProperties.Write))
  193. {
  194. // This characteristic supports writing to it.
  195. }
  196. if (properties.HasFlag(GattCharacteristicProperties.Notify))
  197. {
  198. // This characteristic supports subscribing to notifications.
  199. }
  200. if (characteristic.Uuid.ToString() == "b5f90002-aa8d-11e3-9046-0002a5d5c51b")
  201. {
  202. mReadAPName = characteristic;
  203. }
  204. if (characteristic.Uuid.ToString() == "b5f90003-aa8d-11e3-9046-0002a5d5c51b")
  205. {
  206. mReadAPPass = characteristic;
  207. }
  208. if (characteristic.Uuid.ToString() == "b5f90072-aa8d-11e3-9046-0002a5d5c51b")
  209. {
  210. mSendCmds = characteristic;
  211. }
  212. if (characteristic.Uuid.ToString() == "b5f90073-aa8d-11e3-9046-0002a5d5c51b")
  213. {
  214. mNotifyCmds = characteristic;
  215. GattCommunicationStatus status = await mNotifyCmds.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
  216. if (status == GattCommunicationStatus.Success)
  217. {
  218. mNotifyCmds.ValueChanged += MNotifyCmds_ValueChanged;
  219. }
  220. else
  221. {
  222. //failure
  223. StatusOutput("Failed to attach notify cmd " + status);
  224. }
  225. }
  226. if (characteristic.Uuid.ToString() == "b5f90074-aa8d-11e3-9046-0002a5d5c51b")
  227. {
  228. mSetSettings = characteristic;
  229. }
  230. if (characteristic.Uuid.ToString() == "b5f90075-aa8d-11e3-9046-0002a5d5c51b")
  231. {
  232. mNotifySettings = characteristic;
  233. GattCommunicationStatus status = await mNotifySettings.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
  234. if (status == GattCommunicationStatus.Success)
  235. {
  236. mNotifySettings.ValueChanged += MNotifySettings_ValueChanged;
  237. }
  238. else
  239. {
  240. //failure
  241. StatusOutput("Failed to attach notify settings " + status);
  242. }
  243. }
  244. if (characteristic.Uuid.ToString() == "b5f90076-aa8d-11e3-9046-0002a5d5c51b")
  245. {
  246. mSendQueries = characteristic;
  247. }
  248. if (characteristic.Uuid.ToString() == "b5f90077-aa8d-11e3-9046-0002a5d5c51b")
  249. {
  250. mNotifyQueryResp = characteristic;
  251. GattCommunicationStatus status = await mNotifyQueryResp.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
  252. if (status == GattCommunicationStatus.Success)
  253. {
  254. mNotifyQueryResp.ValueChanged += MNotifyQueryResp_ValueChanged;
  255. if (mSendQueries != null)
  256. {
  257. //Register for settings and status updates
  258. DataWriter mm = new DataWriter();
  259. mm.WriteBytes(new byte[] { 1, 0x52 });
  260. GattCommunicationStatus gat = await mSendQueries.WriteValueAsync(mm.DetachBuffer());
  261. mm = new DataWriter();
  262. mm.WriteBytes(new byte[] { 1, 0x53 });
  263. gat = await mSendQueries.WriteValueAsync(mm.DetachBuffer());
  264. }
  265. else
  266. {
  267. StatusOutput("send queries was null!");
  268. }
  269. }
  270. else
  271. {
  272. //failure
  273. StatusOutput("Failed to attach notify query " + status);
  274. }
  275. }
  276. }
  277. }
  278. }
  279. SetThirdPartySource();
  280. }
  281. else if (result.Status == GattCommunicationStatus.Unreachable)
  282. {
  283. //couldn't find camera
  284. StatusOutput("Connection failed");
  285. }
  286. }
  287. private async void BtnReadAPName_Click(object sender, RoutedEventArgs e)
  288. {
  289. if (mReadAPName != null)
  290. {
  291. GattReadResult res = await mReadAPName.ReadValueAsync();
  292. if (res.Status == GattCommunicationStatus.Success)
  293. {
  294. DataReader dataReader = Windows.Storage.Streams.DataReader.FromBuffer(res.Value);
  295. string output = dataReader.ReadString(res.Value.Length);
  296. txtAPName.Text = output;
  297. }
  298. else
  299. {
  300. StatusOutput("Failed to read ap name");
  301. }
  302. }
  303. else
  304. {
  305. StatusOutput("Not connected");
  306. }
  307. }
  308. private async void BtnReadAPPass_Click(object sender, RoutedEventArgs e)
  309. {
  310. if (mReadAPPass != null)
  311. {
  312. GattReadResult res = await mReadAPPass.ReadValueAsync();
  313. if (res.Status == GattCommunicationStatus.Success)
  314. {
  315. DataReader dataReader = Windows.Storage.Streams.DataReader.FromBuffer(res.Value);
  316. string output = dataReader.ReadString(res.Value.Length);
  317. txtAPPassword.Text = output;
  318. }
  319. else
  320. {
  321. StatusOutput("Failed to read password");
  322. }
  323. }
  324. else
  325. {
  326. StatusOutput("Not connected");
  327. }
  328. }
  329. private void BtnTurnWifiOn_Click(object sender, RoutedEventArgs e)
  330. {
  331. TogglefWifiAP(1);
  332. }
  333. private void BtnTurnWifiOff_Click(object sender, RoutedEventArgs e)
  334. {
  335. TogglefWifiAP(0);
  336. }
  337. private void BtnShutterOn_Click(object sender, RoutedEventArgs e)
  338. {
  339. ToggleShutter(1);
  340. }
  341. private void BtnShutterOff_Click(object sender, RoutedEventArgs e)
  342. {
  343. ToggleShutter(0);
  344. }
  345. #endregion
  346. #region Device Watcher Event Handlers
  347. private void MDeviceWatcher_Stopped(DeviceWatcher sender, object args)
  348. {
  349. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  350. {
  351. this.txtStatusBar.Text = "Scan Stopped!";
  352. }));
  353. }
  354. private void MDeviceWatcher_EnumerationCompleted(DeviceWatcher sender, object args)
  355. {
  356. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  357. {
  358. this.txtStatusBar.Text = "Scan Complete";
  359. }));
  360. }
  361. private void MDeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args)
  362. {
  363. for (int i = 0; i < Devices.Count; i++)
  364. {
  365. if (Devices[i].DeviceInfo.Id == args.Id)
  366. {
  367. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  368. {
  369. Devices.RemoveAt(i);
  370. }));
  371. break;
  372. }
  373. }
  374. }
  375. private void MDeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args)
  376. {
  377. bool isPresent = false, isConnected = false, found = false;
  378. if (args.Properties.ContainsKey("System.Devices.Aep.Bluetooth.Le.IsConnectable"))
  379. {
  380. isPresent = (bool)args.Properties["System.Devices.Aep.Bluetooth.Le.IsConnectable"];
  381. }
  382. if (args.Properties.ContainsKey("System.Devices.Aep.IsConnected"))
  383. {
  384. isConnected = (bool)args.Properties["System.Devices.Aep.IsConnected"];
  385. }
  386. for (int i = 0; i < Devices.Count; i++)
  387. {
  388. if (Devices[i].DeviceInfo.Id == args.Id)
  389. {
  390. found = true;
  391. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  392. {
  393. Devices[i].DeviceInfo.Update(args);
  394. Devices[i].IsPresent = isPresent;
  395. Devices[i].IsConnected = isConnected;
  396. }));
  397. break;
  398. }
  399. }
  400. if(!found && (isPresent || isConnected))
  401. {
  402. if (mAllDevices.ContainsKey(args.Id))
  403. {
  404. mAllDevices[args.Id].Update(args);
  405. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  406. {
  407. Devices.Add(new GDeviceInformation(mAllDevices[args.Id], isPresent, isConnected));
  408. }));
  409. }
  410. }
  411. }
  412. private void MDeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args)
  413. {
  414. bool isPresent = false;
  415. bool isConnected = false;
  416. if (args.Properties.ContainsKey("System.Devices.Aep.Bluetooth.Le.IsConnectable"))
  417. {
  418. isPresent = (bool)args.Properties["System.Devices.Aep.Bluetooth.Le.IsConnectable"];
  419. }
  420. if (args.Properties.ContainsKey("System.Devices.Aep.IsConnected"))
  421. {
  422. isConnected = (bool)args.Properties["System.Devices.Aep.IsConnected"];
  423. }
  424. if (args.Name != "" && args.Name.Contains("GoPro"))
  425. {
  426. bool found = false;
  427. if (!mAllDevices.ContainsKey(args.Id))
  428. {
  429. mAllDevices.Add(args.Id, args);
  430. }
  431. for (int i = 0; i < Devices.Count; i++)
  432. {
  433. if (Devices[i].DeviceInfo.Id == args.Id)
  434. {
  435. found = true;
  436. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  437. {
  438. Devices[i].DeviceInfo = args;
  439. Devices[i].IsPresent = isPresent;
  440. Devices[i].IsConnected = isConnected;
  441. }));
  442. break;
  443. }
  444. }
  445. if (!found && (isPresent || isConnected))
  446. {
  447. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  448. {
  449. Devices.Add(new GDeviceInformation(args, isPresent, isConnected));
  450. }));
  451. }
  452. }
  453. }
  454. #endregion
  455. #region BLE Device Handlers
  456. private void MBLED_ConnectionStatusChanged(BluetoothLEDevice sender, object args)
  457. {
  458. if (sender.ConnectionStatus == BluetoothConnectionStatus.Connected)
  459. StatusOutput("CONNECTED");
  460. else
  461. StatusOutput("DISCONNECTED");
  462. }
  463. private void Custom_PairingRequested(DeviceInformationCustomPairing sender, DevicePairingRequestedEventArgs args)
  464. {
  465. StatusOutput("Pairing request...");
  466. args.Accept();
  467. }
  468. #endregion
  469. #region Gatt Characteristic Notification Handlers
  470. private readonly List<byte> mBufQ = new List<byte>();
  471. private int mExpectedLengthQ = 0;
  472. private void MNotifyQueryResp_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
  473. {
  474. var reader = DataReader.FromBuffer(args.CharacteristicValue);
  475. byte[] myBytes = new byte[reader.UnconsumedBufferLength];
  476. reader.ReadBytes(myBytes);
  477. int newLength = ReadBytesIntoBuffer(myBytes, mBufQ);
  478. if (newLength > 0)
  479. mExpectedLengthQ = newLength;
  480. if (mExpectedLengthQ == mBufQ.Count)
  481. {
  482. if ((mBufQ[0] == 0x53 || mBufQ[0] == 0x93) && mBufQ[1] == 0)
  483. {
  484. //status messages
  485. for (int k = 0; k < mBufQ.Count;)
  486. {
  487. if (mBufQ[k] == 10)
  488. {
  489. Encoding = mBufQ[k + 2] > 0;
  490. }
  491. if (mBufQ[k] == 70)
  492. {
  493. BatteryLevel = mBufQ[k + 2];
  494. }
  495. if(mBufQ[k] == 69)
  496. {
  497. WifiOn = mBufQ[k + 2] == 1;
  498. }
  499. k += 2 + mBufQ[k + 1];
  500. }
  501. }
  502. else
  503. {
  504. //Unhandled Query Message
  505. }
  506. mBufQ.Clear();
  507. mExpectedLengthQ = 0;
  508. }
  509. }
  510. private readonly List<byte> mBufSet = new List<byte>();
  511. private int mExpectedLengthSet = 0;
  512. private void MNotifySettings_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
  513. {
  514. var reader = DataReader.FromBuffer(args.CharacteristicValue);
  515. byte[] myBytes = new byte[reader.UnconsumedBufferLength];
  516. reader.ReadBytes(myBytes);
  517. int newLength = ReadBytesIntoBuffer(myBytes, mBufSet);
  518. if (newLength > 0)
  519. mExpectedLengthSet = newLength;
  520. if (mExpectedLengthSet == mBufSet.Count)
  521. {
  522. /*
  523. if (mBufSet[0] == 0xXX)
  524. {
  525. }
  526. */
  527. mBufSet.Clear();
  528. }
  529. }
  530. private readonly List<byte> mBufCmd = new List<byte>();
  531. private int mExpectedLengthCmd = 0;
  532. private void MNotifyCmds_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
  533. {
  534. var reader = DataReader.FromBuffer(args.CharacteristicValue);
  535. byte[] myBytes = new byte[reader.UnconsumedBufferLength];
  536. reader.ReadBytes(myBytes);
  537. int newLength = ReadBytesIntoBuffer(myBytes, mBufCmd);
  538. if (newLength > 0)
  539. mExpectedLengthCmd = newLength;
  540. if (mExpectedLengthCmd == mBufCmd.Count)
  541. {
  542. /*
  543. if (mBufCmd[0] == 0xXX)
  544. {
  545. }
  546. */
  547. mBufCmd.Clear();
  548. }
  549. }
  550. #endregion
  551. #region Private Helper Functions
  552. private async void SetThirdPartySource()
  553. {
  554. DataWriter mm = new DataWriter();
  555. mm.WriteBytes(new byte[] { 0x01, 0x50 });
  556. GattCommunicationStatus res = GattCommunicationStatus.Unreachable;
  557. if (mSendCmds != null)
  558. {
  559. res = await mSendCmds.WriteValueAsync(mm.DetachBuffer());
  560. }
  561. if (res != GattCommunicationStatus.Success && mSendCmds != null)
  562. {
  563. StatusOutput("Failed to set command source: " + res.ToString());
  564. }
  565. }
  566. private void StatusOutput(string status)
  567. {
  568. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  569. {
  570. this.txtStatusBar.Text = status;
  571. }));
  572. }
  573. private int ReadBytesIntoBuffer(byte[] bytes, List<byte> mBuf)
  574. {
  575. int returnLength = -1;
  576. int startbyte = 1;
  577. int theseBytes = bytes.Length;
  578. if ((bytes[0] & 32) > 0)
  579. {
  580. //extended 13 bit header
  581. startbyte = 2;
  582. int len = ((bytes[0] & 0xF) << 8) | bytes[1];
  583. returnLength = len;
  584. }
  585. else if ((bytes[0] & 64) > 0)
  586. {
  587. //extended 16 bit header
  588. startbyte = 3;
  589. int len = (bytes[1] << 8) | bytes[2];
  590. returnLength = len;
  591. }
  592. else if ((bytes[0] & 128) > 0)
  593. {
  594. //its a continuation packet
  595. }
  596. else
  597. {
  598. //8 bit header
  599. returnLength = bytes[0];
  600. }
  601. for (int k = startbyte; k < theseBytes; k++)
  602. mBuf.Add(bytes[k]);
  603. return returnLength;
  604. }
  605. private async void TogglefWifiAP(int onOff)
  606. {
  607. DataWriter mm = new DataWriter();
  608. mm.WriteBytes(new byte[] { 0x03, 0x17, 0x01, (byte)onOff });
  609. GattCommunicationStatus res = GattCommunicationStatus.Unreachable;
  610. if (onOff != 1 && onOff != 0)
  611. {
  612. res = GattCommunicationStatus.AccessDenied;
  613. }
  614. else if (mSendCmds != null)
  615. {
  616. res = await mSendCmds.WriteValueAsync(mm.DetachBuffer());
  617. }
  618. if (res != GattCommunicationStatus.Success)
  619. {
  620. StatusOutput("Failed to turn on wifi: " + res.ToString());
  621. }
  622. }
  623. private async void ToggleShutter(int onOff)
  624. {
  625. DataWriter mm = new DataWriter();
  626. mm.WriteBytes(new byte[] { 3, 1, 1, (byte)onOff });
  627. GattCommunicationStatus res = GattCommunicationStatus.Unreachable;
  628. if (onOff != 1 && onOff != 0)
  629. {
  630. res = GattCommunicationStatus.AccessDenied;
  631. }
  632. else if (mSendCmds != null)
  633. {
  634. res = await mSendCmds.WriteValueAsync(mm.DetachBuffer());
  635. }
  636. if (res != GattCommunicationStatus.Success)
  637. {
  638. StatusOutput("Failed to send shutter: " + res.ToString());
  639. }
  640. }
  641. #endregion
  642. }
  643. public class BrushBoolColorConverter : IValueConverter
  644. {
  645. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  646. {
  647. if (!(bool)value)
  648. {
  649. return new SolidColorBrush(Color.FromRgb(100, 100, 100));
  650. }
  651. return new SolidColorBrush(Color.FromRgb(255, 100, 100));
  652. }
  653. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  654. {
  655. throw new NotImplementedException();
  656. }
  657. }
  658. }