Bluetoothで周辺の機器を探索
bluetooth(BT)機器が周りにあるかどうかを探索します。
何に使うのかは、また今度に。
BT関連の情報ってなぜかあんまない。サンプルもあんまないし。
今回のターゲットはwindowsで、環境はVS2005です。
大まかにいえば、こんな感じ。
BluetoothDeviceSearchParams() で、BT機器の探索設定
BluetoothFindFirstDevice() で、BT機器を探索開始
BluetoothFindNextDevice() で、次のBT機器を取得
BluetoothFindDeviceClose() で、探索終了
サンプル。エラー処理とか入ってないので注意を。
動作テストとかもあんましてない。かなりやっつけ。
間違ってたら教えてください(><。
それぞれのAPIはこんな感じ。
そして、BTデバイスの内容を格納する場所の定義。
それ以外にも、以下のような構造体が必要です。
と、まぁ、こんな感じで。
周辺のBT機器を探索することができます(できるはず...)。
何に使うのかは、また今度に。
BT関連の情報ってなぜかあんまない。サンプルもあんまないし。
今回のターゲットはwindowsで、環境はVS2005です。
大まかにいえば、こんな感じ。
BluetoothDeviceSearchParams() で、BT機器の探索設定
BluetoothFindFirstDevice() で、BT機器を探索開始
BluetoothFindNextDevice() で、次のBT機器を取得
BluetoothFindDeviceClose() で、探索終了
サンプル。エラー処理とか入ってないので注意を。
動作テストとかもあんましてない。かなりやっつけ。
間違ってたら教えてください(><。
Dim btdi As New BluetoothDeviceInfo(0)
Dim btdsp As New BluetoothDeviceSearchParams(0)
btdsp.returnAuthenticated = 1
btdsp.returnConnected = 1
btdsp.returnRemembered = 1
btdsp.issueInquiry = 1
btdsp.returnUnknown = 1
btdsp.timeoutMultiplier = 5
Dim hRetFirstDev As IntPtr = BluetoothFindFirstDevice(btdsp, btdi)
If hRetFirstDev <> IntPtr.Zero Then
debug.print("address = " & btdi.address.ToString())
debug.print("szName = " & btdi.name)
Dim bNext As Boolean = False
Do
btdi = Nothing
btdi = New BluetoothDeviceInfo(0)
bNext = BluetoothFindNextDevice(hRetFirstDev, btdi)
If bNext Then
debug.print("address = " & btdi.address.ToString())
debug.print("szName = " & btdi.name)
End If
Loop While bNext
BluetoothFindDeviceClose(hRetFirstDev)
End If
それぞれのAPIはこんな感じ。
<StructLayout(LayoutKind.Sequential, pack:=4)> _
Private Structure BluetoothDeviceSearchParams
Public size As UInteger
Public returnAuthenticated As Integer
Public returnRemembered As Integer
Public returnUnknown As Integer
Public returnConnected As Integer
Public issueInquiry As Integer
Public timeoutMultiplier As Byte
Public hRadio As IntPtr
Sub New(ByVal dmy As Integer)
Me.size = Marshal.SizeOf(Me)
End Sub
End Structure
<DllImport("irprops.cpl", setlasterror:=True> _
Private Shared Function BluetoothFindFirstDevice( _
<[In]()> ByRef searchParams As BluetoothDeviceSearchParams, _
ByRef deviceInfo As BluetoothDeviceInfo) As IntPtr
End Function
<DllImport("irprops.cpl", setlasterror:=True)> _
Private Shared Function BluetoothFindNextDevice( _
ByVal hFind As IntPtr, _
ByRef pbtdi As BluetoothDeviceInfo) As Integer
End Function
<DllImport("irprops.cpl", setlasterror:=True)> _
Private Shared Function BluetoothFindDeviceClose( _
ByVal hFind As IntPtr) As Integer
End Function
そして、BTデバイスの内容を格納する場所の定義。
Private Const BT_MAX_NM_SZ = 248
<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Private Structure BluetoothDeviceInfo
Public size As UInteger
Public address As BluetoothAddress
Public classofDevice As UInteger
Public connected As Boolean
Public remembered As Boolean
Public authenticated As Boolean
Public lastSeen As SystemTime
Public lastUsed As SystemTime
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=BT_MAX_NM_SZ)> _
Public name As String
Sub New(ByVal dmy As Integer)
Me.name = New String("*"c, BT_MAX_NM_SZ)
Me.lastSeen = New SystemTime
Me.lastUsed = New SystemTime
Me.size = Marshal.SizeOf(Me)
End Sub
End Structure
それ以外にも、以下のような構造体が必要です。
<StructLayout(LayoutKind.Sequential, pack:=1)> _
Private Structure SystemTime
Public year As UShort
Public month As UShort
Public dayOfWeek As UShort
Public day As UShort
Public hour As UShort
Public minute As UShort
Public second As UShort
Public milliseconds As UShort
End Structure
<StructLayout(LayoutKind.Sequential, pack:=1)> _
Private Structure BluetoothAddress
Public byte1 As Byte
Public byte2 As Byte
Public byte3 As Byte
Public byte4 As Byte
Public byte5 As Byte
Public byte6 As Byte
Public byte7 As Byte
Public byte8 As Byte
Public byte9 As Byte
Public byte10 As Byte
Public byte11 As Byte
Public byte12 As Byte
Public Overrides Function ToString() As String
Return String.Format("{0}:{1}:{2}:{3}:{4}:{5}", _
byte10.ToString("x"), byte9.ToString("x"), _
byte8.ToString("x"), byte7.ToString("x"), _
byte6.ToString("x"), byte5.ToString("x"))
End Function
End Structure
と、まぁ、こんな感じで。
周辺のBT機器を探索することができます(できるはず...)。
- 最新技術解説 入門Bluetooth (NEW COMPUTER TECHNOLOGY LIB.../ネットテクノロジーラボ

- ¥1,869
- Amazon.co.jp