1

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

このプロセスを変更して、最初にプリンターを検出したいと思います。プリンターが使用できない場合は、印刷せずにドキュメントを保存します。Bluetoothデバイスが接続されている/アクティブである/利用可能であるかどうかを検出する方法はコードにありますか?

コントロールパネルの下のBluetoothパネルでデバイスを見ると、デバイスが使用可能かどうかを反映するステータスがないように見えるため、これは不可能な場合があります。

プリンターは既にWindowsでセットアップおよび構成されていると想定しています。必要なのは、特定の時点でプリンターが実際に存在するかどうかを検出することだけです。

4

1 に答える 1

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);
    }
}
于 2009-07-03T13:54:06.920 に答える