⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 这是Linux环境下的蓝牙源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	message = dbus_message_new_method_call ("org.bluez", adapter,						"org.bluez.Adapter",						"ListRemoteDevices");	if (message == NULL)		return FALSE;	dbus_error_init(&error);	reply = dbus_connection_send_with_reply_and_block(conn,							message, -1, &error);	dbus_message_unref(message);	if (&error != NULL && dbus_error_is_set(&error))		return FALSE;	dbus_message_iter_init(reply, &reply_iter);	if (dbus_message_iter_get_arg_type(&reply_iter) != DBUS_TYPE_ARRAY) {		dbus_message_unref(reply);		return FALSE;	}	dbus_message_iter_recurse(&reply_iter, &iter_array);	while (dbus_message_iter_get_arg_type(&iter_array) == DBUS_TYPE_STRING) {		char *bdaddr;		dbus_message_iter_get_basic(&iter_array, &bdaddr);		if (device_is_printer(adapter, bdaddr)) {			char *name, *id;			name = device_get_name(adapter, bdaddr);			id = device_get_ieee1284_id(adapter, bdaddr);			add_device_to_list(name, bdaddr, id);			g_free(name);			g_free(id);		}		dbus_message_iter_next(&iter_array);	}	dbus_message_unref(reply);	return FALSE;}static DBusHandlerResult filter_func(DBusConnection *connection, DBusMessage *message, void *user_data){	const char *adapter;	if (dbus_message_is_signal(message, "org.bluez.Adapter",						"RemoteDeviceFound")) {		char *bdaddr;		guint class;		int rssi;		dbus_message_get_args(message, NULL,					DBUS_TYPE_STRING, &bdaddr,					DBUS_TYPE_UINT32, &class,					DBUS_TYPE_INT32, &rssi,					DBUS_TYPE_INVALID);		adapter = dbus_message_get_path(message);		remote_device_found(adapter, bdaddr, class, rssi);	} else if (dbus_message_is_signal(message, "org.bluez.Adapter",							"RemoteNameUpdated")) {		char *bdaddr, *name;		dbus_message_get_args(message, NULL,					DBUS_TYPE_STRING, &bdaddr,					DBUS_TYPE_STRING, &name,					DBUS_TYPE_INVALID);		remote_name_updated(bdaddr, name);	} else if (dbus_message_is_signal(message, "org.bluez.Adapter",						"RemoteDeviceDisappeared")) {		char *bdaddr;		dbus_message_get_args(message, NULL,					DBUS_TYPE_STRING, &bdaddr,					DBUS_TYPE_INVALID);		remote_device_disappeared(bdaddr);	} else if (dbus_message_is_signal(message, "org.bluez.Adapter",						"DiscoveryCompleted")) {		discovery_completed();	}	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;}static gboolean list_printers(void){	/* 1. Connect to the bus	 * 2. Get the manager	 * 3. Get the default adapter	 * 4. Get a list of devices	 * 5. Get the class of each device	 * 6. Print the details from each printer device	 */	DBusError error;	dbus_bool_t hcid_exists;	DBusMessage *reply, *message;	DBusMessageIter reply_iter;	char *adapter, *match;	guint len;	conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL);	if (conn == NULL)		return FALSE;	dbus_error_init(&error);	hcid_exists = dbus_bus_name_has_owner(conn, "org.bluez", &error);	if (&error != NULL && dbus_error_is_set(&error))		return FALSE;	if (!hcid_exists)		return FALSE;	/* Get the default adapter */	message = dbus_message_new_method_call("org.bluez", "/org/bluez",						"org.bluez.Manager",						"DefaultAdapter");	if (message == NULL) {		dbus_connection_unref(conn);		return FALSE;	}	reply = dbus_connection_send_with_reply_and_block(conn,							message, -1, &error);	dbus_message_unref(message);	if (&error != NULL && dbus_error_is_set(&error)) {		dbus_connection_unref(conn);		return FALSE;	}	dbus_message_iter_init(reply, &reply_iter);	if (dbus_message_iter_get_arg_type(&reply_iter) != DBUS_TYPE_STRING) {		dbus_message_unref(reply);		dbus_connection_unref(conn);		return FALSE;	}	dbus_message_iter_get_basic(&reply_iter, &adapter);	adapter = g_strdup(adapter);	dbus_message_unref(reply);	if (!dbus_connection_add_filter(conn, filter_func, adapter, g_free)) {		g_free(adapter);		dbus_connection_unref(conn);		return FALSE;	}#define MATCH_FORMAT				\	"type='signal',"			\	"interface='org.bluez.Adapter',"	\	"sender='org.bluez',"			\	"path='%s'"	len = strlen(MATCH_FORMAT) - 2 + strlen(adapter) + 1;	match = g_malloc(len);	snprintf(match, len, "type='signal',"				"interface='org.bluez.Adapter',"				"sender='org.bluez',"				"path='%s'",				adapter);	dbus_bus_add_match(conn, match, &error);	g_free(match);	message = dbus_message_new_method_call("org.bluez", adapter,					"org.bluez.Adapter",					"DiscoverDevicesWithoutNameResolving");	if (!dbus_connection_send_with_reply(conn, message, NULL, -1)) {		dbus_message_unref(message);		dbus_connection_unref(conn);		g_free(adapter);		return FALSE;	}	dbus_message_unref(message);	/* Also add the the recent devices */	g_timeout_add(0, (GSourceFunc) list_known_printers, adapter);	loop = g_main_loop_new(NULL, TRUE);	g_main_loop_run(loop);	dbus_connection_unref(conn);	return TRUE;}/* *  Usage: printer-uri job-id user title copies options [file] * */int main(int argc, char *argv[]){	sdp_session_t *sdp;	bdaddr_t bdaddr;	unsigned short ctrl_psm, data_psm;	uint8_t channel, b[6];	char *ptr, str[3], device[18], service[12];	const char *uri, *cups_class;	int i, err, fd, copies, proto;	/* Make sure status messages are not buffered */	setbuf(stderr, NULL);	/* Ignore SIGPIPE signals */#ifdef HAVE_SIGSET	sigset(SIGPIPE, SIG_IGN);#elif defined(HAVE_SIGACTION)	memset(&action, 0, sizeof(action));	action.sa_handler = SIG_IGN;	sigaction(SIGPIPE, &action, NULL);#else	signal(SIGPIPE, SIG_IGN);#endif /* HAVE_SIGSET */	if (argc == 1) {		if (list_printers() == TRUE)			return CUPS_BACKEND_OK;		else			return CUPS_BACKEND_FAILED;	}	if (argc < 6 || argc > 7) {		fprintf(stderr, "Usage: bluetooth job-id user title copies options [file]\n");		return CUPS_BACKEND_FAILED;	}	if (argc == 6) {		fd = 0;		copies = 1;	} else {		if ((fd = open(argv[6], O_RDONLY)) < 0) {			perror("ERROR: Unable to open print file");			return CUPS_BACKEND_FAILED;		}		copies = atoi(argv[4]);	}	uri = getenv("DEVICE_URI");	if (!uri)		uri = argv[0];	if (strncasecmp(uri, "bluetooth://", 12)) {		fprintf(stderr, "ERROR: No device URI found\n");		return CUPS_BACKEND_FAILED;	}	ptr = argv[0] + 12;	for (i = 0; i < 6; i++) {		strncpy(str, ptr, 2);		b[i] = (uint8_t) strtol(str, NULL, 16);		ptr += 2;	}	sprintf(device, "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",		b[0], b[1], b[2], b[3], b[4], b[5]);	str2ba(device, &bdaddr);	ptr = strchr(ptr, '/');	if (ptr) {		strncpy(service, ptr + 1, 12);		if (!strncasecmp(ptr + 1, "spp", 3))			proto = 1;		else if (!strncasecmp(ptr + 1, "hcrp", 4))			proto = 2;		else			proto = 0;	} else {		strcpy(service, "auto");		proto = 0;	}	cups_class = getenv("CLASS");	fprintf(stderr, "DEBUG: %s device %s service %s fd %d copies %d class %s\n",			argv[0], device, service, fd, copies,					cups_class ? cups_class : "(none)");	fputs("STATE: +connecting-to-device\n", stderr);service_search:	sdp = sdp_connect(BDADDR_ANY, &bdaddr, SDP_RETRY_IF_BUSY);	if (!sdp) {		fprintf(stderr, "ERROR: Can't open Bluetooth connection\n");		return CUPS_BACKEND_FAILED;	}	switch (proto) {	case 1:		err = sdp_search_spp(sdp, &channel);		break;	case 2:		err = sdp_search_hcrp(sdp, &ctrl_psm, &data_psm);		break;	default:		proto = 2;		err = sdp_search_hcrp(sdp, &ctrl_psm, &data_psm);		if (err) {			proto = 1;			err = sdp_search_spp(sdp, &channel);		}		break;	}	sdp_close(sdp);	if (err) {		if (cups_class) {			fputs("INFO: Unable to contact printer, queuing on "					"next printer in class...\n", stderr);			sleep(5);			return CUPS_BACKEND_FAILED;		}		sleep(20);		fprintf(stderr, "ERROR: Can't get service information\n");		goto service_search;	}connect:	switch (proto) {	case 1:		err = spp_print(BDADDR_ANY, &bdaddr, channel,						fd, copies, cups_class);		break;	case 2:		err = hcrp_print(BDADDR_ANY, &bdaddr, ctrl_psm, data_psm,						fd, copies, cups_class);		break;	default:		err = CUPS_BACKEND_FAILED;		fprintf(stderr, "ERROR: Unsupported protocol\n");		break;	}	if (err == CUPS_BACKEND_FAILED && cups_class) {		fputs("INFO: Unable to contact printer, queuing on "					"next printer in class...\n", stderr);		sleep(5);		return CUPS_BACKEND_FAILED;	} else if (err == CUPS_BACKEND_RETRY) {		sleep(20);		goto connect;	}	if (fd != 0)		close(fd);	if (!err)		fprintf(stderr, "INFO: Ready to print\n");	return err;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -