{"id":27062763,"url":"https://github.com/sdwfqin/classicbluetooth","last_synced_at":"2025-04-05T15:19:22.616Z","repository":{"id":43673139,"uuid":"135448214","full_name":"sdwfqin/ClassicBluetooth","owner":"sdwfqin","description":"Android经典蓝牙工具类(cbt)------开关蓝牙、扫描蓝牙设备、连接配对、发送数据、接收数据、获取已配对设备","archived":false,"fork":false,"pushed_at":"2019-10-31T11:57:52.000Z","size":204,"stargazers_count":134,"open_issues_count":3,"forks_count":36,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-04-24T11:10:33.081Z","etag":null,"topics":["android","bluetooth"],"latest_commit_sha":null,"homepage":"https://sdwfqin.github.io","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sdwfqin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-30T13:39:19.000Z","updated_at":"2024-01-12T02:24:38.000Z","dependencies_parsed_at":"2022-07-20T04:32:28.412Z","dependency_job_id":null,"html_url":"https://github.com/sdwfqin/ClassicBluetooth","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sdwfqin%2FClassicBluetooth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sdwfqin%2FClassicBluetooth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sdwfqin%2FClassicBluetooth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sdwfqin%2FClassicBluetooth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sdwfqin","download_url":"https://codeload.github.com/sdwfqin/ClassicBluetooth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247353808,"owners_count":20925344,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","bluetooth"],"created_at":"2025-04-05T15:19:22.144Z","updated_at":"2025-04-05T15:19:22.599Z","avatar_url":"https://github.com/sdwfqin.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ClassicBluetooth\nAndroid经典蓝牙工具类\n\n已完成：\n\n- 开启蓝牙\n- 关闭蓝牙\n- 扫描蓝牙设备\n- 连接配对\n- 发送数据\n- 接收数据\n- 获取已配对设备\n\nTODO：\n\n- 接收文件\n- 功能优化、性能优化、BUG清扫\n\n最近在做一个带有蓝牙打印机的项目，借此完成这个库的开发\n\n# 导入\n\n```\nimplementation 'com.sdwfqin.cbt:cbt:1.1.1'\n```\n\n# 使用\n\n1. 初始化\n\n    ``` java\n    public class BaseApp extends Application {\n        @Override\n        public void onCreate() {\n            super.onCreate();\n            CbtManager\n                    .getInstance()\n                    // 初始化\n                    .init(this)\n                    // 是否打印相关日志\n                    .enableLog(true);\n        }\n    }\n    \n    /**\n     * 设置自定义UUID\n     *\n     * @param uuid\n     * @return BleManager\n     */\n    public CbtManager setUUID(String uuid) {\n        CbtConstant.CBT_UUID = UUID.fromString(uuid);\n        return this;\n    }\n\n    /**\n     * 设置自定义服务名称\n     *\n     * @param name\n     * @return BleManager\n     */\n    public CbtManager setServiceName(String name) {\n        CbtConstant.CBT_NAME = name;\n        return this;\n    }\n    ```\n\n2. 开启蓝牙\n\n    ``` java\n    CbtManager\n        .getInstance()\n        .enableBluetooth(isOn -\u003e {\n            if (isOn) {\n                Toast.makeText(mContext, \"蓝牙已开启\", Toast.LENGTH_SHORT).show();\n            }\n        });\n    ```\n\n2. 关闭蓝牙\n\n    ``` java\n    CbtManager\n        .getInstance()\n        .disableBluetooth(isOn -\u003e {\n            if (!isOn) {\n                Toast.makeText(mContext, \"蓝牙已关闭\", Toast.LENGTH_SHORT).show();\n            }\n        });\n    ```\n\n3. 扫描设备\n\n    ``` java\n    CbtManager\n        .getInstance()\n        .scan(new ScanCallback() {\n            @Override\n            public void onScanStart(boolean isOn) {\n                // 开始扫描\n            }\n\n            @Override\n            public void onScanStop(List\u003cBluetoothDevice\u003e devices) {\n                // 搜索完成\n                mScanListAdapter.setNewData(devices);\n            }\n\n            @Override\n            public void onFindDevice(BluetoothDevice device) {\n                // 搜索到设备\n                mScanListAdapter.addData(device);\n            }\n        });\n    ```\n\n5. 连接设备\n\n    ``` java\n    BluetoothDevice item = mScanListAdapter.getItem(position);\n    CbtManager\n        .getInstance()\n        .connectDevice(item, new ConnectDeviceCallback() {\n            @Override\n            public void connectSuccess(BluetoothSocket socket, BluetoothDevice device) {\n                // 连接成功\n                Toast.makeText(mContext, \"连接成功！\", Toast.LENGTH_SHORT).show();\n            }\n\n            @Override\n            public void connectError(Throwable throwable) {\n                // 连接失败\n                Toast.makeText(mContext, \"连接失败：\" + throwable.getMessage(), Toast.LENGTH_SHORT).show();\n            }\n        });\n    ```\n\n5. 发送数据\n\n    ``` java\n    byte[] data;\n    try {\n        data = (mData.getText().toString() + \"\\n\\n\\n\\n\\n\\n\").getBytes(\"GBK\");\n    } catch (UnsupportedEncodingException e) {\n        e.printStackTrace();\n        return;\n    }\n\n    List\u003cbyte[]\u003e bytes = new ArrayList\u003c\u003e();\n    bytes.add(BYTES[0]);\n    bytes.add(BYTES[1]);\n    bytes.add(data);\n\n    CbtManager\n        .getInstance()\n        .sendData(bytes, new SendDataCallback() {\n            @Override\n            public void sendSuccess() {\n                // 发送成功\n            }\n\n            @Override\n            public void sendError(Throwable throwable) {\n                // 发送失败\n                Toast.makeText(SendDataActivity.this, throwable.getMessage(), Toast.LENGTH_SHORT).show();\n            }\n        });\n    ```\n\n7. 蓝牙服务端（回调接口目前是在子线程中调用）\n\n    ``` java\n    CbtManager\n        .getInstance()\n        .startServiceListener(new ServiceListenerCallback() {\n            @Override\n            public void onStartError(Throwable throwable) {\n                // 发生错误\n                CbtLogs.e(throwable.getMessage());\n            }\n\n            @Override\n            public void onDataListener(String s, BluetoothDevice device) {\n                // 获取到数据\n                runOnUiThread(() -\u003e\n                        mReceiveDataAdapter.addData(new ReceiveDataModel(device, s))\n                );\n            }\n        });\n    ```\n\n# License\n\n```\nCopyright 2018 zhangqin\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsdwfqin%2Fclassicbluetooth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsdwfqin%2Fclassicbluetooth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsdwfqin%2Fclassicbluetooth/lists"}