2009-07-01 12 views
1

私はモバイルアプリケーション(タブレットPCのC#/ WPF)でBluetooth接続プリンタに印刷しています。今は印刷ジョブを起動します。プリンタが存在しない場合、プリンタサブシステムはエラーをユーザーに報告します。私はBluetoothでプログラム的に何もしていない、ただPrintDialog()を使っている。ブルートゥースプリンタの存在を検出する

プリンタを検出するためにこのプロセスを変更したいのですが、使用できない場合は印刷せずにドキュメントを保存します。 Bluetoothデバイスが接続されているかどうかを検出する方法はありますか?

[コントロールパネル]の[Bluetooth]パネルでデバイスを見ると、デバイスが使用可能かどうかを示すステータスが表示されないため、これは不可能です。

私はプリンタが既にセットアップされ、Windowsで設定されていると仮定しています。実行する必要があるのは、特定の時点で実際に存在するかどうかを検出することだけです。

答えて

1

おそらく、(私は管理者です)32feet.NETライブラリを使用して、ジョブを送信する前にプリンタが存在するかどうかを確認してください。プリンタのBluetoothアドレスを知る必要があります。システムからそれを得ることができますか、あるいはあなたはいつもそれを知ることができます。

MSFT Bluetoothスタックでの検出は、範囲内の既知のデバイスを常に返します.-(ただし、他の手段を使用してデバイスの有無を検出することができます)BeginGetServiceRecordsフォームのBluetoothDeviceInfo.GetServiceRecordsを使用します。 (テストされていない/コンパイル済み):

bool IsPresent(BluetoothAddress addr) // address from config somehow 
{ 
    BluetoothDeviceInfo bdi = new BluetoothDeviceInfo(addr); 
    if (bdi.Connected) { 
     return true; 
    } 
    Guid arbitraryClass = BluetoothService.Headset; 
    AsyncResult<bool> ourAr = new AsyncResult<bool>(); // Jeffrey Richter's impl 
    IAsyncResult ar = bdi.BeginGetService(arbitraryClass, IsPresent_GsrCallback, ourAr); 
    bool signalled = ourAr.AsyncWaitHandle.WaitOne(Timeout); 
    if (!signalled) { 
     return false; // Taken too long, so not in range 
    } else { 
     return ourAr.Result; 
    } 
} 

void IsPresent_GsrCallback(IAsyncResult ar) 
{ 
    AsyncResult<bool> ourAr = (AsyncResult<bool>)ar.AsyncState; 
    const bool IsInRange = true; 
    const bool completedSyncFalse = true; 
    try { 
     bdi.EndGetServiceResult(ar); 
     ourAr.SetAsCompleted(IsInRange, completedSyncFalse); 
    } catch { 
     // If this returns quickly, then it is in range and 
     // if slowly then out of range but caller will have 
     // moved on by then... So set true in both cases... 
     // TODO check what error codes we get here. SocketException(10108) iirc 
     ourAr.SetAsCompleted(IsInrange, completedSyncFalse); 
    } 
} 
関連する問題