예제 #1
0
        public void SetServiceStateDoIt(Guid service, bool state)
        {
            UInt16?classId16 = BluetoothService.GetAsClassId16(service);

            if (!classId16.HasValue)
            {
                throw new ArgumentException("BlueSoleil only supports standard Bluetooth UUID16 services.");
            }
            //
            // MSDN says the posible errors are:
            //   ERROR_INVALID_PARAMETER The dwServiceFlags are invalid.
            //   ERROR_SERVICE_DOES_NOT_EXIST The GUID specified in pGuidService is not supported.
            // Numerically:
            //   #define ERROR_FILE_NOT_FOUND             2L
            //   #define ERROR_SERVICE_DOES_NOT_EXIST     1060L
            //   #define ERROR_NOT_FOUND                  1168L
            //
            // Seen:
            // • 0x00000424 = 1060 ----> ERROR_SERVICE_DOES_NOT_EXIST
            // When service not present, or device not present.
            //
            // • 0x80070002        -/\-> ERROR_FILE_NOT_FOUND
            // PANU on Broadcom peer.  "No driver for service"?
            //
            // • 0x00000490 = 1168 ----> ERROR_NOT_FOUND
            // Setting 'False' on a service not set previously registered.
            //--
            BtSdkError ret;

            if (state)
            {
                UInt32 hConn;
                ret = _factory.Api.Btsdk_ConnectEx(_hDev, classId16.Value, 0, out hConn);
                if (ret != BtSdkError.OK)
                {
                    throw new Win32Exception((int)Win32Error.ERROR_SERVICE_DOES_NOT_EXIST, "Failed to enable the service.");
                }
            }
            else
            {
                var hConnList = FindConnection(_hDev, classId16.Value);
                if (hConnList == null || hConnList.Count == 0)
                {
                    throw new Win32Exception((int)Win32Error.ERROR_NOT_FOUND, "No matching enabled service found.");
                }
                // TO-DO SetServiceState, before disabling, ensure its not BluetoothClient's connection.
                Debug.Assert(hConnList.Count == 1, "SetServiceState: What to do if more than one match?");
                ret = _factory.Api.Btsdk_Disconnect(hConnList[0]);
                if (ret != BtSdkError.OK)
                {
                    throw new Win32Exception((int)Win32Error.ERROR_SERVICE_DOES_NOT_EXIST, "Failed to disabled the service.");
                }
            }
        }