/******************* * Tool response events */////////////////// private void ProcedureCompleteEvent(object sender, ProcedureCompletedEventArgs e) { Console.WriteLine(String.Format("Got response from tool {0}", e.result)); // //TODO remove the 1,5. I just made that up //2,3 doesn't error //We can track globally for getting shit done, or per tool //use e.connection. That is the key for the dictionary if (connState == BluetoothState.STATE_FINDING_SERVICES) { Byte[] cmd = bglib.BLECommandATTClientFindInformation(e.connection, 2, 3); bglib.SendCommand(serialAPI, cmd); Console.WriteLine("getting attributes"); } if (connState == BluetoothState.STATE_FINDING_ATTRIBUTES) { //TODO replace e.connection with the tool connection. pull tool out based on e.connection //Also the payload has to mean something, like identify tool Byte[] payload = { 0x01, 0x01, 0x01, 0xA0, 0x23, 0x01, 0x00, 0xc7 }; Byte[] cmd = bglib.BLECommandATTClientAttributeWrite(e.connection, 0x000F, payload); bglib.SendCommand(serialAPI, cmd); Console.WriteLine("Wrote data to attribute"); connState = BluetoothState.STATE_SENDING_COMMAND; } }
private void BLEPerformNextTask(BLEPeripheral peripheral) { // Find all attributes if (peripheral.Attributes.Count() == 0) { ushort start = 0x0001; ushort end = 0xFFFF; cmd = bglib.BLECommandATTClientFindInformation(peripheral.Connection, start, end); MessageWriter.LogWrite("ble_cmd_att_client_find_information: ", string.Format("connection={0}, start={1}, end={2}", peripheral.Connection, start, end)); bglib.SendCommand(SelectedPort, cmd); } // Have attributes been found? else if (peripheral.Services.Count() == 0) { // Perform service discovery ushort start = 0x0001; ushort end = 0xFFFF; byte[] primaryService = new byte[] { 0x00, 0x28 }; cmd = bglib.BLECommandATTClientReadByGroupType(peripheral.Connection, start, end, primaryService); MessageWriter.LogWrite("ble_cmd_att_client_read_by_group_type: ", string.Format("connection={0}, start={1}, end={2}, uuid={3}", peripheral.Connection, start, end, BitConverter.ToString(primaryService))); bglib.SendCommand(SelectedPort, cmd); } else if (!peripheral.Services.ContainsKey("CustomService")) { MessageWriter.LogWrite("Invalid device selected"); peripheral.Disconnect(); } else if (peripheral.attHandleCCC.Count > 0) { // Enable indications byte[] indications = new byte[] { 0x03, 0x00 }; byte[] cmd = bglib.BLECommandATTClientAttributeWrite(peripheral.Connection, peripheral.attHandleCCC.Peek(), indications); MessageWriter.LogWrite("ble_cmd_att_client_attribute_write: ", string.Format("connection={0}, att_handle={1}, data={2}", peripheral.Connection, peripheral.attHandleCCC.Dequeue(), BitConverter.ToString(indications))); bglib.SendCommand(SelectedPort, cmd); } // Is the low power mode state known? else if (peripheral.Characteristics.ContainsKey("LPM")) { if (peripheral.Characteristics["LPM"].ValueAttribute.Handle > 0 && peripheral.LowPowerMode == BLEPeripheral.LPM.Unknown) //TODO Low-Power mode key not found { // Check sleep mode cmd = bglib.BLECommandATTClientReadByHandle(peripheral.Connection, peripheral.Characteristics["LPM"].ValueAttribute.Handle); MessageWriter.LogWrite("ble_cmd_att_client_read_by_handle: ", string.Format("connection={0}, handle={1}", peripheral.Connection, peripheral.Characteristics["LPM"].ValueAttribute.Handle)); bglib.SendCommand(SelectedPort, cmd); } } }
public void ATTClientProcedureCompletedEvent(object sender, Bluegiga.BLE.Events.ATTClient.ProcedureCompletedEventArgs e) { String log = String.Format("ble_evt_attclient_procedure_completed: connection={0}, result={1}, chrhandle={2}" + Environment.NewLine, e.connection, e.result, e.chrhandle ); Console.Write(log); ThreadSafeDelegate(delegate { txtLog.AppendText(log); }); // check if we just finished searching for services if (app_state == STATE_FINDING_SERVICES) { if (att_handlesearch_end > 0) { //print "Found 'Heart Rate' service with UUID 0x180D" // found the Heart Rate service, so now search for the attributes inside Byte[] cmd = bglib.BLECommandATTClientFindInformation(e.connection, att_handlesearch_start, att_handlesearch_end); // DEBUG: display bytes written ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("=> TX ({0}) [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine); }); bglib.SendCommand(serialAPI, cmd); //while (bglib.IsBusy()) ; // update state app_state = STATE_FINDING_ATTRIBUTES; } else { ThreadSafeDelegate(delegate { txtLog.AppendText("Could not find 'Heart Rate' service with UUID 0x180D" + Environment.NewLine); }); } } // check if we just finished searching for attributes within the heart rate service else if (app_state == STATE_FINDING_ATTRIBUTES) { if (att_handle_measurement_ccc > 0) { //print "Found 'Heart Rate' measurement attribute with UUID 0x2A37" // found the measurement + client characteristic configuration, so enable notifications // (this is done by writing 0x01 to the client characteristic configuration attribute) Byte[] cmd = bglib.BLECommandATTClientAttributeWrite(e.connection, att_handle_measurement_ccc, new Byte[] { 0x01 }); // DEBUG: display bytes written ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("=> TX ({0}) [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine); }); bglib.SendCommand(serialAPI, cmd); //while (bglib.IsBusy()) ; // update state app_state = STATE_LISTENING_MEASUREMENTS; } else { ThreadSafeDelegate(delegate { txtLog.AppendText("Could not find 'Heart Rate' measurement attribute with UUID 0x2A37" + Environment.NewLine); }); } } }
public void ATTClientProcedureCompletedEvent(object sender, Bluegiga.BLE.Events.ATTClient.ProcedureCompletedEventArgs e) { String log = String.Format("ble_evt_attclient_procedure_completed: connection={0}, result={1}, chrhandle={2}" + Environment.NewLine, e.connection, e.result, e.chrhandle ); Console.Write(log); ThreadSafeDelegate(delegate { txtLog.AppendText(log); }); if (app_state == STATE_FINDING_SERVICES) { if (att_handlesearch_end > 0) { Byte[] cmd = bglib.BLECommandATTClientFindInformation(e.connection, att_handlesearch_start, att_handlesearch_end); #if PRINT_DEBUG ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("=> TX ({0}) [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine); }); #endif bglib.SendCommand(serialAPI, cmd); // update state app_state = STATE_FINDING_ATTRIBUTES; } else { ThreadSafeDelegate(delegate { txtLog.AppendText("Could not find service" + Environment.NewLine); }); } } else if (app_state == STATE_FINDING_ATTRIBUTES) { if (att_handle_measurement_ccc > 0) { Byte[] cmd = bglib.BLECommandATTClientAttributeWrite(e.connection, (UInt16)(att_handle_measurement + 0x01), new Byte[] { 0x01, 0x00 }); #if PRINT_DEBUG ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("=> TX ({0}) [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine); }); #endif bglib.SendCommand(serialAPI, cmd); Byte[] cmd2 = bglib.BLECommandATTClientAttributeWrite(e.connection, (UInt16)(att_handle_measurement_ccc + 0x01), new Byte[] { 0x01, 0x00 }); #if PRINT_DEBUG ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("=> TX ({0}) [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine); }); #endif bglib.SendCommand(serialAPI, cmd2); app_state = STATE_LISTENING_MEASUREMENTS; } else { ThreadSafeDelegate(delegate { txtLog.AppendText("Could not find 'Heart Rate' measurement attribute with UUID 0x2A37" + Environment.NewLine); }); } } }
/// <summary> /// The last started procedure has completed. E.g. we ended searching attr. or serv. /// </summary> /// <param name="sender"></param> /// <param name="e">ProcedureCompletedEventArgs</param> public void ATTClientProcedureCompletedEvent(object sender, Bluegiga.BLE.Events.ATTClient.ProcedureCompletedEventArgs e) { lock (o) { String log = String.Format("procedure_completed: connection={0}, result={1}, chrhandle={2}" + Environment.NewLine, e.connection, e.result, e.chrhandle ); Console.Write(log); ThreadSafeDelegate(delegate { txtLog.AppendText(log); }); Byte[] cmd; // check if we just finished searching for attr in services switch (app_state) { case STATE_SEARCH_PLATTFORM_SERVICE: // set to value when new service found if (att_handlesearch_end > 0) { // found a service, so now search for the attributes inside cmd = bglib.BLECommandATTClientFindInformation(e.connection, (UInt16)att_handlesearch_start, (UInt16)att_handlesearch_end); // Byte[] cmd = bglib.BLECommandATTClientReadByType(e.connection, (ushort)att_handlesearch_start, (ushort)att_handlesearch_end, new byte[] { 0x03, 0x28 }); ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("Sending find information command \n => Length: ({0}) Data: [ {1}]", cmd.Length, ByteArrayToHexString(cmd)) + Environment.NewLine); }); bglib.SendCommand(serialAPI, cmd); //while (bglib.IsBusy()) ; // update state app_state = STATE_SEARCH_ATTRIBUTES; att_handlesearch_start = 0; att_handlesearch_end = 0; } else { ThreadSafeDelegate(delegate { txtLog.AppendText("Could not find services" + Environment.NewLine); }); } break; // characteristics search completed -- send notification command to first char case STATE_SEARCH_ATTRIBUTES: // lasts ATTRccc val set in model - send notifications ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("Set STATE to SET_CCC \n ==> SET CCC Command Handle {0} ", tmpCharContainer1.Handle_ccc) + Environment.NewLine); }); app_state = STATE_SET_ATTRIBUTES_CCC; cmd = bglib.BLECommandATTClientAttributeWrite(e.connection, (UInt16)tmpCharContainer1.Handle_ccc, new Byte[] { 0x01 }); bglib.SendCommand(serialAPI, cmd); break; case STATE_SET_ATTRIBUTES_CCC: cmd = bglib.BLECommandATTClientAttributeWrite(e.connection, (UInt16)tmpCharContainer2.Handle_ccc, new Byte[] { 0x01 }); ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("STATE_SETATTR -- send last notify !!!!! to {0}", tmpCharContainer2.Handle_ccc) + Environment.NewLine); }); // we done notify for all characs app_state = STATE_LISTEN_MEASUREMENTS; readButton.Enabled = true; bglib.SendCommand(serialAPI, cmd); /* // set the state to measure when all sensors connected * if (att_Counter == SENSOR_COUNT) * { * app_state = STATE_LISTENING_MEASUREMENTS; * } */ break; case STATE_READ_ATTR_VAL: ThreadSafeDelegate(delegate { txtLog.AppendText(String.Format("Setting appstate back to READ Measure" + Environment.NewLine)); }); break; default: ThreadSafeDelegate(delegate { txtLog.AppendText("!!! STATE NOT FOUND" + Environment.NewLine); }); break; } } }
protected Byte[] NewFindInfoCmd(Byte connection) { return(bglib.BLECommandATTClientFindInformation(connection, att_handlesearch_start, att_handlesearch_end)); }
private void btnDiscoverService_Click(object sender, EventArgs e) { bglib.SendCommand(serialAPI, bglib.BLECommandATTClientFindInformation(0, 1, 65535)); }
private void ScanThread() { DateTime mTimStart = DateTime.Now; while (true) { switch (c_BleDev.State) { case GhpBle.ACTTION_IDLE: Thread.Sleep(50); break; #if false case GhpBle.ACTIION_ATTR_PAIR_CHECK: { mTimStart = DateTime.Now; while (c_BleDev.Busy) { TimeSpan mTimDif = DateTime.Now.Subtract(mTimStart); if (mTimDif.Seconds > 3) { break; } } c_BleDev.Busy = true; byte[] cmd = bglib.BLECommandATTClientReadByType(c_BleDev.ConnHandle, 0x0001, 0xFFFF, new byte[] { 0x03, 0x28 }); bglib.SendCommand(comDev, cmd); mTimStart = DateTime.Now; while (c_BleDev.Busy) { TimeSpan mTimDif = DateTime.Now.Subtract(mTimStart); if (mTimDif.Seconds > 3) { break; } } if (c_BleDev.NeedPair) { if (c_BleDev.Paired == false) { c_BleDev.Busy = true; byte[] cmd2 = bglib.BLECommandSMEncryptStart(c_BleDev.ConnHandle, 1); bglib.SendCommand(comDev, cmd2); mTimStart = DateTime.Now; while (c_BleDev.Paired == false) { TimeSpan mTimDif = DateTime.Now.Subtract(mTimStart); if (mTimDif.Seconds > 3) { break; } } } else { c_BleDev.State = GhpBle.ACTIONN_ATTR_PAIR_DONE; } } else { c_BleDev.State = GhpBle.ACTIONN_ATTR_PAIR_DONE; } } break; case GhpBle.ACTIONN_ATTR_PAIR_DONE: if (c_BleDev.NeedPair && c_BleDev.Paired) { c_BleDev.State = GhpBle.ACTTION_SCAN_PRIMSRV; } break; #endif case GhpBle.ACTTION_SCAN_PRIMSRV: { c_BleDev.Busy = true; Byte[] cmd = bglib.BLECommandATTClientReadByGroupType(c_BleDev.ConnHandle, 0x0001, 0xFFFF, new Byte[] { 0x00, 0x28 }); bglib.SendCommand(comDev, cmd); mTimStart = DateTime.Now; while (c_BleDev.Busy) { TimeSpan mTimDif = DateTime.Now.Subtract(mTimStart); if (mTimDif.Seconds > 10) { break; } } c_BleDev.State = GhpBle.ACTTION_SCAN_PRIMSRV_DONE; } break; case GhpBle.ACTTION_SCAN_PRIMSRV_DONE: break; case GhpBle.ACTTION_SCAN_ATTRIB: if (c_BleDev.CurrentPrimSrv != null) { if (c_BleDev.CurrentPrimSrv.AttScanDone == false) { // //scan all attribute first. // c_BleDev.Busy = true; Byte[] cmd = bglib.BLECommandATTClientFindInformation(c_BleDev.ConnHandle, c_BleDev.CurrentPrimSrv.Start, c_BleDev.CurrentPrimSrv.End); bglib.SendCommand(comDev, cmd); mTimStart = DateTime.Now; while (c_BleDev.Busy) { TimeSpan mTimDif = DateTime.Now.Subtract(mTimStart); if (mTimDif.Seconds > 20) { break; } } // //Now read user description of each attribute; // foreach (CAttribute attr in c_BleDev.CurrentPrimSrv.AttrList) { c_BleDev.Busy = true; c_BleDev.AttReadDone = false; cmd = bglib.BLECommandATTClientReadByHandle(c_BleDev.ConnHandle, attr.UserDescHandle); bglib.SendCommand(comDev, cmd); while (c_BleDev.Busy && c_BleDev.AttReadDone == false) { } if (c_BleDev.AttReadDone == true) { attr.AttName = Encoding.UTF8.GetString(c_BleDev.AttReadValue); } } c_BleDev.CurrentPrimSrv.AttScanDone = true; } } c_BleDev.State = GhpBle.ACTTION_SCAN_ATTRIB_DONE; break; case GhpBle.ACTTION_SCAN_ATTRIB_DONE: break; case GhpBle.ACTTION_ATTR_READ: while (c_BleDev.Busy == false && c_BleDev.AttReadDone == false) { ; } c_BleDev.State = GhpBle.ACTIONN_ATTR_READ_DONE; break; case GhpBle.ACTIONN_ATTR_READ_DONE: break; case GhpBle.ACTTION_ATTR_WRITE: break; default: break; } } }