7

Linuxでgatttoolを使用して制御できるBluetoothデバイスがあります。コマンドを送信できる独自の C プログラムを開発したいと考えています。

私は過去にBluetoothプログラミングを行ったことがあり、ネットワークプログラミングに似て比較的簡単ですが、今回はBluetooth低エネルギーデバイスであり、ここでの原則に従うと、明確に接続/切断できるときにホストがダウンしているというメッセージが表示されますガットツールを使用。

このプログラムを作成するにはどうすればよいですか? bluez ライブラリを使用する必要があることはわかっていますが、低エネルギー デバイスをどこから始めればよいかわかりません。

int main(int argc, char **argv)
{
   struct sockaddr_rc addr = { 0 };
   int s, status;
   char dest[18] = "B4:99:4C:5C:EE:49";
   char buf[2048];
   pthread_t rthread;

   setbuf(stdout, NULL); 
   // allocate a socket
   s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
   // set the connection parameters (who to connect to)
   addr.rc_family = AF_BLUETOOTH;
   addr.rc_channel = (uint8_t) 1;
   str2ba( dest, &addr.rc_bdaddr );
   // connect to server
   status = connect(s, (struct sockaddr *)&addr, sizeof(addr));

   if( status < 0 ){
      perror("Error connecting to host\n");
      exit(1);
   }

   while(fgets(buf, sizeof(buf), stdin) != NULL){
      status = send(s, buf, sizeof(buf), 0);
      if(status < 0){
         printf("Error sending.\n");
     exit(1);
      }
   }

   close(s);

   return;
4

3 に答える 3

2

あなたのプログラムは古典的なBluetooth用です。私の声明をサポートするために、古典的なBluetoothデバイスではコードが正常に機能すると言います

Lescan を入手するには、このリンクを参照することをお勧めします。sudo ./stは、近くの ble デバイスをスキャンします

https://github.com/carsonmcdonald/bluez-experiments

于 2015-03-28T14:23:09.607 に答える
0

github.com の別のプロジェクトはきれいに見えます: https://github.com/edrosten/libblepp

ここの議論で言及されています: https://mbientlab.com/community/discussion/2492/bluetooth-le-library-linux

ただし、CではなくC ++です。

于 2020-12-17T17:10:08.290 に答える