Wie man hier nachlesen kann, ist die Bluetoothunterstützung von Windows eher mau,
es kann also durchaus sein, dass ihr gar keine Devices scannen könnt.
Aber da dige so nett gefragt hat, stelle ich es doch hier rein.
Das ganze ist mal wieder als Interface aufgebaut.
Keine Ahnung warum, irgendwie gefällt mir diese Art der Kapsellung.
Es wird noch die bthprops.lib benötigt.
Die ist im Windows Vista SDK enthalten (a bissle gross, wegen so einer lib, ich weiss, aber ich kann nix dafür).
Kommentare sind etwas spärlich, wie immer.
Hoffe ihr kommt trotzdem damit klar.
BlueTooth.pbi:
Code: Alles auswählen
;/------------------
;|
;| BlueTooth.pbi
;|
;| (c)HeX0R 2011
;| V1.00 [03.02.2011]
;|
;| MSDN Bluetooth-Reference:
;| http://msdn.microsoft.com/en-us/library/aa362930%28v=VS.85%29.aspx
;|
;| See also this thread to be informed, that microsofts
;| bluetooth-support is not the best at all: [This thread is in german language!]
;| http://www.purebasic.fr/german/viewtopic.php?f=6&t=23856
;|
;| So chances are good, that you won't scan any device with this include.
;| If you have no lock, blame M$ not me ;)
;|
;/------------------
#BLUETOOTH_MAX_NAME_SIZE = 248
#ERROR_NOT_FOUND = 1168
;The following structure is not really correct, but it works
Structure BLUETOOTH_ADDRESS
BTH_ADDR.l
rgBytes.b[8]
EndStructure
Structure BLUETOOTH_DEVICE_SEARCH_PARAMS
dwSize.l
fReturnAuthenticated.l
fReturnRemembered.l
fReturnUnknown.l
fReturnConnected.l
fIssueInquiry.l
cTimeoutMultiplier.l
hRadio.i
EndStructure
Structure BLUETOOTH_DEVICE_INFO
dwSize.l
Address.BLUETOOTH_ADDRESS
ulClassofDevice.l
fConnected.l
fRemembered.l
fAuthenticated.l
stLastSeen.SYSTEMTIME
stLastUsed.SYSTEMTIME
szName.w[#BLUETOOTH_MAX_NAME_SIZE]
EndStructure
Structure BLUETOOTH_FIND_RADIO_PARAMS
dwSize.l
EndStructure
Structure BLUETOOTH_RADIO_INFO
dwSize.l
address.BLUETOOTH_ADDRESS
szName.w[#BLUETOOTH_MAX_NAME_SIZE];
ulClassofDevice.l
lmpSubversion.w
manufacturer.w
EndStructure
Structure _ST_IF_BLUETOOTH_
VTable.i
Handle.i
LastError.l
NumDevices.i
MaxDevices.i
*Devices.BLUETOOTH_DEVICE_INFO[0]
EndStructure
;Bthprops.lib is available in the Windows Vista SDK, see:
;http://msdn.microsoft.com/en-us/library/aa363058%28VS.85%29.aspx
Import "bthprops.lib"
BluetoothFindFirstDevice.l(*pbtsp, *pbtdi) As "_BluetoothFindFirstDevice@8"
BluetoothFindNextDevice.l(hFind, *pbtdi) As "_BluetoothFindNextDevice@8"
BluetoothFindDeviceClose.l(hFind) As "_BluetoothFindDeviceClose@4"
BluetoothFindFirstRadio.l(*pbtfrp, *phRadio) As "_BluetoothFindFirstRadio@8"
BluetoothFindNextRadio.l(hFind, *phRadio) As "_BluetoothFindNextRadio@8"
BluetoothFindRadioClose.l(hFind) As "_BluetoothFindRadioClose@4"
BluetoothGetRadioInfo.l(hRadio, *pRadioInfo) As "_BluetoothGetRadioInfo@8"
BluetoothGetDeviceInfo.l(hRadio, *pbtdi) As "_BluetoothGetDeviceInfo@8"
BluetoothDisplayDeviceProperties.l(hwndParent, *pbtdi) As "_BluetoothDisplayDeviceProperties@8"
BluetoothEnableDiscovery(hRadio, fEnabled) As "_BluetoothEnableDiscovery@8"
BluetoothEnableIncomingConnections(hRadio, fEnabled) As "_BluetoothEnableIncomingConnections@8"
BluetoothSelectDevices(*pbtsdp) As "_BluetoothSelectDevices@4"
BluetoothIsConnectable(hRadio) As "_BluetoothIsConnectable@4"
BluetoothIsDiscoverable(hRadio) As "_BluetoothIsDiscoverable@4"
BluetoothSelectDevicesFree(*pbtsdp) As "_BluetoothSelectDevicesFree@4"
BluetoothAuthenticateDevice(hwndParent, hRadio, *pbtdi, *pszPasskey, ulPasskeyLength) As "_BluetoothAuthenticateDevice@20"
BluetoothSetServiceState(hRadio, *pbtdi, *pGuidService, dwServiceFlags.l) As "_BluetoothSetServiceState@16"
BluetoothRemoveDevice(*pAddress.BLUETOOTH_ADDRESS) As "_BluetoothRemoveDevice@4"
EndImport
Interface _BLUETOOTH_
ConnectLocal.i()
DisconnectLocal()
ScanDevices.i(SearchTime, IgnoreUnnamedDevices = #False)
CountFoundDevices.i()
GetDeviceName.s(Num.i)
GetDeviceAddress.s(Num)
GetDeviceTimeLastSeen.i(Num.i)
GetDeviceTimeLastUsed.i(Num.i)
RefreshDeviceInfo.i(Num.i)
IsDeviceConnected.i(Num)
IsDeviceAuthenticated.i(Num)
GetDeviceInfo.i(Num, *pbtdi.BLUETOOTH_DEVICE_INFO)
AuthenticateDevice.i(Num, Password.s = "")
AddSerialPortToDevice.i(Num, Mode)
RemoveDevice.i(Num)
GetLastError.i()
EndInterface
Procedure _BT_GetLastError(*THIS._ST_IF_BLUETOOTH_)
ProcedureReturn *THIS\LastError
EndProcedure
Procedure _BT_LastSeen(*THIS._ST_IF_BLUETOOTH_, Num)
;/-------------
;| returns #ERROR_SUCCESS on Success
;|
;| other possible values:
;| #ERROR_NOT_FOUND : this device is not available (at the moment)
;| or not connected
;| #ERROR_INVALID_PARAMETER : Num is out of range
;/-------------
Protected *ST.SYSTEMTIME, Result
If Num >= 0 And Num < *THIS\NumDevices
*ST = *THIS\Devices + Num * SizeOf(BLUETOOTH_DEVICE_INFO) + OffsetOf(BLUETOOTH_DEVICE_INFO\stLastSeen)
Result = Date(*ST\wYear, *ST\wMonth, *ST\wDay, *ST\wHour, *ST\wMinute, *ST\wSecond)
EndIf
ProcedureReturn Result
EndProcedure
Procedure _BT_LastUsed(*THIS._ST_IF_BLUETOOTH_, Num)
;/-------------
;| returns #ERROR_SUCCESS on Success
;|
;| other possible values:
;| #ERROR_NOT_FOUND : this device is not available (at the moment)
;| or not connected
;| #ERROR_INVALID_PARAMETER : Num is out of range
;/-------------
Protected *ST.SYSTEMTIME, Result
If Num >= 0 And Num < *THIS\NumDevices
*ST = *THIS\Devices + Num * SizeOf(BLUETOOTH_DEVICE_INFO) + OffsetOf(BLUETOOTH_DEVICE_INFO\stLastUsed)
Result = Date(*ST\wYear, *ST\wMonth, *ST\wDay, *ST\wHour, *ST\wMinute, *ST\wSecond)
EndIf
ProcedureReturn Result
EndProcedure
Procedure _BT_RefreshDeviceInfo(*THIS._ST_IF_BLUETOOTH_, Num)
;/-------------
;| returns #ERROR_SUCCESS on Success
;|
;| other possible values:
;| #ERROR_NOT_FOUND : this device is not available (at the moment)
;| or not connected
;| #ERROR_INVALID_PARAMETER : Num is out of range
;/-------------
Protected Result = #ERROR_INVALID_PARAMETER
If Num >= 0 And Num < *THIS\NumDevices
Result = BluetoothGetDeviceInfo(*THIS\Handle, *THIS\Devices + Num * SizeOf(BLUETOOTH_DEVICE_INFO))
EndIf
If Result <> #ERROR_SUCCESS
*THIS\LastError = Result
EndIf
ProcedureReturn Result
EndProcedure
Procedure _BT_RemoveDevice(*THIS._ST_IF_BLUETOOTH_, Num)
;/-------------
;| returns #ERROR_SUCCESS on Success
;|
;| other possible values:
;| #ERROR_NOT_FOUND : this device is not available (at the moment)
;| or not connected
;| #ERROR_INVALID_PARAMETER : Num is out of range
;/-------------
Protected Result = #ERROR_INVALID_PARAMETER, *pAddress
If Num >= 0 And Num < *THIS\NumDevices
*pAddress = *THIS\Devices + Num * SizeOf(BLUETOOTH_DEVICE_INFO) + OffsetOf(BLUETOOTH_DEVICE_INFO\Address) + 4
Result = BluetoothRemoveDevice(*pAddress)
EndIf
If Result <> #ERROR_SUCCESS
*THIS\LastError = Result
EndIf
ProcedureReturn Result
EndProcedure
Procedure _BT_AddSerialPortToDevice(*THIS._ST_IF_BLUETOOTH_, Num, Mode)
;/-------------
;| returns #ERROR_SUCCESS on Success
;|
;| other possible values:
;| #ERROR_INVALID_PARAMETER : Num is out of range
;| #ERROR_SERVICE_DOES_NOT_EXIST : No Bluetooth to serial service available
;| #E_INVALIDARG : You tried to de/activate, but it is allready de/activated
;/-------------
Protected Result = #ERROR_INVALID_PARAMETER
If Num >= 0 And Num < *THIS\NumDevices
If Mode
Mode = #True
EndIf
Result = BluetoothSetServiceState(*THIS\Handle, *THIS\Devices + Num * SizeOf(BLUETOOTH_DEVICE_INFO), ?GUID_SerialPortServiceClass_UUID, Mode)
EndIf
If Result = #ERROR_SUCCESS
_BT_RefreshDeviceInfo(*THIS, Num)
Else
*THIS\LastError = Result
EndIf
ProcedureReturn Result
EndProcedure
Procedure _BT_AuthenticateDevice(*THIS._ST_IF_BLUETOOTH_, Num, Password.s = "")
;/-------------
;| returns #ERROR_SUCCESS on Success
;|
;| other possible values:
;| #ERROR_INVALID_PARAMETER : Num is out of range
;| #ERROR_NO_MORE_ITEMS : The device is already marked as authenticated
;| #WAIT_TIMEOUT : The device didn't respond in time
;| #ERROR_DEVICE_NOT_CONNECTED
;| #ERROR_GEN_FAILURE
;| #ERROR_NOT_AUTHENTICATED
;| #ERROR_NOT_ENOUGH_MEMORY
;| #ERROR_REQ_NOT_ACCEP
;| #ERROR_ACCESS_DENIED
;| #ERROR_NOT_READY
;| #ERROR_VC_DISCONNECTED
;/-------------
Protected Result = #ERROR_INVALID_PARAMETER, *Buffer, L
If Num >= 0 And Num < *THIS\NumDevices
If Password
L = Len(Password)
*Buffer = AllocateMemory(StringByteLength(Password, #PB_Unicode) + 2)
PokeS(*Buffer, Password, -1, #PB_Unicode)
EndIf
Result = BluetoothAuthenticateDevice(#Null, *THIS\Handle, *THIS\Devices + Num * SizeOf(BLUETOOTH_DEVICE_INFO), *Buffer, L)
If L
FreeMemory(*Buffer)
EndIf
EndIf
If Result = #ERROR_SUCCESS
_BT_RefreshDeviceInfo(*THIS, Num)
Else
*THIS\LastError = Result
EndIf
ProcedureReturn Result
EndProcedure
Procedure _BT_IsDeviceAuthenticated(*THIS._ST_IF_BLUETOOTH_, Num)
;/-------------
;| returns <> 0 if device is authenticated
;|
;| other possible values:
;| #False : This device is not authenticated
;/-------------
Protected Result
If Num >= 0 And Num < *THIS\NumDevices
Result = PeekL(*THIS\Devices + Num * SizeOf(BLUETOOTH_DEVICE_INFO) + OffsetOf(BLUETOOTH_DEVICE_INFO\fAuthenticated))
EndIf
ProcedureReturn Result
EndProcedure
Procedure _BT_IsDeviceConnected(*THIS._ST_IF_BLUETOOTH_, Num)
;/-------------
;| returns <> 0 if device is connected
;|
;| other possible values:
;| #False : This device is not connected
;/-------------
Protected Result
If Num >= 0 And Num < *THIS\NumDevices
Result = PeekL(*THIS\Devices + Num * SizeOf(BLUETOOTH_DEVICE_INFO) + OffsetOf(BLUETOOTH_DEVICE_INFO\fConnected))
EndIf
ProcedureReturn Result
EndProcedure
Procedure _BT_GetDeviceInfo(*THIS._ST_IF_BLUETOOTH_, Num, *pbtdi.BLUETOOTH_DEVICE_INFO)
Protected Result = #ERROR_INVALID_PARAMETER
If Num >= 0 And Num < *THIS\NumDevices
If *pbtdi
If CopyMemory(*THIS\Devices + Num * SizeOf(BLUETOOTH_DEVICE_INFO), *pbtdi, SizeOf(BLUETOOTH_DEVICE_INFO))
Result = #True
EndIf
Else
Result = *THIS\Devices + Num * SizeOf(BLUETOOTH_DEVICE_INFO)
EndIf
EndIf
If Result = #ERROR_INVALID_PARAMETER
*THIS\LastError = Result
EndIf
ProcedureReturn Result
EndProcedure
Procedure.s _BT_GetDeviceAddress(*THIS._ST_IF_BLUETOOTH_, Num)
Protected i, Result.s, *pAddress.BLUETOOTH_ADDRESS
If Num >= 0 And Num < *THIS\NumDevices
*pAddress = *THIS\Devices + Num * SizeOf(BLUETOOTH_DEVICE_INFO) + OffsetOf(BLUETOOTH_DEVICE_INFO\Address)
For i = 5 To 1 Step - 1
Result + RSet(Hex(*pAddress\rgBytes[i], #PB_Byte), 2, "0") + ":"
Next i
Result + RSet(Hex(*pAddress\rgBytes[0], #PB_Byte), 2, "0")
Else
*THIS\LastError = #ERROR_INVALID_PARAMETER
EndIf
ProcedureReturn Result
EndProcedure
Procedure.s _BT_GetDeviceName(*THIS._ST_IF_BLUETOOTH_, Num)
Protected Result.s
If Num >= 0 And Num < *THIS\NumDevices
Result = PeekS(*THIS\Devices + Num * SizeOf(BLUETOOTH_DEVICE_INFO) + OffsetOf(BLUETOOTH_DEVICE_INFO\szName), -1, #PB_Unicode)
Else
*THIS\LastError = #ERROR_INVALID_PARAMETER
EndIf
ProcedureReturn Result
EndProcedure
Procedure _BT_CountFoundDevices(*THIS._ST_IF_BLUETOOTH_)
;/-----------
;| returns the number of
;| found devices
;/-----------
ProcedureReturn *THIS\NumDevices
EndProcedure
Procedure _BT_DisconnectLocal(*THIS._ST_IF_BLUETOOTH_)
;/-----------
;| Call this at the end of your
;| program, or if you are finished
;| with bluetooth.
;/-----------
If *THIS\Handle
CloseHandle_(*THIS\Handle)
*THIS\Handle = #Null
*THIS\NumDevices = 0
EndIf
EndProcedure
Procedure _BT_ConnectLocal(*THIS._ST_IF_BLUETOOTH_)
;/-------------
;| Tries to connect to your local Bluetooth
;| Call this before you try to Scan.
;|
;| On Success it will return something other then #False
;/-------------
Protected pbtfrp.BLUETOOTH_FIND_RADIO_PARAMS, hFind
_BT_DisconnectLocal(*THIS._ST_IF_BLUETOOTH_)
pbtfrp\dwSize = SizeOf(BLUETOOTH_FIND_RADIO_PARAMS)
hFind = BluetoothFindFirstRadio(@pbtfrp, @*THIS\Handle)
If hFind
If BluetoothIsConnectable(*THIS\Handle) = #False
BluetoothEnableIncomingConnections(*THIS\Handle, #True)
EndIf
If BluetoothIsDiscoverable(*THIS\Handle) = #False
BluetoothEnableDiscovery(*THIS\Handle, #True)
EndIf
BluetoothFindRadioClose(hFind)
Else
*THIS\LastError = #ERROR_SERVICE_NOT_ACTIVE
EndIf
ProcedureReturn hFind
EndProcedure
Procedure _BT_ScanDevices(*THIS._ST_IF_BLUETOOTH_, SearchTime, IgnoreUnnamedDevices = #False)
;/----------------
;| Scan for radio devices
;|
;| REMARK:
;| This procedure will NOT return before SearchTime (in Seconds).
;| So be prepared, that your application will halt for this time.
;| Better call it through a thread.
;|
;| Returns the number of found deviced
;/----------------
Protected *pbtsp.BLUETOOTH_DEVICE_SEARCH_PARAMS, f.f
Protected *pbtdi.BLUETOOTH_DEVICE_INFO
Protected hFind, *Buffer
*THIS\NumDevices = 0
If *THIS\Handle = 0
_BT_ConnectLocal(*THIS._ST_IF_BLUETOOTH_)
EndIf
If *THIS\Handle
*pbtsp = AllocateMemory(SizeOf(BLUETOOTH_DEVICE_SEARCH_PARAMS))
*pbtdi = AllocateMemory(SizeOf(BLUETOOTH_DEVICE_INFO))
f = SearchTime / 1.28
If f > 48
SearchTime = 48
ElseIf f < 0
SearchTime = 0
Else
SearchTime = Int(f)
EndIf
*pbtsp\dwSize = SizeOf(BLUETOOTH_DEVICE_SEARCH_PARAMS)
*pbtsp\fReturnUnknown = #True
*pbtsp\fReturnAuthenticated = #True
*pbtsp\fReturnConnected = #True
*pbtsp\fReturnRemembered = #True
*pbtsp\fIssueInquiry = #True
*pbtsp\hRadio = *THIS\Handle
*pbtsp\cTimeoutMultiplier = SearchTime
*pbtdi\dwSize = SizeOf(BLUETOOTH_DEVICE_INFO)
hFind = BluetoothFindFirstDevice(*pbtsp, *pbtdi)
If hFind
Repeat
If PeekW(*pbtdi + OffsetOf(BLUETOOTH_DEVICE_INFO\szName)) Or IgnoreUnnamedDevices = #False
CopyMemory(*pbtdi, *THIS\Devices + *THIS\NumDevices * SizeOf(BLUETOOTH_DEVICE_INFO), SizeOf(BLUETOOTH_DEVICE_INFO))
*THIS\NumDevices + 1
If *THIS\NumDevices > *THIS\MaxDevices
*THIS\MaxDevices + 10
*THIS\Devices = ReAllocateMemory(*THIS\Devices, *THIS\MaxDevices * SizeOf(BLUETOOTH_DEVICE_INFO))
EndIf
EndIf
Until BluetoothFindNextDevice(hFind, *pbtdi) = 0
BluetoothFindDeviceClose(hFind)
Else
*THIS\LastError = #ERROR_NO_MORE_ITEMS
EndIf
FreeMemory(*pbtsp)
FreeMemory(*pbtdi)
EndIf
ProcedureReturn *THIS\NumDevices
EndProcedure
Procedure CreateBlueToothInterface()
;/----------
;| The Interface creating procedure
;/----------
Protected *THIS._ST_IF_BLUETOOTH_
If OSVersion() < #PB_OS_Windows_XP
ProcedureReturn #False
EndIf
*THIS = AllocateMemory(SizeOf(_ST_IF_BLUETOOTH_) + 8)
*THIS\VTable = ?_VT_BLUETOOTH_DATA_
If *THIS = 0
ProcedureReturn #False
EndIf
*THIS\MaxDevices = 10
*THIS\NumDevices = 0
*THIS\Handle = 0
*THIS\Devices = AllocateMemory(*THIS\MaxDevices * SizeOf(BLUETOOTH_DEVICE_INFO))
ProcedureReturn *THIS
EndProcedure
DataSection
GUID_SerialPortServiceClass_UUID:
Data.l $00001101
Data.w $0000, $1000
Data.b $80, $00, $00, $80, $5F, $9B, $34, $FB
_VT_BLUETOOTH_DATA_:
Data.i @_BT_ConnectLocal()
Data.i @_BT_DisconnectLocal()
Data.i @_BT_ScanDevices()
Data.i @_BT_CountFoundDevices()
Data.i @_BT_GetDeviceName()
Data.i @_BT_GetDeviceAddress()
Data.i @_BT_LastSeen()
Data.i @_BT_LastUsed()
Data.i @_BT_RefreshDeviceInfo()
Data.i @_BT_IsDeviceConnected()
Data.i @_BT_IsDeviceAuthenticated()
Data.i @_BT_GetDeviceInfo()
Data.i @_BT_AuthenticateDevice()
Data.i @_BT_AddSerialPortToDevice()
Data.i @_BT_RemoveDevice()
Data.i @_BT_GetLastError()
EndDataSection
Code: Alles auswählen
XIncludeFile "BlueTooth.pbi"
EnableExplicit
Global *BTI._BLUETOOTH_
Enumeration
#Window_BlueTooth
EndEnumeration
Enumeration
#BT_ClientList
#BT_ScanButton
#BT_Text_Log
#BT_Log
EndEnumeration
Enumeration
#Popup_Menu_Bluetooth
EndEnumeration
Enumeration
#MenuItem_BT_Authenticate
#MenuItem_BT_AddSerialPort
#MenuItem_BT_RemoveDevice
EndEnumeration
Enumeration
#StatusBar_BlueTooth
EndEnumeration
Enumeration
#Timer_BlueTooth
EndEnumeration
;Helper for our Connect-Thread
Structure _BLUETOOTH_HELPER_
Num.i
SerialPortAdded.i
i.i
Password.s
EndStructure
Procedure.s GetErrorMessage(Error = #PB_Default)
;Get WinAPI-Error-Message
Protected Result.s = "No Error", Msg.s
If Error = #PB_Default
Error = GetLastError_()
EndIf
Msg = Space(1024)
If FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, 0, Error, 0, @Msg, 1024, 0)
Result = Msg
EndIf
ProcedureReturn Result
EndProcedure
Procedure BT_Log(Text.s)
AddGadgetItem(#BT_Log, -1, FormatDate("[%hh:%ii:%ss] ", Date()) + Text)
SendMessage_(GadgetID(#BT_Log), #EM_SCROLLCARET, #SB_BOTTOM, 0)
EndProcedure
Procedure BT_Window_CallBack(hWnd, MSG, wparam, lparam)
;Use a callback, because PB still looses Events, when User moves the window.
;This is inacceptable, when you wait for a message.
;(Well in fact this behaviour is ALLWAYS inacceptable, but i guess it will never change...)
Protected i, j, Result = #PB_ProcessPureBasicEvents
Select MSG
Case #WM_APP
DisableGadget(#BT_ScanButton, 0)
j = *BTI\CountFoundDevices()
BT_Log(Str(j) + " devices found!")
For i = 1 To j
AddGadgetItem(#BT_ClientList, -1, *BTI\GetDeviceName(i - 1) + " (" + *BTI\GetDeviceAddress(i - 1) + ")")
Next i
EndSelect
ProcedureReturn Result
EndProcedure
Procedure BT_ScanThread(TimeOUT)
;We are scanning within a thread,
;so our program won't freeze the whole time.
If *BTI\ScanDevices(TimeOUT) = #ERROR_SUCCESS
SendMessage_(WindowID(#Window_BlueTooth), #WM_APP, 1, 1)
Else
SendMessage_(WindowID(#Window_BlueTooth), #WM_APP, 0, 0)
EndIf
EndProcedure
Procedure BT_ConnectThread(*BT_Client._BLUETOOTH_HELPER_)
;Another Thread
;This one tries to connect, authenticate and
;opens a virtual serial port.
Protected i, j
i = *BT_Client\i
If *BTI\IsDeviceAuthenticated(i)
BT_Log("Device is allready authenticated.")
*BT_Client\Num = i
j = #True
Else
BT_Log("Trying to authenticate " + *BTI\GetDeviceName(i) + "...")
Delay(2000)
If *BTI\AuthenticateDevice(i, *BT_Client\Password) = #ERROR_SUCCESS
BT_Log("Successfully authenticated!")
*BT_Client\Num = i
j = #True
Else
Delay(2000)
If *BTI\AuthenticateDevice(i, *BT_Client\Password) = #ERROR_SUCCESS
BT_Log("Successfully authenticated!")
*BT_Client\Num = i
j = #True
Else
BT_Log("Authentication Error!(" + GetErrorMessage(*BTI\GetLastError()) + ")")
j = #False
EndIf
EndIf
EndIf
If j
;Authenticated, now wait a little bit, before adding serial port
Delay(2500)
If *BTI\AddSerialPortToDevice(i, #True) = #ERROR_SUCCESS
BT_Log("Serial Port successful connected!")
*BT_Client\Num = i
*BT_Client\SerialPortAdded = #True
ElseIf *BTI\GetLastError() = #E_INVALIDARG
BT_Log("Serial Port allready there...")
*BT_Client\Num = i
*BT_Client\SerialPortAdded = #True
Else
BT_Log("Error while trying to connect serial port.")
*BT_Client\Num = i
*BT_Client\SerialPortAdded = 0
EndIf
EndIf
DisableGadget(#BT_ScanButton, 0)
EndProcedure
Procedure BT_CheckActiveClient(*BT_Client._BLUETOOTH_HELPER_, Num)
If *BT_Client\Num > -1 And Num <> *BT_Client\Num
If *BT_Client\SerialPortAdded
*BTI\AddSerialPortToDevice(*BT_Client\Num, #False)
EndIf
*BTI\RemoveDevice(*BT_Client\Num)
*BT_Client\Num = -1
*BT_Client\SerialPortAdded = 0
EndIf
EndProcedure
Procedure OpenWindow_Bluetooth()
OpenWindow(#Window_BlueTooth, 0, 0, 300, 400, "", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
SetWindowCallback(@BT_Window_CallBack(), #Window_BlueTooth)
AddWindowTimer(#Window_BlueTooth, #Timer_BlueTooth, 900)
ListIconGadget(#BT_ClientList, 5, 5, 290, 250, "Clients", 290)
TextGadget(#BT_Text_Log, 5, 268, 50, 20, "Log:")
ButtonGadget(#BT_ScanButton, 110, 260, 80, 22, "ReScan")
EditorGadget(#BT_Log, 5, 288, 290, 88, #PB_Editor_ReadOnly)
CreateStatusBar(#StatusBar_BlueTooth, WindowID(#Window_BlueTooth))
AddStatusBarField(#PB_Ignore)
If CreatePopupMenu(#Popup_Menu_Bluetooth)
MenuItem(#MenuItem_BT_Authenticate, "Authenticate")
MenuItem(#MenuItem_BT_AddSerialPort, "Add Serial Port")
MenuBar()
MenuItem(#MenuItem_BT_RemoveDevice, "Remove Device")
EndIf
EndProcedure
Procedure main()
Protected i, j, a$, BT_Scan_Duration, BT_ThreadID_A, BT_ThreadID_B, BT_Client._BLUETOOTH_HELPER_
*BTI = CreateBlueToothInterface()
OpenWindow_Bluetooth()
BT_Client\Num = -1
BT_ThreadID_A = CreateThread(@BT_ScanThread(), 12)
If BT_ThreadID_A
BT_Scan_Duration = ElapsedMilliseconds() + 12 * 1000
DisableGadget(#BT_ScanButton, 1)
BT_Log("Scanning started, please wait...")
EndIf
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
If EventWindow() = #Window_BlueTooth
Break
EndIf
Case #PB_Event_Timer
Select EventTimer()
Case #Timer_BlueTooth
If BT_ThreadID_A And IsThread(BT_ThreadID_A)
StatusBarText(#StatusBar_BlueTooth, 0, "Scanning another " + Str(Int((BT_Scan_Duration - ElapsedMilliseconds()) / 1000)) + "s")
ElseIf BT_ThreadID_A
BT_ThreadID_A = 0
StatusBarText(#StatusBar_BlueTooth, 0, "")
EndIf
EndSelect
Case #PB_Event_Menu
Select EventMenu()
Case #MenuItem_BT_AddSerialPort
i = GetGadgetState(#BT_ClientList)
If i > -1
BT_CheckActiveClient(@BT_Client, i)
If BT_Client\SerialPortAdded
If *BTI\AddSerialPortToDevice(i, #False) = #ERROR_SUCCESS
BT_Client\Num = i
BT_Client\SerialPortAdded = #False
BT_Log("Serial Port successfully disconnected!")
ElseIf *BTI\GetLastError() = #E_INVALIDARG
BT_Client\Num = i
BT_Client\SerialPortAdded = #False
BT_Log("Serial Port is allready disconnected...")
Else
BT_Log("Something went wrong...")
EndIf
Else
If *BTI\AddSerialPortToDevice(i, #True) = #ERROR_SUCCESS
BT_Log("Serial Port successfully connected!")
BT_Client\Num = i
BT_Client\SerialPortAdded = #True
ElseIf *BTI\GetLastError() = #E_INVALIDARG
BT_Log("Serial Port is allready connected...")
BT_Client\Num = i
BT_Client\SerialPortAdded = #True
Else
BT_Log("Something went wrong...")
EndIf
EndIf
EndIf
Case #MenuItem_BT_Authenticate
i = GetGadgetState(#BT_ClientList)
If i > -1
BT_CheckActiveClient(@BT_Client, i)
If GetMenuItemState(#Popup_Menu_Bluetooth, #MenuItem_BT_Authenticate)
BT_Log("You are allready authenticated!")
BT_Client\Num = i
Else
a$ = InputRequester("Enter Password", "Enter Password for authentication", "")
If a$ And *BTI\AuthenticateDevice(i, a$) = #ERROR_SUCCESS
BT_Log("Successfully authenticated!")
BT_Client\Num = i
Else
BT_Log("Authentication Error!(" + GetErrorMessage(*BTI\GetLastError()) + ")")
EndIf
EndIf
EndIf
Case #MenuItem_BT_RemoveDevice
i = GetGadgetState(#BT_ClientList)
If i > -1
If BT_Client\i = i
BT_CheckActiveClient(@BT_Client, i)
Else
*BTI\RemoveDevice(i)
EndIf
RemoveGadgetItem(#BT_ClientList, i)
EndIf
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
Case #BT_ScanButton
ClearGadgetItems(#BT_ClientList)
BT_CheckActiveClient(@BT_Client, -1)
StatusBarText(#StatusBar_BlueTooth, 0, "")
BT_Scan_Duration = ElapsedMilliseconds() + 12 * 1000
BT_ThreadID_A = CreateThread(@BT_ScanThread(), 12)
If BT_ThreadID_A
DisableGadget(#BT_ScanButton, 1)
BT_Log("Scanning started...")
EndIf
Case #BT_ClientList
i = GetGadgetState(#BT_ClientList)
If BT_ThreadID_B = 0 Or IsThread(BT_ThreadID_B) = 0
Select EventType()
Case #PB_EventType_LeftDoubleClick
If i > -1
BT_Client\Password = InputRequester("Enter Password", "Enter Password for authentication", BT_Client\Password)
BT_CheckActiveClient(@BT_Client, i)
BT_Client\i = i
BT_ThreadID_B = CreateThread(@BT_ConnectThread(), @BT_Client)
If BT_ThreadID_B
DisableGadget(#BT_ScanButton, 1)
EndIf
EndIf
Case #PB_EventType_RightClick
If i > -1
If *BTI\IsDeviceAuthenticated(i)
SetMenuItemState(#Popup_Menu_Bluetooth, #MenuItem_BT_Authenticate, 1)
Else
SetMenuItemState(#Popup_Menu_Bluetooth, #MenuItem_BT_Authenticate, 0)
EndIf
DisplayPopupMenu(#Popup_Menu_Bluetooth, WindowID(#Window_BlueTooth))
EndIf
EndSelect
EndIf
EndSelect
EndSelect
ForEver
If BT_ThreadID_B And IsThread(BT_ThreadID_B)
If WaitThread(BT_ThreadID_B, 2000) = 0
KillThread(BT_ThreadID_B)
EndIf
EndIf
If BT_ThreadID_A And IsThread(BT_ThreadID_A)
If WaitThread(BT_ThreadID_A, 2000) = 0
KillThread(BT_ThreadID_A)
EndIf
EndIf
BT_CheckActiveClient(@BT_Client, -1)
*BTI\DisconnectLocal()
EndProcedure
main()