点击登录

教程 对于小米9Pro手环通过蓝牙进行网络连接的操作方法

yuan15622

Lv.0
社区会员
小米手环9PRO官网虽然标明不支持fetch,但是我通过在米坛社区群里询问群友得知,其实小米9Pro是支持fetch的,只不过官网标明错误(在此感谢某位群友,不然某个ai应用就止步于此了)。虽然支持fetch,但是从官网中的介绍可知fetch仅仅支持http协议的网址,而https的网址压根不支持,我后面搞得一个ai应用也印证了这一点,我不知道在其他设备上怎么样,但是在小米9pro上,fetch仅支持http协议,不支持https协议。
基于此,那为何我们不设置一个中转服务器,手环快应用中通过fetch来请求 支持http的中转服务器,通过这些中转服务器来进行api请求(例如gpt),然后返回的数据再次经过这个中转服务器传输回来,流程图如下:
未标题-1.webp
(gemini的生图能力确实不错)

通过这种方式,即可通过fetch完成很多操作了。那么到这里fetch联网的问题解决了,但是中转服务器呢?则么解决?
刚好有一个免费的中转服务器厂商:https://www.pythonanywhere.com/ 下面以创建chatai的中转服务器为例进行演示:
①创建账号完成之后的界面如下:
261dc2538c4db4a20dd4aa91ee818a17.webp
②点击右上角的Web,然后在界面中点击新建web app,
fdb3bde7ddb912709d7dca3434b7de0a.webp
③选择flask,python版本看个人所需.
④创建好后的界面如下图所示,点击这个目录链接:
b62689798dc2128ca595947163d6e584.webp
⑤点进去可以看到一个官方默认的py文件,点击编辑,将中转服务器代码复制粘贴进去
207ee7e92c0a8007524b65630bb6d4c9.webp
⑥然后点击保存,运行,看看输出,如果没有出错便可以回到控制台,有出错的话,比如缺少某些模块,便进入bash安装即可。
9084e5f4a82bc44b567707d62770084e.webp
⑦退回到控制台,重新加载网站,就是点击绿色的那个【Reload chatai...com】,中转服务器搭建完成,然后在手表中输入 中转服务器的网址即可,注意!!!!9pro的中转服务器网址一定要http开头,不要https,不然不行,这个网站我测试过,支持http也支持https访问。
b7e44d4764f3af1eefcb75e575e84a60.webp

中转服务器的代码:
代码:
from flask import Flask, request, jsonify
import requests


app = Flask(__name__)

# === 1. 厂商配置表 (兼具旧版兼容和新版查询) ===
PROVIDER_CONFIGS = {
    # 常用厂商
    "deepseek": "https://api.deepseek.com/v1/chat/completions",
    "glm": "https://open.bigmodel.cn/api/paas/v4/chat/completions",
    "qwen": "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions",
    "gpt": "https://api.openai.com/v1/chat/completions",
    
    # 预设别名 (手表填 '66' 就能用)
    "66": "https://api.deepseek.com/v1/chat/completions",
    "kimmmm": "https://api.moonshot.cn/v1/chat/completions",
    "yi": "https://api.lingyiwanwu.com/v1/chat/completions",
    "doubao": "https://ark.cn-beijing.volces.com/api/v3/chat/completions"
}

# === 2. 文本清洗 ===
def format_table_to_text(text):
    try:
        lines = text.split('\n')
        new_lines = []
        in_table = False
        for line in lines:
            stripped = line.strip()
            if stripped.startswith('|') and stripped.endswith('|'):
                if not in_table:
                    in_table = True
                    new_lines.append("\n[表格]:")
                else:
                    new_lines.append(stripped)
            else:
                if in_table: in_table = False
                new_lines.append(line)
        return "\n".join(new_lines)
    except:
        return text

@app.route('/api/proxy', methods=['POST'])
def proxy_request():
    try:
        data = request.get_json()
        if not data: return jsonify({"success": False, "error": "No JSON"}), 400

        api_key = data.get('apiKey')
        model = data.get('model', 'deepseek-chat').strip()
        messages = data.get('messages', [])
        
        custom_url = data.get('customUrl', '').strip()
        vendor_tag = data.get('vendor', '').strip().lower()
        client_max_tokens = data.get('max_tokens')

        target_url = ""

        # 1. 优先使用手表传来的完整 URL (手表端完全自定义)
        if custom_url.startswith("http"):
            target_url = custom_url
            print(f"路由: 使用自定义URL -> {target_url}")

        # 2. 其次使用厂商标识查表 (中转端预设)
        elif vendor_tag and vendor_tag in PROVIDER_CONFIGS:
            target_url = PROVIDER_CONFIGS[vendor_tag]
            print(f"路由: 厂商标识命中 [{vendor_tag}] -> {target_url}")

        # 3.  (兼容旧版手表,通过模型名猜)
        else:
            target_url = PROVIDER_CONFIGS["deepseek"] 
            model_lower = model.lower()
            for key, url in PROVIDER_CONFIGS.items():
                if key in model_lower:
                    target_url = url
                    print(f"路由: 旧版兼容/模型匹配 [{key}] -> {target_url}")
                    break
   
        headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {api_key}"
        }

        payload = {
            "model": model,
            "messages": messages,
            "stream": False
        }

        # 优先使用手表设定的字数
        if client_max_tokens:
            payload["max_tokens"] = int(client_max_tokens)
        else:
            if "reasoner" not in model.lower() and "o1" not in model.lower():
                payload["temperature"] = 0.7
                payload["max_tokens"] = 2000   
            else:
                 payload["max_tokens"] = 5000

        response = requests.post(target_url, json=payload, headers=headers, timeout=120)

        if response.status_code == 200:
            try:
                upstream_data = response.json()
                final_text = ""
           
                if 'choices' in upstream_data and len(upstream_data['choices']) > 0:
                    message = upstream_data['choices'][0].get('message', {})
                    final_text = message.get('content', '')
                    if not final_text and message.get('reasoning_content'):
                         final_text = "[思考过程]\n" + message.get('reasoning_content')
           
                clean_text = format_table_to_text(final_text)

                return jsonify({
                    "success": True,
                    "pure_reply": clean_text,
                    "model_used": model
                })
            except Exception as e:
                print(f"解析错误: {e}")
                return jsonify({"success": False, "error": f"Parse Error: {str(e)}"})
        else:
            return jsonify({"success": False, "error": f"API Error {response.status_code}: {response.text}"})

    except Exception as e:
        return jsonify({"success": False, "error": f"Server Error: {str(e)}"}), 500

到此就完结了,下面是一些注意事项:
chatai应用帖子:https://www.bandbbs.cn/resources/5151/
添加自定义模型:https://www.bandbbs.cn/threads/24194/
①这个中转服务器因为是免费的,所以一个账号只能建立一个网站,不能建立多个,多个的话得花钱,每一个月就需要延期一次,不然就会给你关闭。
②是不是有这个作为中转,9pro就可以浏览任何内容了?并非如此,https://www.pythonanywhere.com/whitelist/
这个网址中列出了免费用户的白名单网址,api.deepseek.com这个也在其中,如果想实现一些其他的功能,需要看一看是否在这个白名单网址中或者自建服务器进行操作。
③如有疑问,评论区留言吧,但是不保证能看到,因为平时搞论文,做这个东西纯属娱乐。
 
最后编辑:

*这是一则由 Google AdSense 自动推荐的广告,与本站无关,不对其真实性与可靠性负责

正在阅览本贴的用户

Home 首页
Home 资源
News 发现
Account 我的
顶部