private void btCalibMicMaskSet_Click(object sender, EventArgs e) { UInt16 micMask = 0; UInt16 attrID; if (cbCalibMicIL.Checked) { micMask |= 0x01; } if (cbCalibMicIR.Checked) { micMask |= 0x02; } if (cbCalibMicOL.Checked) { micMask |= 0x04; } if (cbCalibMicOR.Checked) { micMask |= 0x08; } attrID = m_ParentFrm.c_BleDev.AttrHandleByUUID(DatConvert.ByteArrayToHexString(UUID_MicMask)); if (attrID != CAttribute.InvalidHandle) { byte[] data = new byte[2] { (byte)micMask, (byte)(micMask >> 8) }; Byte[] cmd = m_ParentFrm.bglib.BLECommandATTClientAttributeWrite(m_ParentFrm.c_BleDev.ConnHandle, attrID, data); m_ParentFrm.SendBleCmd(cmd); } }
private void btCalibFreqGet_Click(object sender, EventArgs e) { UInt16 attrID; UInt32 freq; string str = string.Empty; DateTime tStart = DateTime.Now; TimeSpan ts = DateTime.Now.Subtract(tStart); attrID = m_ParentFrm.c_BleDev.AttrHandleByUUID(DatConvert.ByteArrayToHexString(UUID_Freq)); if (attrID != CAttribute.InvalidHandle) { Byte[] cmd = m_ParentFrm.bglib.BLECommandATTClientReadByHandle(m_ParentFrm.c_BleDev.ConnHandle, attrID); m_ParentFrm.BleDoReadAction(); m_ParentFrm.SendBleCmd(cmd); while (m_ParentFrm.BleIsReadDone() == false) { Application.DoEvents(); ts = DateTime.Now.Subtract(tStart); if (ts.Seconds > 1) { break; } } if (ts.Seconds <= 1) { freq = System.BitConverter.ToUInt32(m_ParentFrm.c_BleDev.AttReadValue, 0); tbCalibFreqGet.Text = freq.ToString(); } } }
private void listScanDev_DoubleClick(object sender, EventArgs e) { if (comDev.IsOpen == false) { MessageBox.Show("请先打开串口", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (listScanDev.SelectedIndices != null && listScanDev.SelectedItems.Count > 0) { c_BleDev.Reset(); c_BleDev.MacAddr = listScanDev.SelectedItems[0].SubItems[2].Text; if (listScanDev.SelectedItems[0].SubItems[3].Text == "Random") { c_BleDev.AddrType = 1; } else { c_BleDev.AddrType = 0; } c_BleDev.DevName = listScanDev.SelectedItems[0].SubItems[0].Text; stsLb_ConnSts.Text = listScanDev.SelectedItems[0].SubItems[0].Text; stsLb_ConnMac.Text = "MacAddr=" + listScanDev.SelectedItems[0].SubItems[2].Text; Byte[] cmd = bglib.BLECommandGAPConnectDirect(DatConvert.strToHexByte(c_BleDev.MacAddr), c_BleDev.AddrType, 0x20, 0x30, 0x100, 0); bglib.SendCommand(comDev, cmd);// 125ms interval, 125ms window, active scanning tvSrvTree.Nodes.Clear(); } }
private void btTonePlyStsGet_Click(object sender, EventArgs e) { UInt16 attrID; byte sts; DateTime tStart = DateTime.Now; TimeSpan ts = DateTime.Now.Subtract(tStart); attrID = m_ParentFrm.c_BleDev.AttrHandleByUUID(DatConvert.ByteArrayToHexString(UUID_Status)); if (attrID != CAttribute.InvalidHandle) { Byte[] cmd = m_ParentFrm.bglib.BLECommandATTClientReadByHandle(m_ParentFrm.c_BleDev.ConnHandle, attrID); m_ParentFrm.BleDoReadAction(); m_ParentFrm.SendBleCmd(cmd); while (m_ParentFrm.BleIsReadDone() == false) { Application.DoEvents(); ts = DateTime.Now.Subtract(tStart); if (ts.Seconds > 1) { break; } } if (ts.Seconds <= 1) { sts = m_ParentFrm.c_BleDev.AttReadValue[0]; tbTonePlyStsGet.Text = sts.ToString(); } } }
private void btCalibSpkSensGet_Click(object sender, EventArgs e) { UInt16 attrID; Single leftSpk, rightSpk; DateTime tStart = DateTime.Now; TimeSpan ts = DateTime.Now.Subtract(tStart); attrID = m_ParentFrm.c_BleDev.AttrHandleByUUID(DatConvert.ByteArrayToHexString(UUID_SpkSens)); if (attrID != CAttribute.InvalidHandle) { Byte[] cmd = m_ParentFrm.bglib.BLECommandATTClientReadByHandle(m_ParentFrm.c_BleDev.ConnHandle, attrID); m_ParentFrm.BleDoReadAction(); m_ParentFrm.SendBleCmd(cmd); while (m_ParentFrm.BleIsReadDone() == false) { Application.DoEvents(); ts = DateTime.Now.Subtract(tStart); if (ts.Seconds > 1) { break; } } if (ts.Seconds <= 1) { leftSpk = BitConverter.ToSingle(m_ParentFrm.c_BleDev.AttReadValue, 0); rightSpk = BitConverter.ToSingle(m_ParentFrm.c_BleDev.AttReadValue, 4); tbCalibSpkSensLeftGet.Text = leftSpk.ToString(); tbCalibSpkSensRightGet.Text = rightSpk.ToString(); } } }
public CPrimService(byte[] uuid, string name) { string uuidstr = DatConvert.ByteArrayToDecString(uuid); Description = name; UUID = uuidstr; AttScanDone = false; }
private void btStrCvt_Click(object sender, EventArgs e) { byte[] hexDat = DatConvert.strToHexByte(tbAttrGet.Text); switch (cmbGetType.SelectedIndex) { case 0://Hex tbAttrGetCvt.Text = DatConvert.ByteArrayToHexString(hexDat); break; case 1://Number { if (cmbGetEndian.SelectedIndex == 1) { Array.Reverse(hexDat); } switch (cmbGetFormat.SelectedIndex) { case 0: //INT8 case 1: //UINT8 tbAttrGetCvt.Text = BitConverter.ToChar(hexDat, 0).ToString(); break; case 2: //INT16; tbAttrGetCvt.Text = BitConverter.ToInt16(hexDat, 0).ToString(); //Int16.Parse(tbAttrGet.Text).ToString(); break; case 3: //UINT16 tbAttrGetCvt.Text = BitConverter.ToUInt16(hexDat, 0).ToString(); // UInt16.Parse(tbAttrGet.Text).ToString(); //tbAttrGet.Text = ByteArrayToDecString(e.value); break; case 4: //INT32; tbAttrGetCvt.Text = BitConverter.ToInt32(hexDat, 0).ToString(); break; case 5: //UINT32; tbAttrGetCvt.Text = BitConverter.ToUInt32(hexDat, 0).ToString(); break; case 6: //float tbAttrGetCvt.Text = BitConverter.ToSingle(hexDat, 0).ToString(); break; default: break; } } break; case 2://String tbAttrGetCvt.Text = DatConvert.HexArrayToString(hexDat); break; } }
public void DelAttribute(byte[] uuid) { string uuidstr = DatConvert.HexArrayToString(uuid); foreach (CAttribute item in AttrList.ToArray()) { if (uuidstr == item.AttUUID) { AttrList.Remove(item); } } }
private void btTonePlyStop_Click(object sender, EventArgs e) { UInt16 attrID; attrID = m_ParentFrm.c_BleDev.AttrHandleByUUID(DatConvert.ByteArrayToHexString(UUID_Status)); if (attrID != CAttribute.InvalidHandle) { byte[] data = new byte[1] { 0 }; Byte[] cmd = m_ParentFrm.bglib.BLECommandATTClientAttributeWrite(m_ParentFrm.c_BleDev.ConnHandle, attrID, data); m_ParentFrm.SendBleCmd(cmd); } }
private void timEvent_Tick(object sender, EventArgs e) { if (m_ActionDone == true) { m_ActionDone = false; lvPersistent.Items.Clear(); foreach (CPersistent item in m_PersistentList) { ListViewItem lv = new ListViewItem("0x" + item.Key.ToString("X4")); //[0]; lv.SubItems.Add(DatConvert.ByteArrayToHexString(item.Value)); //[1] lvPersistent.Items.Add(lv); } lvPersistent.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); } }
public CAttribute GetAttribute(byte[] uuid) { CAttribute mAttribute = null; string uuidstr = DatConvert.HexArrayToString(uuid); foreach (CAttribute item in AttrList.ToArray()) { if (uuidstr == item.AttUUID) { mAttribute = item; break; } } return(mAttribute); }
private void btAttrRead_Click(object sender, EventArgs e) { DateTime tStart = DateTime.Now; TimeSpan ts = DateTime.Now.Subtract(tStart); if (comDev.IsOpen == false) { MessageBox.Show("请先打开串口", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (c_BleDev.ConnHandle == CAttribute.InvalidHandle) { return; } if (tvSrvTree.SelectedNode.Level != 1) { return; } string uuidstr = (string)tvSrvTree.SelectedNode.Tag; ushort attrID = c_BleDev.AttrHandleByUUID(uuidstr); if (attrID == CAttribute.InvalidHandle) { return; } Byte[] cmd = bglib.BLECommandATTClientReadByHandle(c_BleDev.ConnHandle, attrID); BleDoReadAction(); SendBleCmd(cmd); while (BleIsReadDone() == false) { Application.DoEvents(); ts = DateTime.Now.Subtract(tStart); if (ts.Seconds > 1) { break; } } if (ts.Seconds <= 1) { tbAttrGet.Text = DatConvert.ByteArrayToHexString(c_BleDev.AttReadValue); btStrCvt_Click(sender, e); } }
private void btCalibFreqSet_Click(object sender, EventArgs e) { UInt16 attrID; UInt32 freq; byte[] data; string str = string.Empty; attrID = m_ParentFrm.c_BleDev.AttrHandleByUUID(DatConvert.ByteArrayToHexString(UUID_Freq)); if (attrID == CAttribute.InvalidHandle) { return; } freq = UInt32.Parse(tbCalibFreqSet.Text); data = BitConverter.GetBytes(freq); Byte[] cmd = m_ParentFrm.bglib.BLECommandATTClientAttributeWrite(m_ParentFrm.c_BleDev.ConnHandle, attrID, data); m_ParentFrm.SendBleCmd(cmd); }
private void btTonePlyCfgSet_Click(object sender, EventArgs e) { UInt16 attrID, freq; byte spl; attrID = m_ParentFrm.c_BleDev.AttrHandleByUUID(DatConvert.ByteArrayToHexString(UUID_Config)); if (attrID != CAttribute.InvalidHandle) { byte[] data = new byte[3]; spl = byte.Parse(tbTonePlySplSet.Text); freq = UInt16.Parse(tbTonePlyFreqSet.Text); data[0] = spl; BitConverter.GetBytes(freq).CopyTo(data, 1); Byte[] cmd = m_ParentFrm.bglib.BLECommandATTClientAttributeWrite(m_ParentFrm.c_BleDev.ConnHandle, attrID, data); m_ParentFrm.SendBleCmd(cmd); } }
private void btCalibSpkSensSet_Click(object sender, EventArgs e) { UInt16 attrID; float leftSpk, rightSpk; byte[] data = new byte[8]; attrID = m_ParentFrm.c_BleDev.AttrHandleByUUID(DatConvert.ByteArrayToHexString(UUID_SpkSens)); if (attrID == CAttribute.InvalidHandle) { return; } leftSpk = float.Parse(tbCalibSpkSensLeftSet.Text); rightSpk = float.Parse(tbCalibSpkSensRightSet.Text); BitConverter.GetBytes(leftSpk).CopyTo(data, 0); BitConverter.GetBytes(rightSpk).CopyTo(data, 4); Byte[] cmd = m_ParentFrm.bglib.BLECommandATTClientAttributeWrite(m_ParentFrm.c_BleDev.ConnHandle, attrID, data); m_ParentFrm.SendBleCmd(cmd); }
private void btCalibRspGet_Click(object sender, EventArgs e) { UInt16 attrID, micMask; Single sensitivity; byte sts; DateTime tStart = DateTime.Now; TimeSpan ts = DateTime.Now.Subtract(tStart); attrID = m_ParentFrm.c_BleDev.AttrHandleByUUID(DatConvert.ByteArrayToHexString(UUID_Response)); if (attrID != CAttribute.InvalidHandle) { Byte[] cmd = m_ParentFrm.bglib.BLECommandATTClientReadByHandle(m_ParentFrm.c_BleDev.ConnHandle, attrID); m_ParentFrm.BleDoReadAction(); m_ParentFrm.SendBleCmd(cmd); while (m_ParentFrm.BleIsReadDone() == false) { Application.DoEvents(); ts = DateTime.Now.Subtract(tStart); if (ts.Seconds > 1) { break; } } if (ts.Seconds <= 1) { sts = m_ParentFrm.c_BleDev.AttReadValue[0]; micMask = BitConverter.ToUInt16(m_ParentFrm.c_BleDev.AttReadValue, 1); sensitivity = BitConverter.ToSingle(m_ParentFrm.c_BleDev.AttReadValue, 3); tbCalibRspResult.Text = CvtCalibStatusToString(sts); tbCalibRspMicMask.Text = CvtMicMaskToString(micMask); tbCalibRspSensitivity.Text = sensitivity.ToString(); } } }
private void btCalibConstGet_Click(object sender, EventArgs e) { UInt16 attrID; Single valIL, valIR, valOL, valOR;; DateTime tStart = DateTime.Now; TimeSpan ts = DateTime.Now.Subtract(tStart); attrID = m_ParentFrm.c_BleDev.AttrHandleByUUID(DatConvert.ByteArrayToHexString(UUID_CalibConst)); if (attrID != CAttribute.InvalidHandle) { Byte[] cmd = m_ParentFrm.bglib.BLECommandATTClientReadByHandle(m_ParentFrm.c_BleDev.ConnHandle, attrID); m_ParentFrm.BleDoReadAction(); m_ParentFrm.SendBleCmd(cmd); while (m_ParentFrm.BleIsReadDone() == false) { Application.DoEvents(); ts = DateTime.Now.Subtract(tStart); if (ts.Seconds > 1) { break; } } if (ts.Seconds <= 1) { valIL = BitConverter.ToSingle(m_ParentFrm.c_BleDev.AttReadValue, 0); valIR = BitConverter.ToSingle(m_ParentFrm.c_BleDev.AttReadValue, 4); valOL = BitConverter.ToSingle(m_ParentFrm.c_BleDev.AttReadValue, 8); valOR = BitConverter.ToSingle(m_ParentFrm.c_BleDev.AttReadValue, 12); tbCalibConstGetIL.Text = valIL.ToString(); tbCalibConstGetIR.Text = valIR.ToString(); tbCalibConstGetOL.Text = valOL.ToString(); tbCalibConstGetOR.Text = valOR.ToString(); } } }
public string ServiceNameByUUID(string uuidstr) { byte[] uuid = DatConvert.strToHexByte(uuidstr); return(ServiceNameByUUID(uuid)); }
public CPrimService GetPrimSrv(byte[] uuid) { string uuidstr = DatConvert.ByteArrayToHexString(uuid); return(GetPrimSrv(uuidstr)); }
public void PrimSrvAdd(byte[] uuid, ushort start, ushort end) { string uuidstr = DatConvert.ByteArrayToHexString(uuid); PrimSrvAdd(uuidstr, start, end); }
// //查找子服务的应答事件,从中提取子服务内容; // public void EventFindInformationFound(object sender, Bluegiga.BLE.Events.ATTClient.FindInformationFoundEventArgs e) { //ThreadSafeDelegate(delegate { txtLog.AppendText(log); }); if (e.connection == c_BleDev.ConnHandle) { if (c_BleDev.IsDeclarePrimaryService(e.uuid))//e.uuid.SequenceEqual(new Byte[] { 0x00, 0x28 })) { } else if (c_BleDev.IsDeclareAttribute(e.uuid))//e.uuid.SequenceEqual(new Byte[] { 0x03, 0x28 })) { } else if (c_BleDev.IsDescClientConfig(e.uuid)) {//ClientCharacteristicConfiguration } else if (c_BleDev.IsDescUserAttribute(e.uuid)) {//Characteristic User Description if (c_BleDev.CurrentPrimSrv.AttrList.Count > 0) { c_BleDev.CurrentPrimSrv.AttrList[c_BleDev.CurrentPrimSrv.AttrList.Count - 1].UserDescHandle = e.chrhandle; } } else {//charactestic attribute if (c_BleDev.CurrentPrimSrv != null) { c_BleDev.CurrentPrimSrv.AddAttribute(c_BleDev.CurrentPrimSrv.UUID, DatConvert.ByteArrayToHexString(e.uuid), "", e.chrhandle); } } } }
//Adevertising search event. // for master/scanner devices, the "gap_scan_response" event is a common entry-like point // this filters ad packets and display it into listview. // // //Search Event // for master/scanner devices, the "gap_scan_response" event is a common entry-like point // this filters ad packets to find devices which advertise the Health Thermometer service // This is a scan response event. This event is normally received by a Master which is scanning for advertisement and // scan response packets from Slaves. // Data Fiels: // [4]=int8, rssi; RSSI value(dBm), range=[-103 ~ -38]; // [5]=uint8,packet_type; Scan response header; // 0: Connectable Advertisement packet. // 2: Non Connectable Advertisement packet. // 4: Scan response packet. // 6: Discoverable advertisement packet. // [6~11]:bd_addr; Advertisers Bluetooth address. // [12]:uint8,address_type; Advertiser address type, // 1: random address; 0: public address // [13]:uint8,bond; Bond handle if there is known bond for this device, 0xff otherwise. // [14]:uint8 array, data; Scan response data. public void EventDevScanResponse(object sender, Bluegiga.BLE.Events.GAP.ScanResponseEventArgs e) { string mName = "(NoName)"; // pull all advertised service info from ad packet //List<Byte[]> ad_services = new List<Byte[]>(); Byte[] this_field = { }; int bytes_left = 0; int field_offset = 0; for (int i = 0; i < e.data.Length; i++) { //提取设备名称; if (bytes_left == 0) { bytes_left = e.data[i]; this_field = new Byte[e.data[i]]; field_offset = i + 1; } else { this_field[i - field_offset] = e.data[i]; bytes_left--; if (bytes_left == 0) { #if false if (this_field[0] == 0x02 || this_field[0] == 0x03) {// partial or complete list of 16-bit UUIDs ad_services.Add(this_field.Skip(1).Take(2).Reverse().ToArray()); } else if (this_field[0] == 0x04 || this_field[0] == 0x05) {// partial or complete list of 32-bit UUIDs ad_services.Add(this_field.Skip(1).Take(4).Reverse().ToArray()); } else if (this_field[0] == 0x06 || this_field[0] == 0x07) {// partial or complete list of 128-bit UUIDs ad_services.Add(this_field.Skip(1).Take(16).Reverse().ToArray()); } else if (this_field[0] == 0x09) {//Full name mName = System.Text.Encoding.ASCII.GetString(this_field.Skip(1).ToArray()); } #else if (this_field[0] == 0x09) {//Full name mName = System.Text.Encoding.ASCII.GetString(this_field.Skip(1).ToArray()); } #endif } } } //添加扫描信息到列表中; int rssi = Math.Abs(e.rssi); string macaddr = DatConvert.ByteArrayToHexString(e.sender); ThreadSafeDelegate(delegate { // //check whether this dev is already displayed in the list. //if it is not, we add it to the listbox,otherwise, just update. // ListViewItem lvS = listScanDev.FindItemWithText(macaddr);//查找列表中是否有重复设备; if (lvS != null) { lvS.SubItems[0].Text = mName; lvS.SubItems[1].Text = rssi.ToString(); lvS.SubItems[2].Text = DatConvert.ByteArrayToHexString(e.sender); } else { ListViewItem lv = new ListViewItem(mName); //[0]; lv.SubItems.Add(rssi.ToString()); //[1] lv.SubItems.Add(DatConvert.ByteArrayToHexString(e.sender)); //[2] lv.SubItems[2].Tag = e.bond; if (e.address_type == 1) { lv.SubItems.Add("Random"); //[3] } else { lv.SubItems.Add("Public"); //[3] } lv.SubItems.Add(e.bond.ToString()); //[4] lv.SubItems.Add(e.packet_type.ToString()); //[4]; listScanDev.Items.Add(lv); } }); }
private void btAttrWrite_Click(object sender, EventArgs e) { if (comDev.IsOpen == false) { MessageBox.Show("请先打开串口", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (c_BleDev.ConnHandle == CAttribute.InvalidHandle) { return; } if (tvSrvTree.SelectedNode.Level != 1) { return; } string uuidstr = (string)tvSrvTree.SelectedNode.Tag; ushort attrID = c_BleDev.AttrHandleByUUID(uuidstr); if (attrID == CAttribute.InvalidHandle) { return; } List <byte> byteList = new List <byte>(); switch (cmbSetType.SelectedIndex) { case 0://hex { byte[] v = DatConvert.strToHexByte(tbAttrSet.Text); byteList.AddRange(v); } break; case 1://Number switch (cmbSetFormat.SelectedIndex) { case 0: //I8 case 1: //U8 byteList.Add(byte.Parse(tbAttrSet.Text)); break; case 2://I16 { Int16 val = Int16.Parse(tbAttrSet.Text); byte[] b = BitConverter.GetBytes(val); byteList.AddRange(b); } break; case 3://U16 { UInt16 val = UInt16.Parse(tbAttrSet.Text); byte[] b = BitConverter.GetBytes(val); byteList.AddRange(b); } break; case 4://I32 { Int32 val = Int32.Parse(tbAttrSet.Text); byte[] b = BitConverter.GetBytes(val); byteList.AddRange(b); } break; case 5://U32 { UInt32 val = UInt32.Parse(tbAttrSet.Text); byte[] b = BitConverter.GetBytes(val); byteList.AddRange(b); } break; case 6://F32 { float val = float.Parse(tbAttrSet.Text); byte[] b = BitConverter.GetBytes(val); byteList.AddRange(b); } break; default: break; } break; case 2://String { byte[] v = System.Text.Encoding.Default.GetBytes(tbAttrSet.Text); byteList.AddRange(v); } break; } Byte[] cmd = bglib.BLECommandATTClientAttributeWrite(c_BleDev.ConnHandle, attrID, byteList.ToArray()); bglib.SendCommand(comDev, cmd); }