利用 vivo 手机为电脑自动开启热点
每次给笔记本共享手机热点都需要在手机上操作,感觉非常繁琐。好在 vivo 有个设置可以简化步骤。
vivo 通过蓝牙自动打开热点主要有两种实现方法,各有利弊。
第一种
在 vivo “设置 -> 更多连接 -> 个人热点 -> 更多热点设置 -> 连接蓝牙自动开启热点”中,勾选“连接蓝牙自动开启热点”。
在“可选择的蓝牙设备”中,选择添加你的电脑设备。
若未找到你的电脑设备,需先将手机与电脑配对。
设置好后,当手机与电脑处于蓝牙连接状态时,手机将自动打开热点。
优点:
设置简单,不会误触发打开热点。
缺点:
需借助 A2DP 让手机无感与电脑建立蓝牙连接。
第二种
对蓝心小 V 说“条件指令”,打开快捷指令界面。
创建一个条件指令,当连接蓝牙你的电脑时执行开启个人热点。
优点:
貌似无需建立连接,只要设备在附近就能触发条件。无需额外干预即可打开手机热点。
缺点:
设置复杂。容易误触发。指令会被重复执行。
利用 AudioPlaybackConnector 触发自动开启热点
第一种方法需要手机与电脑建立蓝牙连接,目前我能想到的无需手机做额外配置的连接方法是通过 A2DP 协议,电脑充当 A2DP Sink。
可下载 AudioPlaybackConnector,通过该软件便捷向手机发起 A2DP 连接,热点开启后断开 A2DP 连接即可。
打开程序后会产生一个托盘图标,点击托盘图标,点击手机蓝牙名称就连接上了。
如果不需要手机的声音在电脑上响,记得建立连接后断开连接,vivo 的自动开启热点触发非常迅速,连上的瞬间热点就打开了。
但感觉这个方法仍然不够优雅,不确定是否有其他便捷建立电脑与手机蓝牙连接的方法。
利用 C++ 代码迅速触发开启热点
A few moments later,我把上面提到的 AudioPlaybackConnector 程序的核心逻辑提取出来,搓了一个打开直接连接然后断开的小程序。
代码如下,以飨众人:
#include <Windows.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Devices.Enumeration.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Media.Audio.h>
using namespace winrt::Windows::Devices::Enumeration;
using namespace winrt::Windows::Media::Audio;
int main() {
winrt::init_apartment();
HANDLE completion_event = ::CreateEvent(nullptr,TRUE,FALSE,nullptr);
if (completion_event == NULL) {
printf("ERROR: Failed to create Win32 event.\n");
return 1;
}
[](HANDLE event_handle) -> winrt::fire_and_forget {
printf("Async task started.\n");
try {
printf("Start A2DP Connect\n");
auto s = AudioPlaybackConnection::GetDeviceSelector();
auto devices = co_await DeviceInformation::FindAllAsync(s);
printf("Found %u devices in the collection.\n", devices.Size());
auto device = devices.GetAt(0);
auto connection = AudioPlaybackConnection::TryCreateFromId(device.Id());
if (connection) {
printf("Connection object created successfully.\n");
} else {
printf("Failed to create connection object.\n");
co_return;
}
co_await connection.StartAsync();
auto result = co_await connection.OpenAsync();
printf("Successfully connected to the device.\n");
connection.Close();
printf("End A2DP Connect\n");
} catch (...) {
printf("Async task completed with an error.\n");
}
::SetEvent(event_handle);
co_return;
}(completion_event);
::WaitForSingleObject(completion_event, INFINITE);
::CloseHandle(completion_event);
printf("Async task finished. Application is exiting gracefully.\n");
return 0;
}
编译后直接运行程序就能触发开启热点了,可以设置个任务计划,或者配合其他程序增加更细节的判断条件。
我觉得用的时候在桌面点击这个程序就挺方便了,本来想写个在后台判断没有网络连接就自动触发开启热点,但后来想想似乎不是所有没网的时候都想连手机就作罢了。
另外因为我电脑上只有一台手机是能发起 A2DP 连接的,所以写的时候非常偷懒,如果有需要可修改 auto device = devices.GetAt(0);
处的代码,增加更健壮的判断。
文章始发于笔者博客 雾芽 ,欢迎来访。