使用c编程连接蓝牙设备



我想将用于将蓝牙设备连接到Raspberry pi的python代码转换为C代码。有可能吗??有没有一种方法可以使用c编程将蓝牙设备连接到树莓pi。代码如下:

import pygatt
from binascii import hexlify
import time
adapter = pygatt.GATTToolBackend()
def handle_data(handle, value):
"""
handle -- integer, characteristic read handle the data was received on
value -- bytearray, the data returned in the notification
"""
print("Received data: %s" % hexlify(value))
try:
adapter.start()
device = adapter.connect('AC:23:3F:AA:36:7C')
device.subscribe("7f280002-8204-f393-e0a9-e50e24dcca9e",
callback=handle_data)
# The subscription runs on a background thread. You must stop this main
# thread from exiting, otherwise you will not receive any messages, and
# the program will exit. Sleeping in a while loop like this is a simple
# solution that won't eat up unnecessary CPU, but there are many other
# ways to handle this in more complicated program. Multi-threaded
# programming is outside the scope of this README.
while True:
time.sleep(10)
finally:
adapter.stop()

我知道的两个选项:https://github.com/petzval/btferret和https://github.com/weliem/bluez_inc.这是等效的bt雪貂代码。


/********* DEVICES.TXT file ************
DEVICE = My Pi         TYPE=mesh node=1  ADDRESS = B8:27:EB:F1:50:C3
DEVICE = LE device     TYPE=LE      NODE=7   ADDRESS = AC:23:3F:AA:36:7C                                      
*******************************************/

/******** C file *****************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "btlib.h"

int callback(int clientnode,int cticn,unsigned char *dat,int len);
int main()
{
int cticn;

if(init_blue("devices.txt") == 0)
return(0);
// connect to LE device, node 7

connect_node(7,CHANNEL_LE,0);
find_ctics(7);
cticn = find_ctic_index(7,UUID_16,strtohex("7f280002-8204-f393-e0a9-e50e24dcca9e",NULL));  
notify_ctic(7,cticn,NOTIFY_ENABLE,callback);
read_notify(10000);  // read for 10s

close_all();

return(0);
}

int callback(int clientnode,int cticn,unsigned char *dat,int len)
{
int n;

printf("Notify =");
for(n = 0 ; n < len ; ++n)
printf(" %02X",dat[n]);
printf("n");
return(SERVER_CONTINUE);
}

最新更新