{"id":16795,"date":"2024-11-18T12:36:51","date_gmt":"2024-11-18T04:36:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/16795\/"},"modified":"2024-11-18T12:36:51","modified_gmt":"2024-11-18T04:36:51","slug":"%e9%ba%92%e9%ba%9f%e6%93%8d%e4%bd%9c%e7%b3%bb%e7%bb%9f%e5%a6%82%e4%bd%95%e4%b8%8e%e5%85%b6%e4%bb%96%e8%ae%be%e5%a4%87%e5%92%8c%e6%93%8d%e4%bd%9c%e7%b3%bb%e7%bb%9f%e8%bf%9b%e8%a1%8c%e4%ba%92%e8%81%94","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/16795\/","title":{"rendered":"\u9e92\u9e9f\u64cd\u4f5c\u7cfb\u7edf\u5982\u4f55\u4e0e\u5176\u4ed6\u8bbe\u5907\u548c\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4e92\u8054\u4e92\u901a\uff1f"},"content":{"rendered":"<p>\u9e92\u9e9f\u64cd\u4f5c\u7cfb\u7edf\uff08harmonyos\uff09\u662f\u534e\u4e3a\u63a8\u51fa\u7684\u4e00\u6b3e\u5168\u573a\u666f\u5206\u5e03\u5f0f\u64cd\u4f5c\u7cfb\u7edf\u3002\u4f5c\u4e3a\u4e00\u6b3e\u65b0\u578b\u64cd\u4f5c\u7cfb\u7edf\uff0c\u9e92\u9e9f\u4e0d\u4ec5\u652f\u6301\u5728\u534e\u4e3a\u8bbe\u5907\u4e0a\u8fd0\u884c\uff0c\u8fd8\u53ef\u4ee5\u4e0e\u5176\u4ed6\u8bbe\u5907\u548c\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4e92\u8054\u4e92\u901a\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u9e92\u9e9f\u64cd\u4f5c\u7cfb\u7edf\u5982\u4f55\u5b9e\u73b0\u4e0e\u5176\u4ed6\u8bbe\u5907\u548c\u64cd\u4f5c\u7cfb\u7edf\u7684\u4e92\u8fde\u4e92\u901a\uff0c\u5e76\u7ed9\u51fa\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u9996\u5148\uff0c\u9e92\u9e9f\u64cd\u4f5c\u7cfb\u7edf\u652f\u6301\u591a\u79cd\u901a\u4fe1\u534f\u8bae\uff0c\u5982\u84dd\u7259\u3001Wi-Fi\u3001NFC\u7b49\uff0c\u8fd9\u4f7f\u5f97\u5b83\u80fd\u591f\u8f7b\u677e\u4e0e\u5176\u4ed6\u8bbe\u5907\u8fdb\u884c\u901a\u4fe1\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u901a\u4fe1\u534f\u8bae\uff0c\u9e92\u9e9f\u64cd\u4f5c\u7cfb\u7edf\u53ef\u4ee5\u4e0e\u5176\u4ed6\u8bbe\u5907\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u3001\u6587\u4ef6\u5171\u4eab\u7b49\u64cd\u4f5c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u84dd\u7259\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>import com.huawei.harmonyos.bluetooth.btadapter.BluetoothAdapter;\nimport com.huawei.harmonyos.bluetooth.btle.api.BleCentralManager;\nimport com.huawei.harmonyos.bluetooth.btle.api.BleGatt;\nimport com.huawei.harmonyos.bluetooth.btle.api.BlePeripheralManager;\nimport com.huawei.harmonyos.bluetooth.btle.api.BlePeripheralManagerCallback;\n\npublic class BluetoothDemo {\n    private BluetoothAdapter bluetoothAdapter;\n\n    public void connectToBluetoothDevice(String deviceAddress) {\n        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();\n        BleCentralManager centralManager = bluetoothAdapter.getBleCentralManager();\n        BleGatt gatt = centralManager.connectGatt(deviceAddress);\n        gatt.setGattCallback(new BlePeripheralManagerCallback() {\n            @Override\n            public void onConnectionStateChange(int newState, int status) {\n                if (newState == BlePeripheralManagerCallback.STATE_CONNECTED) {\n                    \/\/ \u8fde\u63a5\u6210\u529f\uff0c\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u7b49\u64cd\u4f5c\n                } else if (newState == BlePeripheralManagerCallback.STATE_DISCONNECTED) {\n                    \/\/ \u8fde\u63a5\u65ad\u5f00\n                }\n            }\n\n            @Override\n            public void onCharacteristicChanged(String uuid, byte[] value) {\n                \/\/ \u63a5\u6536\u5230\u7279\u5f81\u503c\u53d8\u5316\u7684\u56de\u8c03\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5904\u7406\u63a5\u6536\u5230\u7684\u6570\u636e\n            }\n        });\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u9664\u4e86\u4e0e\u5176\u4ed6\u8bbe\u5907\u8fdb\u884c\u901a\u4fe1\u5916\uff0c\u9e92\u9e9f\u64cd\u4f5c\u7cfb\u7edf\u8fd8\u652f\u6301\u4e0e\u5176\u4ed6\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4e92\u8054\u4e92\u901a\u3002\u9e92\u9e9f\u64cd\u4f5c\u7cfb\u7edf\u57fa\u4e8e\u5fae\u5185\u6838\u67b6\u6784\u8bbe\u8ba1\uff0c\u53ef\u4ee5\u901a\u8fc7\u865a\u62df\u5316\u6280\u672f\u5728\u9e92\u9e9f\u4e0a\u8fd0\u884c\u5176\u4ed6\u64cd\u4f5c\u7cfb\u7edf\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5b9e\u73b0\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u95f4\u7684\u65e0\u7f1d\u8854\u63a5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5728\u9e92\u9e9f\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884cAndroid\u5e94\u7528\u7a0b\u5e8f\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>import android.content.Context;\nimport android.content.Intent;\nimport android.content.pm.ApplicationInfo;\nimport android.content.pm.PackageManager;\nimport android.os.Bundle;\n\npublic class AndroidDemo {\n    public void launchAndroidApp(Context context, String packageName, String className) {\n        Intent intent = new Intent(Intent.ACTION_MAIN);\n        intent.addCategory(Intent.CATEGORY_LAUNCHER);\n        intent.setClassName(packageName, className);\n        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);\n        context.startActivity(intent);\n    }\n\n    public void getApplicationMetaData(Context context, String packageName) {\n        try {\n            ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(packageName, \n                PackageManager.GET_META_DATA);\n            Bundle metaData = appInfo.metaData;\n            \/\/ \u83b7\u53d6\u5176\u4ed6\u64cd\u4f5c\u7cfb\u7edf\u5e94\u7528\u7a0b\u5e8f\u7684\u5143\u6570\u636e\n        } catch (PackageManager.NameNotFoundException e) {\n            e.printStackTrace();\n        }\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u4ee3\u7801\u793a\u4f8b\u53ef\u89c1\uff0c\u9e92\u9e9f\u64cd\u4f5c\u7cfb\u7edf\u53ef\u4ee5\u901a\u8fc7\u5404\u79cd\u901a\u4fe1\u534f\u8bae\u4e0e\u5176\u4ed6\u8bbe\u5907\u8fdb\u884c\u4e92\u8054\u4e92\u901a\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u901a\u8fc7\u865a\u62df\u5316\u6280\u672f\u4e0e\u5176\u4ed6\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u96c6\u6210\u3002\u8fd9\u4e3a\u7528\u6237\u5e26\u6765\u4e86\u66f4\u591a\u4fbf\u5229\uff0c\u4e5f\u4e3a\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u5e26\u6765\u4e86\u66f4\u591a\u53ef\u80fd\u6027\u3002\u76f8\u4fe1\u968f\u7740\u9e92\u9e9f\u64cd\u4f5c\u7cfb\u7edf\u7684\u53d1\u5c55\uff0c\u5b83\u4e0e\u5176\u4ed6\u8bbe\u5907\u548c\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u95f4\u7684\u4e92\u8fde\u4e92\u901a\u529f\u80fd\u5c06\u4f1a\u8d8a\u6765\u8d8a\u5f3a\u5927\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u9e92\u9e9f\u64cd\u4f5c\u7cfb\u7edf\u5982\u4f55\u4e0e\u5176\u4ed6\u8bbe\u5907\u548c\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4e92\u8054\u4e92\u901a\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9e92\u9e9f\u64cd\u4f5c\u7cfb\u7edf\uff08harmonyos\uff09\u662f\u534e\u4e3a\u63a8\u51fa\u7684\u4e00\u6b3e\u5168\u573a\u666f\u5206\u5e03\u5f0f\u64cd\u4f5c\u7cfb\u7edf\u3002\u4f5c\u4e3a\u4e00\u6b3e\u65b0\u578b\u64cd\u4f5c\u7cfb\u7edf\uff0c\u9e92\u9e9f\u4e0d\u4ec5\u652f\u6301\u5728\u534e\u4e3a\u8bbe\u5907\u4e0a\u8fd0\u884c\uff0c\u8fd8\u53ef\u4ee5\u4e0e\u5176\u4ed6\u8bbe\u5907\u548c\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4e92\u8054\u4e92\u901a\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u9e92\u9e9f\u64cd\u4f5c\u7cfb\u7edf\u5982\u4f55\u5b9e\u73b0\u4e0e\u5176\u4ed6\u8bbe\u5907\u548c\u64cd\u4f5c\u7cfb\u7edf\u7684\u4e92\u8fde\u4e92\u901a\uff0c\u5e76\u7ed9\u51fa\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u3002 \u9996\u5148\uff0c\u9e92\u9e9f\u64cd\u4f5c\u7cfb\u7edf\u652f\u6301\u591a\u79cd\u901a\u4fe1\u534f\u8bae\uff0c\u5982\u84dd\u7259\u3001Wi-Fi\u3001NFC\u7b49\uff0c\u8fd9\u4f7f\u5f97\u5b83\u80fd\u591f\u8f7b\u677e\u4e0e\u5176\u4ed6\u8bbe\u5907\u8fdb\u884c\u901a\u4fe1\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u901a\u4fe1\u534f\u8bae\uff0c\u9e92\u9e9f\u64cd\u4f5c\u7cfb\u7edf\u53ef\u4ee5\u4e0e\u5176\u4ed6\u8bbe\u5907\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u3001\u6587\u4ef6\u5171\u4eab\u7b49\u64cd\u4f5c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u84dd\u7259\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u7684\u793a\u4f8b\u4ee3\u7801\uff1a import com.huawei.harmonyos.bluetooth.btadapter.BluetoothAdapter; import com.huawei.harmonyos.bluetooth.btle.api.BleCentralManager; import com.huawei.harmonyos.bluetooth.btle.api.BleGatt; import com.huawei.harmonyos.bluetooth.btle.api.BlePeripheralManager; import com.huawei.harmonyos.bluetooth.btle.api.BlePeripheralManagerCallback; public class BluetoothDemo { private BluetoothAdapter bluetoothAdapter; public void connectToBluetoothDevice(String deviceAddress) { bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); BleCentralManager centralManager = bluetoothAdapter.getBleCentralManager(); BleGatt gatt = centralManager.connectGatt(deviceAddress); gatt.setGattCallback(new BlePeripheralManagerCallback() { @Override public void onConnectionStateChange(int newState, int status) { if (newState == BlePeripheralManagerCallback.STATE_CONNECTED) { \/\/ \u8fde\u63a5\u6210\u529f\uff0c\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u7b49\u64cd\u4f5c } else [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-16795","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16795","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=16795"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16795\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=16795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=16795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=16795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}