public void Disconnect() { if (Ble != null) { //unsubscribe Ble.ConnectionStatusChanged -= StatusChanged; //sources(services) AudioLevelSource.Dispose(); AccGyroMagSource.Dispose(); ActivityRecognitionSource.Dispose(); BatteryStatusSource.Dispose(); CarryPositionSource.Dispose(); CompassSource.Dispose(); EnvironmentalSource.Dispose(); GestureRecognitionSource.Dispose(); LEDStateConfigSource.Dispose(); LightSensorSource.Dispose(); OrientationSource.Dispose(); ProximitySource.Dispose(); SmokeSensorSource.Dispose(); LEDStateConfigSource.Dispose(); UltraVioletSource.Dispose(); //device Ble.Dispose(); GC.Collect(); GC.WaitForPendingFinalizers(); Ble = null; } }
public async Task Connect() { Ble = await BluetoothLEDevice.FromBluetoothAddressAsync(BluetoothAddress); if (Ble != null) { Ble.ConnectionStatusChanged += StatusChanged; var servicesResult = await Ble.GetGattServicesAsync(BluetoothCacheMode.Uncached); var services = servicesResult.Services.ToList(); foreach (var service in services) { var charsResult = await service.GetCharacteristicsAsync(BluetoothCacheMode.Uncached); var characteristics = charsResult.Characteristics.ToList(); foreach (var characteristic in characteristics) { if (Dictionaries.Sensors.ContainsValue(characteristic.Uuid)) { var kvp = Dictionaries.Sensors.First(x => x.Value == characteristic.Uuid); SetSource(kvp.Key, characteristic); } } } } }
// Start is called before the first frame update void Start() { Ble.Initialize(() => { Debug.Log("initialized"); Ble.StartScan(new string[] { SERVICE_UUID }, (identifier, name, rssi, manufactureData) => { targetIdentifier = identifier; Ble.ConnectToPeripheral(identifier, null, null, (deviceIdentifier, serviceUUID, characteristicUUID) => { if (characteristicUUID == ID_UUID) { Ble.SubscribeCharacteristic(targetIdentifier, serviceUUID, characteristicUUID, (name2, name3, bytedata) => { Debug.Log(BitConverter.ToString(bytedata)); }); } }, null); }); }, (error) => { Debug.LogErrorFormat("error = {0}", error); }); }
private void SendAll_Click(object sender, RoutedEventArgs e) { foreach (MyUserControl1 mc in Txs.Children) { Standart.Instance.AddLogTx(mc.ValueID, mc.ValueData); Ble.AddToTransmit(mc.ValueID, mc.ValueData); } }
// This is the main entry point of the application. static void Main(string[] args) { Ble.DontThrow(); // if you want to use a different Application Delegate class from "AppDelegate" // you can specify it here. UIApplication.Main(args, null, "AppDelegate"); }
// Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0) && targetIdentifier != null) { byte[] data = buildMoveData(30, 30, 500); Ble.WriteCharacteristic(targetIdentifier, SERVICE_UUID, MOTOR_UUID, data, data.Length, false, null); } }
public void Execute() { byte[] disconnect = Ble.Lib.BLECommandConnectionDisconnect(this.Connection); Ble.SendCommand(Port, disconnect); byte[] stopScan = Ble.Lib.BLECommandGAPEndProcedure(); Ble.SendCommand(Port, stopScan); byte[] stopAdvertise = Ble.Lib.BLECommandGAPSetMode(0, 0); Ble.SendCommand(Port, stopAdvertise); }
private void BleItem_Tapped(object sender, TappedRoutedEventArgs e) { Ble.RestoreBle(); BleLog.Text = String.Empty; if (Ble.DemoMode) { BleDemoConnect(((Button)sender).Tag as String); } else { BleConnectAsync(((Button)sender).Tag as String); } }
public void Execute() { Byte[] writeToAttr = Ble.Lib.BLECommandATTClientAttributeWrite(this.Connection, this.AttributeHandle, (byte[])this.Value); ProcedureCompletedEventHandler handler = this.ProcedureCompletedHandler; Ble.Lib.BLEEventATTClientProcedureCompleted += handler; Ble.SendCommand(this.Port, writeToAttr); this.WaitEvent(() => _response != null); Ble.Lib.BLEEventATTClientProcedureCompleted -= handler; }
public StatusEventArgs Execute() { Byte[] connect = Ble.Lib.BLECommandGAPConnectDirect((byte[])this.ServiceUUID, this.Address, 0x20, 0x30, 0x100, 0); StatusEventHandler handler = this.FindConnection; Ble.Lib.BLEEventConnectionStatus += handler; Ble.SendCommand(this.Port, connect); this.WaitEvent(() => _connectionResponse != null); Ble.Lib.BLEEventConnectionStatus -= handler; // not needed anymore return(_connectionResponse); }
public AttributeValueEventArgs Execute() { byte[] readAttr = Ble.Lib.BLECommandATTClientReadByHandle(this.Connection, this.AttributeHandle); AttributeValueEventHandler handler = this.VerifyAttribute; Ble.Lib.BLEEventATTClientAttributeValue += handler; Ble.SendCommand(this.Port, readAttr); this.WaitEvent(() => _attrValueResponse != null); Ble.Lib.BLEEventATTClientAttributeValue -= handler; return(_attrValueResponse); }
public ScanResponseEventArgs Execute() { // Send discover command byte[] scanParams = Ble.Lib.BLECommandGAPSetScanParameters(0xC8, 0xC8, 1); byte[] scanDiscover = Ble.Lib.BLECommandGAPDiscover(1); ScanResponseEventHandler handler = this.FindService; Ble.Lib.BLEEventGAPScanResponse += handler; Ble.SendCommand(this.Port, scanParams); Ble.SendCommand(this.Port, scanDiscover); this.WaitEvent(() => _scanResponse != null); Ble.Lib.BLEEventGAPScanResponse -= handler; // not needed anymore return(_scanResponse); }
private void FindServices() { // (established connection, handle.min_address, handle.max_address, primary gatt service attribute identifier) byte[] readGroups = Ble.Lib.BLECommandATTClientReadByGroupType(this.Connection, GATT_MIN_HANDLE, GATT_MAX_HANDLE, (byte[])GATT_SERVICE_TYPE_PRIMARY); Ble.Lib.BLEEventATTClientGroupFound += GroupFound; ProcedureCompletedEventArgs procedureResponse = null; ProcedureCompletedEventHandler handler = (sender, e) => { procedureResponse = e; }; Ble.Lib.BLEEventATTClientProcedureCompleted += handler; Ble.SendCommand(this.Port, readGroups); this.WaitEvent(() => procedureResponse != null); Ble.Lib.BLEEventATTClientGroupFound -= GroupFound; Ble.Lib.BLEEventATTClientProcedureCompleted -= handler; }
private void FindAttributes(BlePeripheralService service) { byte[] findInfo = Ble.Lib.BLECommandATTClientFindInformation(this.Connection, service.StartHandle, service.EndHandle); FindInformationFoundEventHandler infoHandler = (sender, e) => this.AttributeFound(service, e); Ble.Lib.BLEEventATTClientFindInformationFound += infoHandler; ProcedureCompletedEventArgs procedureResponse = null; ProcedureCompletedEventHandler handler = (sender, e) => { procedureResponse = e; }; Ble.Lib.BLEEventATTClientProcedureCompleted += handler; Ble.SendCommand(this.Port, findInfo); this.WaitEvent(() => procedureResponse != null); Ble.Lib.BLEEventATTClientFindInformationFound -= infoHandler; Ble.Lib.BLEEventATTClientProcedureCompleted -= handler; }
private async void InformationService_LoadCharacteristics() { IReadOnlyList <GattCharacteristic> characteristics = null; try { if (await Ble.InformationService.RequestAccessAsync() == DeviceAccessStatus.Allowed) { GattCharacteristicsResult result = await Ble.InformationService.GetCharacteristicsAsync(BluetoothCacheMode.Uncached); if (result.Status == GattCommunicationStatus.Success) { characteristics = result.Characteristics; } else { Debug.WriteLine("Error accessing service."); } } else { Debug.WriteLine("Error accessing service."); } } catch (Exception ex) { Debug.WriteLine("Restricted service. Can't read characteristics: " + ex.Message); } if (characteristics.Count > 0) { //https://gist.github.com/brayden-morris-303/09a738ed9c83a7d14c82#our-own-useless-little-timer-example-real for (int i = 0; i < characteristics.Count; i++) { BleLog.Text += i.ToString() + ": " + characteristics[i].Uuid.ToString() + "\n"; Byte[] b = characteristics[i].Uuid.ToByteArray(); if (b[1] == 0x2A && b[2] == 0 && b[3] == 0 && b[4] == 0 && b[5] == 0) { switch (b[0]) { case 0x27: Ble.MyHWCharact = characteristics[i]; break; case 0x28: Ble.MySWCharact = characteristics[i]; break; } } } String s = String.Empty; String str = String.Empty; if (Ble.MyHWCharact != null) { s = Ble.ReadCharactAsString(Ble.MyHWCharact).Result; if (s != String.Empty) { str += "Аппаратная версия: " + s; } } if (Ble.MySWCharact != null) { s = Ble.ReadCharactAsString(Ble.MySWCharact).Result; if (s != String.Empty) { str += "Программная версия: " + s; } } } }
private int EmulateInstruction(Instruction instruction, int i) { switch (instruction.OpCode.Code) { case Code.UNKNOWN1: throw new NotSupportedException(); case Code.UNKNOWN2: throw new NotSupportedException(); case Code.Add: Add.Emulate(ValueStack); break; case Code.Add_Ovf: case Code.Add_Ovf_Un: Add.Emulate_Ovf(ValueStack); break; case Code.And: And.Emulate(ValueStack); break; case Code.Arglist: throw new NotSupportedException(); case Code.Beq: case Code.Beq_S: var beqResult = Beq.Emulate(ValueStack, instruction, _instructionsToEmulate); return(beqResult == -1 ? i : beqResult); case Code.Bge: case Code.Bge_S: case Code.Bge_Un: case Code.Bge_Un_S: var bgeResult = Bge.Emulate(ValueStack, instruction, _instructionsToEmulate); return(bgeResult == -1 ? i : bgeResult); case Code.Bgt: case Code.Bgt_S: case Code.Bgt_Un: case Code.Bgt_Un_S: var bgtResult = Bgt.Emulate(ValueStack, instruction, _instructionsToEmulate); return(bgtResult == -1 ? i : bgtResult); case Code.Ble: case Code.Ble_S: case Code.Ble_Un: case Code.Ble_Un_S: var bleResult = Ble.Emulate(ValueStack, instruction, _instructionsToEmulate); return(bleResult == -1 ? i : bleResult); case Code.Blt: case Code.Blt_S: case Code.Blt_Un: case Code.Blt_Un_S: var bltResult = Blt.Emulate(ValueStack, instruction, _instructionsToEmulate); return(bltResult == -1 ? i : bltResult); case Code.Bne_Un: case Code.Bne_Un_S: var bneResult = Bne.Emulate(ValueStack, instruction, _instructionsToEmulate); return(bneResult == -1 ? i : bneResult); case Code.Box: Box.Emulate_Box(ValueStack, instruction); break; case Code.Br: case Code.Br_S: return(Br.Emulate(ValueStack, instruction, _instructionsToEmulate)); case Code.Break: break; case Code.Brfalse: case Code.Brfalse_S: var bFalseResult = BrFalse.Emulate(ValueStack, instruction, _instructionsToEmulate); return(bFalseResult == -1 ? i : bFalseResult); case Code.Brtrue: case Code.Brtrue_S: var bTrueResult = BrTrue.Emulate(ValueStack, instruction, _instructionsToEmulate); return(bTrueResult == -1 ? i : bTrueResult); case Code.Call: Call.Emulate(ValueStack, instruction, _method); break; case Code.Calli: throw new NotSupportedException(); case Code.Callvirt: CallVirt.Emulate(ValueStack, instruction, _method); break; case Code.Castclass: CastClass.Emulate(ValueStack, instruction); break; case Code.Ceq: Ceq.Emulate(ValueStack); break; case Code.Cgt: case Code.Cgt_Un: Ceq.Emulate(ValueStack); break; case Code.Ckfinite: throw new NotSupportedException(); case Code.Clt: case Code.Clt_Un: Clt.Emulate(ValueStack); break; case Code.Constrained: throw new NotSupportedException(); case Code.Conv_I: ConvI.Emulation(ValueStack); break; case Code.Conv_I1: throw new NotSupportedException(); case Code.Conv_I2: ConvI2.Emulation(ValueStack); break; case Code.Conv_I4: ConvI4.Emulation(ValueStack); break; case Code.Conv_I8: ConvI8.Emulation(ValueStack); break; case Code.Conv_Ovf_I: throw new NotSupportedException(); case Code.Conv_Ovf_I_Un: throw new NotSupportedException(); case Code.Conv_Ovf_I1: throw new NotSupportedException(); case Code.Conv_Ovf_I1_Un: throw new NotSupportedException(); case Code.Conv_Ovf_I2: throw new NotSupportedException(); case Code.Conv_Ovf_I2_Un: throw new NotSupportedException(); case Code.Conv_Ovf_I4: throw new NotSupportedException(); case Code.Conv_Ovf_I4_Un: throw new NotSupportedException(); case Code.Conv_Ovf_I8: throw new NotSupportedException(); case Code.Conv_Ovf_I8_Un: throw new NotSupportedException(); case Code.Conv_Ovf_U: throw new NotSupportedException(); case Code.Conv_Ovf_U_Un: throw new NotSupportedException(); case Code.Conv_Ovf_U1: throw new NotSupportedException(); case Code.Conv_Ovf_U1_Un: throw new NotSupportedException(); case Code.Conv_Ovf_U2: throw new NotSupportedException(); case Code.Conv_Ovf_U2_Un: throw new NotSupportedException(); case Code.Conv_Ovf_U4: throw new NotSupportedException(); case Code.Conv_Ovf_U4_Un: throw new NotSupportedException(); case Code.Conv_Ovf_U8: throw new NotSupportedException(); case Code.Conv_Ovf_U8_Un: throw new NotSupportedException(); case Code.Conv_R_Un: throw new NotSupportedException(); case Code.Conv_R4: throw new NotSupportedException(); case Code.Conv_R8: throw new NotSupportedException(); case Code.Conv_U: ConvI.UEmulation(ValueStack); break; case Code.Conv_U1: ConvI1.UEmulation(ValueStack); break; case Code.Conv_U2: ConvI2.UEmulation(ValueStack); break; case Code.Conv_U4: ConvI4.UEmulation(ValueStack); break; case Code.Conv_U8: ConvI8.UEmulation(ValueStack); break; case Code.Cpblk: throw new NotSupportedException(); case Code.Cpobj: throw new NotSupportedException(); case Code.Div: Div.Emulate(ValueStack); break; case Code.Div_Un: Div.Emulate_Un(ValueStack); break; case Code.Dup: ValueStack.CallStack.Push(ValueStack.CallStack.Peek()); break; case Code.Endfilter: throw new NotSupportedException(); case Code.Endfinally: throw new NotSupportedException(); case Code.Initblk: throw new NotSupportedException(); case Code.Initobj: ValueStack.CallStack.Pop(); break; case Code.Isinst: IsInst.Emulate_Box(ValueStack, instruction); break; case Code.Jmp: throw new NotSupportedException(); case Code.Ldarg: case Code.Ldarg_0: case Code.Ldarg_1: case Code.Ldarg_2: case Code.Ldarg_3: case Code.Ldarg_S: Ldarg.Emulate(ValueStack, instruction, _method); break; case Code.Ldarga: throw new NotSupportedException(); case Code.Ldarga_S: throw new NotSupportedException(); case Code.Ldc_I4: case Code.Ldc_I4_0: case Code.Ldc_I4_1: case Code.Ldc_I4_2: case Code.Ldc_I4_3: case Code.Ldc_I4_4: case Code.Ldc_I4_5: case Code.Ldc_I4_6: case Code.Ldc_I4_7: case Code.Ldc_I4_8: ValueStack.CallStack.Push(instruction.GetLdcI4Value()); break; case Code.Ldc_I4_M1: ValueStack.CallStack.Push(-1); break; case Code.Ldc_I4_S: ValueStack.CallStack.Push((sbyte)instruction.GetLdcI4Value()); break; case Code.Ldc_I8: ValueStack.CallStack.Push((long)instruction.Operand); break; case Code.Ldc_R4: ValueStack.CallStack.Push((float)instruction.GetOperand()); break; case Code.Ldc_R8: ValueStack.CallStack.Push((double)instruction.GetOperand()); break; case Code.Ldelem: throw new NotSupportedException(); case Code.Ldelem_I: throw new NotSupportedException(); case Code.Ldelem_I1: throw new NotSupportedException(); case Code.Ldelem_I2: throw new NotSupportedException(); case Code.Ldelem_I4: throw new NotSupportedException(); case Code.Ldelem_I8: throw new NotSupportedException(); case Code.Ldelem_R4: throw new NotSupportedException(); case Code.Ldelem_R8: throw new NotSupportedException(); case Code.Ldelem_Ref: LdelemRef.Emulate(ValueStack); break; case Code.Ldelem_U1: LdelemI1.UEmulate(ValueStack); break; case Code.Ldelem_U2: throw new NotSupportedException(); case Code.Ldelem_U4: LdelemI4.UEmulate(ValueStack); break; case Code.Ldelema: Ldelema.Emulate(ValueStack); break; case Code.Ldfld: throw new NotSupportedException(); case Code.Ldflda: throw new NotSupportedException(); case Code.Ldftn: throw new NotSupportedException(); case Code.Ldind_I: throw new NotSupportedException(); case Code.Ldind_I1: throw new NotSupportedException(); case Code.Ldind_I2: throw new NotSupportedException(); case Code.Ldind_I4: LdindI4.Emulate(ValueStack); break; case Code.Ldind_I8: throw new NotSupportedException(); case Code.Ldind_R4: throw new NotSupportedException(); case Code.Ldind_R8: throw new NotSupportedException(); case Code.Ldind_Ref: throw new NotSupportedException(); case Code.Ldind_U1: LdindI1.UEmulate(ValueStack); break; case Code.Ldind_U2: LdindI2.UEmulate(ValueStack); break; case Code.Ldind_U4: LdindI4.UEmulate(ValueStack); break; case Code.Ldlen: Ldlen.Emulate(ValueStack); break; case Code.Ldloc: case Code.Ldloc_0: case Code.Ldloc_1: case Code.Ldloc_2: case Code.Ldloc_3: case Code.Ldloc_S: Ldloc.Emulate(ValueStack, instruction, _method); break; case Code.Ldloca: case Code.Ldloca_S: Ldloc.EmulateLdloca(ValueStack, instruction, _method); break; case Code.Ldnull: ValueStack.CallStack.Push(null); break; case Code.Ldobj: Ldobj.Emulate(ValueStack, instruction); break; case Code.Ldsfld: Ldsfld.Emulate(ValueStack, instruction); break; case Code.Ldsflda: throw new NotSupportedException(); case Code.Ldstr: ValueStack.CallStack.Push(instruction.Operand.ToString()); break; case Code.Ldtoken: Ldtoken.Emulate(ValueStack, instruction); break; case Code.Ldvirtftn: throw new NotSupportedException(); case Code.Leave: case Code.Leave_S: return(LEave.Emulate(ValueStack, instruction, _instructionsToEmulate)); case Code.Localloc: Localloc.Emulate(ValueStack); break; case Code.Mkrefany: throw new NotSupportedException(); case Code.Mul: case Code.Mul_Ovf: case Code.Mul_Ovf_Un: Mul.Emulate(ValueStack); break; case Code.Neg: ValueStack.CallStack.Push(-ValueStack.CallStack.Pop()); break; case Code.Newarr: NewArr.Emulate(ValueStack, instruction); break; case Code.Newobj: throw new NotSupportedException(); case Code.Nop: break; case Code.Not: Not.Emulate(ValueStack); break; case Code.Or: Or.Emulate(ValueStack); break; case Code.Pop: ValueStack.CallStack.Pop(); break; case Code.Prefix1: throw new NotSupportedException(); case Code.Prefix2: throw new NotSupportedException(); case Code.Prefix3: throw new NotSupportedException(); case Code.Prefix4: throw new NotSupportedException(); case Code.Prefix5: throw new NotSupportedException(); case Code.Prefix6: throw new NotSupportedException(); case Code.Prefix7: throw new NotSupportedException(); case Code.Prefixref: throw new NotSupportedException(); case Code.Readonly: throw new NotSupportedException(); case Code.Refanytype: throw new NotSupportedException(); case Code.Refanyval: throw new NotSupportedException(); case Code.Rem: Rem.Emulate(ValueStack); break; case Code.Rem_Un: Rem.Emulate_Un(ValueStack); break; case Code.Ret: break; case Code.Rethrow: throw new NotSupportedException(); case Code.Shl: Shl.Emulate(ValueStack); break; case Code.Shr: Shr.Emulate(ValueStack); break; case Code.Shr_Un: Shr.Emulate_Un(ValueStack); break; case Code.Sizeof: throw new NotSupportedException(); case Code.Starg: case Code.Starg_S: Starg.Emulate(ValueStack, instruction, _method); break; case Code.Stelem: throw new NotSupportedException(); case Code.Stelem_I: throw new NotSupportedException(); case Code.Stelem_I1: StelemI1.Emulate(ValueStack); break; case Code.Stelem_I2: break; case Code.Stelem_I4: Stelem_I4.Emulate(ValueStack); break; case Code.Stelem_I8: throw new NotSupportedException(); case Code.Stelem_R4: throw new NotSupportedException(); case Code.Stelem_R8: throw new NotSupportedException(); case Code.Stelem_Ref: throw new NotSupportedException(); case Code.Stfld: throw new NotSupportedException(); case Code.Stind_I: throw new NotSupportedException(); case Code.Stind_I1: StindI1.Emulate(ValueStack); break; case Code.Stind_I2: throw new NotSupportedException(); case Code.Stind_I4: StindI4.Emulate(ValueStack); break; case Code.Stind_I8: throw new NotSupportedException(); case Code.Stind_R4: throw new NotSupportedException(); case Code.Stind_R8: throw new NotSupportedException(); case Code.Stind_Ref: throw new NotSupportedException(); case Code.Stloc: case Code.Stloc_0: case Code.Stloc_1: case Code.Stloc_2: case Code.Stloc_3: case Code.Stloc_S: Stloc.Emulate(ValueStack, instruction, _method); break; case Code.Stobj: Stobj.Emulate(ValueStack, instruction); break; case Code.Stsfld: Stsfld.Emulate(ValueStack, instruction); break; case Code.Sub: Sub.Emulate(ValueStack); break; case Code.Sub_Ovf: case Code.Sub_Ovf_Un: Sub.Emulate_Ovf(ValueStack); break; case Code.Switch: var switchResult = Switch.Emulate(ValueStack, instruction, _instructionsToEmulate); return(switchResult == -1 ? i : switchResult); case Code.Tailcall: throw new NotSupportedException(); case Code.Throw: throw new NotSupportedException(); case Code.Unaligned: throw new NotSupportedException(); case Code.Unbox: throw new NotSupportedException(); case Code.Unbox_Any: // Box.Emulate_UnBox_Any(ValueStack,instruction); break; case Code.Volatile: throw new NotSupportedException(); case Code.Xor: Xor.Emulate(ValueStack); break; default: throw new ArgumentOutOfRangeException(); } return(i); }
private void ButtonSend_Tapped(object sender, TappedRoutedEventArgs e) { Standart.Instance.AddLogTx(ValueID, ValueData); Ble.AddToTransmit(ValueID, ValueData); }