<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; AHK: подключение к bluetooth устройствам.]]></title>
		<link>http://forum.script-coding.com/viewtopic.php?id=17669</link>
		<atom:link href="http://forum.script-coding.com/extern.php?action=feed&amp;tid=17669&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «AHK: подключение к bluetooth устройствам.».]]></description>
		<lastBuildDate>Sun, 12 Mar 2023 03:19:24 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[AHK: подключение к bluetooth устройствам.]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=157106#p157106</link>
			<description><![CDATA[<p><a href="https://docs.microsoft.com/en-us/windows/win32/api/_bluetooth/">https://docs.microsoft.com/en-us/window … bluetooth/</a><br />Подключение производится с помощью включения/выключения сервисов устройства.<br /></p><div class="codebox"><pre><code>deviceName := &quot;jbl clip 3&quot;

DllCall(&quot;LoadLibrary&quot;, &quot;str&quot;, &quot;Bthprops.cpl&quot;, &quot;ptr&quot;)
toggle := toggleOn := 1
VarSetCapacity(BLUETOOTH_DEVICE_SEARCH_PARAMS, 24+A_PtrSize*2, 0)
NumPut(24+A_PtrSize*2, BLUETOOTH_DEVICE_SEARCH_PARAMS, 0, &quot;uint&quot;)
NumPut(1, BLUETOOTH_DEVICE_SEARCH_PARAMS, 4, &quot;uint&quot;)   ; fReturnAuthenticated
VarSetCapacity(BLUETOOTH_DEVICE_INFO, 560, 0)
NumPut(560, BLUETOOTH_DEVICE_INFO, 0, &quot;uint&quot;)
loop
{
   If (A_Index = 1)
   {
      foundedDevice := DllCall(&quot;Bthprops.cpl\BluetoothFindFirstDevice&quot;, &quot;ptr&quot;, &amp;BLUETOOTH_DEVICE_SEARCH_PARAMS, &quot;ptr&quot;, &amp;BLUETOOTH_DEVICE_INFO)
      if !foundedDevice
      {
         msgbox no bluetooth devices
         return
      }
   }
   else
   {
      if !DllCall(&quot;Bthprops.cpl\BluetoothFindNextDevice&quot;, &quot;ptr&quot;, foundedDevice, &quot;ptr&quot;, &amp;BLUETOOTH_DEVICE_INFO)
      {
         msgbox no found
         break
      }
   }
   if (StrGet(&amp;BLUETOOTH_DEVICE_INFO+64) = deviceName)
   {
      VarSetCapacity(Handsfree, 16)
      DllCall(&quot;ole32\CLSIDFromString&quot;, &quot;wstr&quot;, &quot;{0000111e-0000-1000-8000-00805f9b34fb}&quot;, &quot;ptr&quot;, &amp;Handsfree)   ; https://www.bluetooth.com/specifications/assigned-numbers/service-discovery/
      VarSetCapacity(AudioSink, 16)
      DllCall(&quot;ole32\CLSIDFromString&quot;, &quot;wstr&quot;, &quot;{0000110b-0000-1000-8000-00805f9b34fb}&quot;, &quot;ptr&quot;, &amp;AudioSink)
      loop
      {
         hr := DllCall(&quot;Bthprops.cpl\BluetoothSetServiceState&quot;, &quot;ptr&quot;, 0, &quot;ptr&quot;, &amp;BLUETOOTH_DEVICE_INFO, &quot;ptr&quot;, &amp;Handsfree, &quot;int&quot;, toggle)   ; voice
         if (hr = 0)
         {
            if (toggle = toggleOn)
               break
            toggle := !toggle
         }
         if (hr = 87)
            toggle := !toggle
      }
      loop
      {
         hr := DllCall(&quot;Bthprops.cpl\BluetoothSetServiceState&quot;, &quot;ptr&quot;, 0, &quot;ptr&quot;, &amp;BLUETOOTH_DEVICE_INFO, &quot;ptr&quot;, &amp;AudioSink, &quot;int&quot;, toggle)   ; music
         if (hr = 0)
         {
            if (toggle = toggleOn)
               break 2
            toggle := !toggle
         }
         if (hr = 87)
            toggle := !toggle
      }
   }
}
DllCall(&quot;Bthprops.cpl\BluetoothFindDeviceClose&quot;, &quot;ptr&quot;, foundedDevice)
msgbox done
ExitApp</code></pre></div><p>Так же подключиться можно через winsock bluetooth api.<br /><a href="https://docs.microsoft.com/en-us/windows/win32/bluetooth/bluetooth-programming-with-windows-sockets">https://docs.microsoft.com/en-us/window … ws-sockets</a><br />Странно, что функция connect возвращает ошибку адреса устройства, но на самом деле подключается, но мы должны отправлять его в цикле около 10 секунд.<br />Не знаю почему так - плохо расписанное АПИ.<br /></p><div class="codebox"><pre><code>deviceName := &quot;jbl clip 3&quot;

DllCall(&quot;LoadLibrary&quot;, &quot;str&quot;, &quot;ws2_32.dll&quot;, &quot;ptr&quot;)
VarSetCapacity(WSADATA, 394 + (A_PtrSize - 2) + A_PtrSize, 0)
DllCall(&quot;ws2_32\WSAStartup&quot;, &quot;ushort&quot;, 0x0202, &quot;ptr&quot;, &amp;WSADATA)
size := 1024
VarSetCapacity(WSAQUERYSETW, size, 0)
NumPut(size, WSAQUERYSETW, 0, &quot;uint&quot;)
NumPut(NS_BTH := 16, WSAQUERYSETW, A_PtrSize*5, &quot;uint&quot;)
DllCall(&quot;ws2_32\WSALookupServiceBeginW&quot;, &quot;ptr&quot;, &amp;WSAQUERYSETW, &quot;uint&quot;, LUP_CONTAINERS := 0x0002, &quot;ptr*&quot;, lphLookup)
loop
{
   if (DllCall(&quot;ws2_32\WSALookupServiceNextW&quot;, &quot;ptr&quot;, lphLookup, &quot;uint&quot;, 0x0002|0x0010|0x0100, &quot;uint*&quot;, size, &quot;ptr&quot;, &amp;WSAQUERYSETW) = -1)   ; LUP_CONTAINERS|LUP_RETURN_NAME|LUP_RETURN_ADDR
   {
      error := DllCall(&quot;ws2_32\WSAGetLastError&quot;)
      if (error = 10110)   ; WSA_E_NO_MORE https://docs.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2
         msgbox device not found
      else
         msgbox % error
      exitapp
   }
   if (strget(numget(WSAQUERYSETW, A_PtrSize)) = deviceName)
   {
      socket := DllCall(&quot;ws2_32\socket&quot;, &quot;int&quot;, AF_BTH := 32, &quot;int&quot;, SOCK_STREAM := 1, &quot;int&quot;, BTHPROTO_RFCOMM := 3)
      VarSetCapacity(SOCKADDR_BTH, 36, 0)
      NumPut(AF_BTH := 32, SOCKADDR_BTH, 0, &quot;ushort&quot;)
      DllCall(&quot;RtlMoveMemory&quot;, &quot;ptr&quot;, &amp;SOCKADDR_BTH+2, &quot;ptr&quot;, numget(numget(WSAQUERYSETW, A_PtrSize*12)+0, A_PtrSize*2)+2, int, 6)   ; lpcsaBuffer.RemoteAddr.lpSockaddr.BLUETOOTH_ADDRESS_STRUCT
      DllCall(&quot;ole32\CLSIDFromString&quot;, &quot;wstr&quot;, &quot;{00000003-0000-1000-8000-00805F9B34FB}&quot;, &quot;ptr&quot;, &amp;SOCKADDR_BTH+16)   ; RFCOMM
      loop 10
      {
         DllCall(&quot;ws2_32\connect&quot;, &quot;int&quot;, socket, &quot;ptr&quot;, &amp;SOCKADDR_BTH, &quot;int&quot;, 36)
         sleep 1500
      }
      DllCall(&quot;ws2_32\closesocket&quot;, &quot;int&quot;, socket)
      DllCall(&quot;ws2_32\WSALookupServiceEnd&quot;, &quot;ptr&quot;, lphLookup)
      break
   }
}
msgbox done
exitapp</code></pre></div><p><a href="http://forum.script-coding.com/viewtopic.php?id=15943">Тема для обсуждения</a></p>]]></description>
			<author><![CDATA[null@example.com (Malcev)]]></author>
			<pubDate>Sun, 12 Mar 2023 03:19:24 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=157106#p157106</guid>
		</item>
	</channel>
</rss>
