//---- internal static BluesoleilDeviceInfo CreateFromGivenAddress(BluetoothAddress addr, BluesoleilFactory factory) { UInt32 hDev; byte[] bd_addr = BluesoleilUtils.FromBluetoothAddress(addr); bool remembered; hDev = factory.Api.Btsdk_GetRemoteDeviceHandle(bd_addr); if (hDev != StackConsts.BTSDK_INVALID_HANDLE) { remembered = true; } else { // Presumably this means that the device isn't known. // Does this add it forever? -- no??? hDev = factory.Api.Btsdk_AddRemoteDevice(bd_addr); if (hDev == StackConsts.BTSDK_INVALID_HANDLE) { BluesoleilUtils.CheckAndThrow(BtSdkError.SDK_UNINIT, "Btsdk_Get/AddRemoteDevice"); } remembered = false; } var result = new BluesoleilDeviceInfo(hDev, factory); result._remembered = remembered; Debug.Assert(addr.Equals(result.DeviceAddress), "Address changed in create! was: " + addr + ", now: " + result.DeviceAddress); return(result); }
//---- internal void SdkInit() { bool init = _api.Btsdk_IsSDKInitialized(); bool conn = _api.Btsdk_IsServerConnected(); // Ok to call if '!init'? if (init && conn) { return; } BtSdkError ret = _api.Btsdk_Init(); //Debug.WriteLine("BlueSoleil Btsdk_Init: " + BluesoleilUtils.BtSdkErrorToString(ret)); BluesoleilUtils.CheckAndThrow(ret, "Btsdk_Init"); #if DEBUG // When Radio is Off, Init succeeds when called but IsSDKInitialized // still returns false! That persists until the radio's turned on. bool initAfter = _api.Btsdk_IsSDKInitialized(); bool connAfter = _api.Btsdk_IsServerConnected(); // Ok to call if '!init'? if (!initAfter || !connAfter) { bool ready = _api.Btsdk_IsBluetoothReady(); Debug.Assert(!ready, "Not init&&conn, but NOT ready..."); Debug.Assert(!init, "Not init&&conn, but NOT !init..."); Debug.Assert(conn, "Not init&&conn, but NOT conn..."); } #endif _needsDispose = true; }
//---- void ConnectRfcommPreAllocateComPort(UInt16 svcClass16, UInt32 hDev, out UInt32 hConn, out byte channel, out int comPort //, out UInt32 comSerialNum ) { UInt32 comSerialNum; // UInt32 comSerialNum0; BtSdkError ret; UInt32 osComPort; // const UInt32 UsageTypeConst = 1; const StackConsts.COMM_SET flags = StackConsts.COMM_SET.Record | StackConsts.COMM_SET.UsageType; const UInt16 BTSDK_CLS_SERIAL_PORT = 0x1101; const int Timeout = 2200; // comSerialNum0 = _factory.Api.Btsdk_GetASerialNum(); comSerialNum = comSerialNum0; Debug.Assert(comSerialNum != 0, "INFO comSerialNum == 0 wierd maybe???"); bool success = _factory.Api.Btsdk_PlugInVComm(comSerialNum, out osComPort, UsageTypeConst, flags, Timeout); Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Btsdk_GetASerialNum comSerialNum: {0}, Btsdk_PlugInVComm success: {1}, osComPort: {2}", comSerialNum0, success, osComPort)); if (!success) { BluesoleilUtils.CheckAndThrow(BtSdkError.OPERATION_FAILURE, "Btsdk_PlugInVComm"); } comPort = checked ((int)osComPort); ret = _factory.Api.Btsdk_InitCommObj(checked ((byte)osComPort), BTSDK_CLS_SERIAL_PORT); Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Btsdk_InitCommObj ret: {0}", ret)); BluesoleilUtils.CheckAndThrow(ret, "Btsdk_InitCommObj"); // bool connSuccess = false; try { var sppStru = new Structs.BtSdkSPPConnParamStru(osComPort); ret = _factory.Api.Btsdk_ConnectEx(hDev, svcClass16, ref sppStru, out hConn); Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "ret: {0}, hConn: 0x{1:X}", ret, hConn)); BluesoleilUtils.CheckAndThrow(ret, "Btsdk_ConnectEx"); _hDev = hDev; // channel = 0; // unknown Console.WriteLine("Connect remote SPP Service with local COM{0}\n", osComPort); connSuccess = true; } finally { if (!connSuccess) { FreeComIndex(_factory, comPort, comSerialNum0); } } }
//---- private void ConnectRfcomm(BluetoothEndPoint remoteEP, UInt32 hDev, out UInt32 hConn, out byte channel, out int comPort) { Structs.BtSdkAppExtSPPAttrStru sppAttr = new Structs.BtSdkAppExtSPPAttrStru(remoteEP); // Debug.WriteLine("Gonna Btsdk_ConnectAppExtSPPService with: " + sppAttr.ToString()); BtSdkError ret = _factory.Api.Btsdk_ConnectAppExtSPPService(hDev, ref sppAttr, out hConn); Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "ret: {0}, hConn: 0x{1:X}, with: {2}", ret, hConn, sppAttr.ToString())); _hDev = hDev; BluesoleilUtils.CheckAndThrow(ret, "Btsdk_ConnectAppExtSPPService"); // var comNumGetClientPort = _factory.Api.Btsdk_GetClientPort(hConn); Debug.WriteLine("comNumGetClientPort: " + comNumGetClientPort); // if (sppAttr.rf_svr_chnl != 0) { Debug.Assert(sppAttr.com_index != 0, "Argghhhh com_index is zero! (When rf_svr_chnl isn't)."); channel = sppAttr.rf_svr_chnl; comPort = sppAttr.com_index; } else { // Some profiles are handled specifically OBEX, etc etc // so they don't create a COM port when that Debug.Assert(sppAttr.com_index == 0, "Don't expect a COM port to be created in this (fail) case."); // // Connecting to SPP 0x1101 also returns no com port in the // struct but a COM port is connected for it. Btsdk_GetClientPort // DOES return the correct port see whether we're in that case. if (comNumGetClientPort != 0) { comPort = comNumGetClientPort; channel = 0; // Unknown! } else { // Highly likely an OPP/etc connection was made, and not a RFCOMM // connection, and thus no COM port we can use. :-( So fail! Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "BlueSoleil seems no RFCOMM connection made, closing. (channel: {0}, COM: {1})", sppAttr.rf_svr_chnl, sppAttr.com_index)); // (Note: Add a dummy record so RemoveLiveConnection works ok). int liveCountB = _factory.AddConnection(hConn, NullBluesoleilConnection.Instance); Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "BlueSoleilClient.Connect non-RFCOMM LiveConns count: {0}.", liveCountB)); var retD = _factory.Api.Btsdk_Disconnect(hConn); BluesoleilUtils.Assert(retD, "Close non-RFCOMM connection"); throw BluesoleilUtils.ErrorConnectIsNonRfcomm(); } } //System.Windows.Forms.MessageBox.Show("BlueSoleil created port: '" + comPort + "'."); }
internal void RegisterCallbacksOnce() { if (_inquiryResultIndFunc != null) { return; } BtSdkError ret; Structs.BtSdkCallbackStru val; // _inquiryResultIndFunc = _inquiryHandler.HandleInquiryResultInd; val = new Structs.BtSdkCallbackStru(_inquiryResultIndFunc); Debug.Assert(val._type == StackConsts.CallbackType.INQUIRY_RESULT_IND); ret = Api.Btsdk_RegisterCallback4ThirdParty(ref val); BluesoleilUtils.CheckAndThrow(ret, "Btsdk_RegisterCallback4ThirdParty"); // _inquiryCompleteIndFunc = HandleInquiryComplete; val = new Structs.BtSdkCallbackStru(_inquiryCompleteIndFunc); Debug.Assert(val._type == StackConsts.CallbackType.INQUIRY_COMPLETE_IND); ret = Api.Btsdk_RegisterCallback4ThirdParty(ref val); BluesoleilUtils.CheckAndThrow(ret, "Btsdk_RegisterCallback4ThirdParty"); // //---- _pinReqIndFunc = _sec.HandlePinReqInd; val = new Structs.BtSdkCallbackStru(_pinReqIndFunc); Debug.Assert(val._type == StackConsts.CallbackType.PIN_CODE_IND); ret = Api.Btsdk_RegisterCallback4ThirdParty(ref val); BluesoleilUtils.CheckAndThrow(ret, "Btsdk_RegisterCallback4ThirdParty"); // //---- _connectionEventIndFunc = _records.HandleConnectionEventInd; val = new Structs.BtSdkCallbackStru(_connectionEventIndFunc); Debug.Assert(val._type == StackConsts.CallbackType.CONNECTION_EVENT_IND); ret = Api.Btsdk_RegisterCallback4ThirdParty(ref val); BluesoleilUtils.CheckAndThrow(ret, "Btsdk_RegisterCallback4ThirdParty"); // _statusCallback = HandleReceiveBluetoothStatusInfo; ret = Api.Btsdk_RegisterGetStatusInfoCB4ThirdParty(ref _statusCallback); BluesoleilUtils.CheckAndThrow(ret, "Btsdk_RegisterGetStatusInfoCB4ThirdParty"); ret = Api.Btsdk_SetStatusInfoFlag(StackConsts.BTSDK_BLUETOOTH_STATUS_FLAG); BluesoleilUtils.CheckAndThrow(ret, "Btsdk_SetStatusInfoFlag"); }
internal IAsyncResult BeginInquiry(int maxDevices, TimeSpan inquiryLength, AsyncCallback callback, object state, BluetoothClient.LiveDiscoveryCallback liveDiscoHandler, object liveDiscoState, DiscoDevsParams args) { _fcty.SdkInit(); _fcty.RegisterCallbacksOnce(); // byte maxDurations; byte maxNum; CommonDiscoveryBluetoothClient.ConvertBthInquiryParams(maxDevices, inquiryLength, out maxNum, out maxDurations); return(BeginInquiry(maxDevices, inquiryLength, callback, state, liveDiscoHandler, liveDiscoState, delegate() { BtSdkError ret = _fcty.Api.Btsdk_StartDeviceDiscovery( AnyClass, maxNum, maxDurations); BluesoleilUtils.CheckAndThrow(ret, "Btsdk_StartDeviceDiscovery"); }, args)); }
void GetInfo(ref BluetoothAddress addr) { BtSdkError ret; var props = new Structs.BtSdkRemoteDevicePropertyStru(); ret = _factory.Api.Btsdk_GetRemoteDeviceProperty(_hDev, out props); BluesoleilUtils.CheckAndThrow(ret, "Btsdk_GetRemoteDeviceProperty"); // if ((props.mask & Structs.BtSdkRemoteDevicePropertyStru.Mask.Handle) != 0) { Debug.Assert(_hDev == props.dev_hdl, "hDev"); } else { Debug.Fail("Handle unknown?!?"); } if ((props.mask & Structs.BtSdkRemoteDevicePropertyStru.Mask.Address) != 0) { addr = BluesoleilUtils.ToBluetoothAddress(props.bd_addr); } if ((props.mask & Structs.BtSdkRemoteDevicePropertyStru.Mask.Class) != 0) { _cod = new ClassOfDevice(props.dev_class); } if ((props.mask & Structs.BtSdkRemoteDevicePropertyStru.Mask.Name) != 0) { Debug.Assert(props.name.Length == StackConsts.BTSDK_DEVNAME_LEN, "props.name.Length: " + props.name.Length + ", BTSDK_DEVNAME_LEN: " + StackConsts.BTSDK_DEVNAME_LEN); string name = BluesoleilUtils.FromNameString(props.name); _cachedName = name; } if ((props.mask & Structs.BtSdkRemoteDevicePropertyStru.Mask.LmpInfo) != 0) { var fs = (LmpFeatures)BitConverter.ToInt64(props.lmp_info.lmp_feature, 0); var v = new RadioVersions(props.lmp_info.lmp_version, props.lmp_info.lmp_subversion, fs, props.lmp_info.manuf_name); _versions = v; } //props. }
public ServiceRecord[] GetServiceRecords(Guid service) { BtSdkError ret; var search = new Structs.BtSdkSDPSearchPatternStru[1]; search[0] = new Structs.BtSdkSDPSearchPatternStru(service); UInt32[] recordHandles = new uint[MaxServiceRecordsLookup]; int num = recordHandles.Length; // Fetch the matching records (the handles of). int numSearch = search.Length; ret = _factory.Api.Btsdk_BrowseRemoteServicesEx(_hDev, search, numSearch, recordHandles, ref num); if (ret == BtSdkError.NO_SERVICE) // None { return(new ServiceRecord[0]); } BluesoleilUtils.CheckAndThrow(ret, "Btsdk_BrowseRemoteServicesEx"); // Fetch the records' attributes and convert to our format. var results = new List <ServiceRecord>(); for (int i = 0; i < num; ++i) { var hRcd = recordHandles[i]; var attrs = new Structs.BtSdkRemoteServiceAttrStru( StackConsts.AttributeLookup.ServiceName | StackConsts.AttributeLookup.ExtAttributes); // Use 'Get' here and not 'Refresh' as the 'Browse' call above // should have retrieved the attributes. ret = _factory.Api.Btsdk_GetRemoteServiceAttributes(hRcd, ref attrs); BluesoleilUtils.CheckAndThrow(ret, "Btsdk_RefreshRemoteServiceAttributes"); Debug.Assert(attrs.dev_hdl == _hDev); // IBluesoleilApi hackApi = _factory.Api; ServiceRecord sr = CreateServiceRecord(ref attrs, hackApi); results.Add(sr); } return(results.ToArray()); }
private BluesoleilDeviceInfo(UInt32 hDev, BluesoleilFactory factory) { if (factory == null) { throw new ArgumentNullException("factory"); } _factory = factory; BtSdkError ret; _hDev = hDev; // byte[] bd_addr = new byte[StackConsts.BTSDK_BDADDR_LEN]; ret = factory.Api.Btsdk_GetRemoteDeviceAddress(_hDev, bd_addr); BluesoleilUtils.CheckAndThrow(ret, "Btsdk_GetRemoteDeviceAddress"); if (ret != BtSdkError.OK) // Not known/present(?). { return; } _addr = BluesoleilUtils.ToBluetoothAddress(bd_addr); // uint cod; ret = factory.Api.Btsdk_GetRemoteDeviceClass(_hDev, out cod); if (ret != BtSdkError.OK) // Not known/present(?). { return; } _cod = new ClassOfDevice(cod); // ret = _factory.Api.Btsdk_IsDevicePaired(_hDev, out _paired); Debug.Assert(ret == BtSdkError.OK, "Btsdk_IsDevicePaired ret: " + ret); // _connected = _factory.Api.Btsdk_IsDeviceConnected(_hDev); // GetInfo(ref _addr); }