{"id":23898069,"url":"https://github.com/byteszero/bluetoothdemo","last_synced_at":"2025-04-10T17:05:11.113Z","repository":{"id":32619676,"uuid":"36205312","full_name":"BytesZero/bluetoothDemo","owner":"BytesZero","description":"蓝牙低功耗（BLE）的demo","archived":false,"fork":false,"pushed_at":"2015-10-22T11:03:00.000Z","size":438,"stargazers_count":40,"open_issues_count":0,"forks_count":15,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-24T14:46:13.867Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BytesZero.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-25T02:28:40.000Z","updated_at":"2021-08-31T03:05:09.000Z","dependencies_parsed_at":"2022-06-26T23:33:26.478Z","dependency_job_id":null,"html_url":"https://github.com/BytesZero/bluetoothDemo","commit_stats":null,"previous_names":["byteszero/bluetoothdemo"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BytesZero%2FbluetoothDemo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BytesZero%2FbluetoothDemo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BytesZero%2FbluetoothDemo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BytesZero%2FbluetoothDemo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BytesZero","download_url":"https://codeload.github.com/BytesZero/bluetoothDemo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248260166,"owners_count":21074207,"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":[],"created_at":"2025-01-04T17:19:49.633Z","updated_at":"2025-04-10T17:05:11.090Z","avatar_url":"https://github.com/BytesZero.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bluetoothDemo\n蓝牙低功耗（BLE）的demo\n\n###工具类\n```java\npackage com.zsl.bluetoothdemo.ble;\n\n\nimport android.bluetooth.BluetoothAdapter;\nimport android.bluetooth.BluetoothDevice;\nimport android.bluetooth.BluetoothGatt;\nimport android.bluetooth.BluetoothGattCallback;\nimport android.bluetooth.BluetoothManager;\nimport android.content.Context;\nimport android.content.Intent;\nimport android.os.Handler;\nimport java.util.ArrayList;\nimport java.util.List;\n\n/**\n * 蓝牙的工具类\n * Created by zsl on 15/5/25.\n */\n\n\npublic class UniversalBluetoothLE {\n\n    //UniversalBluetoothLE\n    public static UniversalBluetoothLE universalBluetoothLE;\n\n    private Context context;\n    //BluetoothAdapter\n    private BluetoothAdapter mBluetoothAdapter;\n    //BluetoothManager\n    private BluetoothManager bluetoothManager;\n\n    //打开蓝牙的请求码\n    public static final int REQUEST_ENABLE_BLUETOOTH = 10010;\n\n    //是否正在扫描蓝牙设备\n    private boolean mScanning;\n    //设置扫描时长\n    private static final long SCAN_PERIOD = 10000;\n\n    //蓝牙扫描的返回\n    BluetoothAdapter.LeScanCallback leScanCallback;\n    //蓝牙设别的list\n    List\u003cBluetoothDevice\u003e bluetoothDeviceList = new ArrayList\u003cBluetoothDevice\u003e();\n\n    Handler mHandler = new Handler();\n\n    LeScanListenter leScanListenter;\n\n    private UniversalBluetoothLE(Context context) {\n        this.context = context;\n        //得到BluetoothManager\n        this.bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);\n        //得到BluetoothAdapter\n        this.mBluetoothAdapter = bluetoothManager.getAdapter();\n\n        //蓝牙搜索的回调\n        leScanCallback = new BluetoothAdapter.LeScanCallback() {\n\n            @Override\n            public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {\n                bluetoothDeviceList.add(device);\n\n                //返回所有列表\n                leScanListenter.leScanCallBack(bluetoothDeviceList);\n\n            }\n        };\n    }\n\n    /**\n     * 获得到UniversalBluetoothLE对象\n     *\n     * @param context\n     * @return\n     */\n    public static UniversalBluetoothLE inistance(Context context) {\n        if (universalBluetoothLE == null) {\n            universalBluetoothLE = new UniversalBluetoothLE(context);\n        }\n        return universalBluetoothLE;\n    }\n\n    /**\n     * 检查蓝牙是否打开并且启动打开蓝牙的方法\n     */\n    public void openBbletooth() {\n        //判断蓝牙是否开启\n        if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {\n            //打开蓝牙\n            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);\n            context.startActivity(enableIntent);\n        }\n    }\n\n    /**\n     * 开始（true）或结束（false）蓝牙扫描\n     *\n     * @param enable\n     */\n    private void scanLeDevice(final boolean enable) {\n        if (enable \u0026\u0026 mScanning == false) {\n            mHandler.postDelayed(new Runnable() {\n                @Override\n                public void run() {\n                    mScanning = false;\n                    mBluetoothAdapter.stopLeScan(leScanCallback);\n                }\n            }, SCAN_PERIOD);\n\n            mScanning = true;\n            mBluetoothAdapter.startLeScan(leScanCallback);\n        } else {\n            mScanning = false;\n            mBluetoothAdapter.stopLeScan(leScanCallback);\n        }\n    }\n\n    /**\n     * 开始搜索蓝牙设备\n     *\n     * @param leScanListenter 搜索蓝牙设备的回调（返回设备列表）\n     */\n    public void startScanLeDevice(final LeScanListenter leScanListenter) {\n        bluetoothDeviceList.clear();\n        this.leScanListenter=leScanListenter;\n        scanLeDevice(true);\n    }\n\n    /**\n     * 停止搜索设备\n     */\n    public void stopScanLeDevice() {\n        if (leScanCallback == null)\n            return;\n        scanLeDevice(false);\n    }\n\n    /**\n     * 搜索蓝牙的回调\n     */\n    public interface LeScanListenter {\n        void leScanCallBack(List\u003cBluetoothDevice\u003e bluetoothDeviceList);\n    }\n\n    /**\n     * 得到BluetoothGatt\n     * @param device 设备\n     * @param autoConnect 是否自动链接\n     * @param bluetoothGattCallback 回调\n     */\n    public BluetoothGatt getConnectGatt(BluetoothDevice device,boolean autoConnect,BluetoothGattCallback bluetoothGattCallback){\n        return device.connectGatt(context, autoConnect, bluetoothGattCallback);\n    }\n\n\n}\n\n```\n\n\u003e 初始化\n```java\n//在onCreate中\n//初始化UniversalBluetoothLE\nuniversalBluetoothLE = UniversalBluetoothLE.inistance(MainActivity.this);\n```\n\n\u003e 检测是否打开蓝牙并且请求系统打开蓝牙\n```java\n//检测是否打开蓝牙并且请求系统打开蓝牙\nuniversalBluetoothLE.openBbletooth();\n```\n\n\u003e 链接设备\n\n```java\nmBluetoothGatt=universalBluetoothLE.getConnectGatt(device,true,mGattCallback);\nmBluetoothGatt.connect();\n```\n\n###效果\n\n![](device-2015-05-25-192100.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbyteszero%2Fbluetoothdemo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbyteszero%2Fbluetoothdemo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbyteszero%2Fbluetoothdemo/lists"}