2017-02-05 4 views
1

Nodejsスクリプトでそのアドレスを指定してBluetoothデバイスのルックアップを実行できますか?既知のBluetoothデバイスのNodejルックアップ

そこにはいくつかのパッケージがありますが、主なものはNobleです。しかし、彼らはすべてスキャンに焦点を当て、既知のアドレスを検索しません(私がとっておられる限り)。

私が達成したいのは、デバイスが見つかるかどうかを知るために、既知のアドレスを調べることです。 PyBluezのような多くの は、Pythonのために行います。Pythonで bluetooth.lookup_name('CC:20:E8:8F:3A:1D', timeout=5)

、これは典型的な問い合わせスキャンが希望とは異なり、それが発見できた場合でも、デバイスを見つけることができます。

答えて

0

私はこの同じ問題を抱え、btwatchライブラリを見つけましたが、最新のraspbianで私のために働いていません。しかし、ソースはちょうどl2pingを呼び出していて、私がもう成功したとは思わない文字列を探しているので、l2pingがインストールされていれば、以下の変更されたコードが代わりに機能します(npmのブルートゥースかpybluezがそれは)

var Spawn = require('child_process').spawn; 

function detectMacAddress(macAddress, callback) 
{ 
    //var macAddress = '72:44:56:05:79:A0'; 
    var ls = Spawn('l2ping', ['-c', '1', '-t', '5', macAddress]); 

    ls.stdout.on('data', function (data) { 
     console.log("Found device in Range! " + macAddress); 
     callback(true); 
    }); 

    ls.on('close', function() { 
     console.log("Could not find: " + macAddress); 
     callback(false); 
    }); 
} 

または、同期方法、

var execSync = require('child_process').execSync; 

function detectMacAddressSync(macAddress) 
{ 
    var cmd = 'l2ping -c 1 -t 5 ' + macAddress; 
    try 
    { 
     var output = execSync(cmd); 
     console.log("output : "+ output); 
     return true; 
    } 
    catch(e) 
    { 
     console.log("caught: " + e); 
     return false; 
    } 
} 
+1

ああ!素晴らしい!これはまさに私が後にしたものです。ありがとうございました。 – jdrucey

0

私が知っている限り、あなたはアドレスを使ってデバイスに接続したいと思っています。次に、node-bluetooth-serial-portを使用することをお勧めします。

var btSerial = new (require('bluetooth-serialport')).BluetoothSerialPort(); 

btSerial.on('found', function(address, name) { 
    btSerial.findSerialPortChannel(address, function(channel) { 
     btSerial.connect(address, channel, function() { 
     console.log('connected'); 

     btSerial.write(new Buffer('my data', 'utf-8'), function(err, bytesWritten) { 
      if (err) console.log(err); 
     }); 

     btSerial.on('data', function(buffer) { 
      console.log(buffer.toString('utf-8')); 
     }); 
    }, function() { 
     console.log('cannot connect'); 
    }); 

    // close the connection when you're ready 
    btSerial.close(); 
}, function() { 
    console.log('found nothing'); 
}); 
}); 

BluetoothSerialPort.findSerialPortChannel(アドレス、コールバック【、errorCallback])

チェックデバイスは、シリアルポートサービスを実行しており、それが見つかった場合、それはRFCOMMに使用するチャネルIDを渡す場合接続。

コールバック(チャネル) - デバイス上のシリアルポートを探し終えたときに呼び出されます。 errorCallback - 検索が完了しましたが、シリアルポートチャネルがデバイス上に見つかりませんでした。リモートBluetoothデバイスに接続します。

bluetoothAddress - リモートBluetoothデバイスのアドレス。

チャネル - 接続先のチャネル。 [successCallback] - 接続が確立されたときに呼び出されます。 [errorCallback(err)] - 接続の試行でエラーが発生したときに呼び出されます。パラメータはErrorオブジェクトです。

+0

HIのrresol、私はいくつかの本で遊んで行っています。 Pythonの例と同じようには動作しません。私がテストしているデバイスはチャンネルをブロードキャストしないので、 'BluetoothSerialPort.findSerialPortChannel'は、そのデバイスが検出可能でないと常に失敗します。この段階では、デバイスが存在するかどうかを単に確認することを目指しています。これらのいずれかの内部動作はわかりませんが、Pythonの '.lookup_name'は単にデバイスを探しますが、bluetooth-serialportライブラリはデバイスを探しています**と**は利用可能なチャネルを持っています。それが理にかなっていれば? – jdrucey

関連する問題